Module: ToPhpArray

Defined in:
lib/to_php_array.rb,
lib/to_php_array/version.rb

Constant Summary collapse

VERSION =
"0.1.0"

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.dump(value, options = {}) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/to_php_array.rb', line 8

def self.dump(value, options = {})
  options = {
    :wrap => false,
    :indent_size => 4,
    :depth => 0,
    :new_line => false
  }.merge(options)

  indent = " " * options[:indent_size] * options[:depth]

  case value
    when Hash
      key_options = options.merge({ :depth => options[:depth] + 1,
                                    :new_line => true })
      value_options = options.merge({ :depth => options[:depth] + 1,
                                      :new_line => false })
      lines = value.map do |key, value|
        "#{dump(key, key_options)} => #{dump(value, value_options)}"
      end
      if options[:wrap]
        res = "array(\n" + lines.join(",\n") + "\n" + indent + ")"
      else
        res = "array(" + lines.join(", ") + ")"
      end
    when Array
      value_options = options.merge({ :depth => options[:depth] + 1,
                                      :new_line => true })
      lines = value.map do |value|
        dump(value, value_options)
      end
      if options[:wrap]
        res = "array(\n" + lines.join(",\n") + "\n" + indent + ")"
      else
        res = "array(" + lines.join(", ") + ")"
      end
    when Fixnum
      res = "#{value.to_s}"
    when Symbol
      res = "'" + value.to_s.gsub(/\'/, "\\\\'") + "'"
    when String
      res = "'" + value.gsub(/\'/, "\\\\'") + "'"
  end

  if options[:wrap] && options[:new_line]
    indent + res
  else
    res
  end
end

Instance Method Details

#to_php_array(options = {}) ⇒ Object



4
5
6
# File 'lib/to_php_array.rb', line 4

def to_php_array(options = {})
  ::ToPhpArray::dump(self, options)
end