Skip to content

Instantly share code, notes, and snippets.

@masarakki
Last active August 29, 2015 14:21
Show Gist options
  • Save masarakki/8de358a363680422d8b7 to your computer and use it in GitHub Desktop.
Save masarakki/8de358a363680422d8b7 to your computer and use it in GitHub Desktop.
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