Last active
August 29, 2015 14:21
-
-
Save masarakki/8de358a363680422d8b7 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module PhpValue | |
def self.indent(depth) | |
' ' * depth * 4 | |
end | |
def self.output(value, depth = 0) | |
case value | |
when true, false | |
value.to_s | |
when nil | |
"null" | |
when String | |
"'#{value}'" | |
when Numeric | |
"#{value}" | |
when Array | |
if value.size > 0 | |
inner = value.map { |v| self.output(v, depth + 1) }.join(",\n#{indent(depth + 1)}") | |
"array(\n#{indent(depth + 1)}#{inner}\n#{indent(depth)})" | |
else | |
"array()" | |
end | |
when Hash | |
inner = value.sort.map do |k, v| | |
vv = self.output(v, depth + 1) | |
"#{indent(depth + 1)}'#{k}' => #{vv}" | |
end.join(",\n") | |
"array(\n#{inner}\n#{indent(depth)})" | |
else | |
"null" | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment