Class: PHP::Loop::ForEach

Inherits:
PHP::Loop show all
Defined in:
lib/php/syntax/loop.rb

Overview

See Also:

Instance Attribute Summary

Instance Method Summary

Methods inherited from PHP::Node

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

Constructor Details

- (ForEach) initialize(iterable, iterator, *body)

A new instance of ForEach

Parameters:



45
46
47
48
49
# File 'lib/php/syntax/loop.rb', line 45

def initialize(iterable, iterator, *body)
  @iterable = iterable
  @iterator = iterator
  @children = body.map { |exp| Block.for(exp) }
end

Instance Attribute Details

- (Expression) iterable

Returns:



35
36
37
# File 'lib/php/syntax/loop.rb', line 35

def iterable
  @iterable
end

- (Expression) iterator

Returns:



39
40
41
# File 'lib/php/syntax/loop.rb', line 39

def iterator
  @iterator
end

Instance Method Details

- (String) to_php

Returns the PHP representation of this foreach loop.

Returns:

  • (String)


55
56
57
58
59
60
61
62
# File 'lib/php/syntax/loop.rb', line 55

def to_php
  body = children.map(&:to_php).join('; ')
  if iterator.children?
    "foreach (#{iterable} as #{iterator.children[0]} => #{iterator.children[1]}) { #{body} }" # FIXME
  else
    "foreach (#{iterable} as #{iterator}) { #{body} }" # FIXME
  end
end