Class: PHP::Literal

Inherits:
PHP::Expression show all
Defined in:
lib/php/syntax/literal.rb

Overview

See Also:

Instance Attribute Summary

Instance Method Summary

Methods inherited from PHP::Node

#<<, #children?, #each, #indent, #indent=, #to_s

Constructor Details

- (Literal) initialize(value)

A new instance of Literal

Parameters:

  • (Object) value


9
10
11
# File 'lib/php/syntax/literal.rb', line 9

def initialize(value)
  @value = value
end

Instance Attribute Details

- (Object) value

Returns the value of attribute value



5
6
7
# File 'lib/php/syntax/literal.rb', line 5

def value
  @value
end

Instance Method Details

- (Integer) <=>(other)

Compares this literal to other for sorting purposes.

Returns:

  • (Integer) — -1, 0, 1


17
18
19
# File 'lib/php/syntax/literal.rb', line 17

def <=>(other)
  value <=> other.value
end

- (String) to_php

Returns the PHP representation of this literal.

Returns:

  • (String)


25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/php/syntax/literal.rb', line 25

def to_php
  case value
    when NilClass   then 'NULL'
    when FalseClass then 'FALSE'
    when TrueClass  then 'TRUE'
    when Integer    then value.to_s
    when Float      then value.to_s
    when String     then value.inspect # FIXME
    when Regexp     then "'#{value.inspect}'"
    when Range
      if value.exclude_end?
        "range(#{value.first}, #{value.last - 1})" # FIXME
      else
        "range(#{value.first}, #{value.last})"
      end
    when ::Array
      "array(#{value.join(', ')})"
    when ::Hash
      pairs = value.to_a.map { |k, v| "#{k} => #{v}" }
      "array(#{pairs.join(', ')})"
    else value.to_s
  end
end