|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* For licensing information, please see the LICENSE file accompanied with this file. |
|
4
|
|
|
* |
|
5
|
|
|
* @author Gerard van Helden <[email protected]> |
|
6
|
|
|
* @copyright 2012 Gerard van Helden <http://melp.nl> |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace Zicht\Tool\Script\Node\Script; |
|
10
|
|
|
|
|
11
|
|
|
use Zicht\Tool\Script\Buffer; |
|
12
|
|
|
use Zicht\Tool\Script\Node\Branch; |
|
13
|
|
|
|
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Class ForIn |
|
17
|
|
|
* |
|
18
|
|
|
* @package Zicht\Tool\Script\Node\Script |
|
19
|
|
|
*/ |
|
20
|
|
|
class ForIn extends Branch implements Annotation |
|
21
|
|
|
{ |
|
22
|
|
|
/** |
|
23
|
|
|
* Construct the decorator with the specified expression as the first and only child node. |
|
24
|
|
|
* |
|
25
|
|
|
* @param \Zicht\Tool\Script\Node\Node $expr |
|
26
|
|
|
* @param string $key |
|
27
|
|
|
* @param string $value |
|
28
|
|
|
*/ |
|
29
|
|
|
public function __construct($expr, $key, $value) |
|
30
|
|
|
{ |
|
31
|
|
|
parent::__construct(array($expr)); |
|
32
|
|
|
$this->key = $key ?: '_key'; |
|
|
|
|
|
|
33
|
|
|
$this->value = $value ?: '_value'; |
|
|
|
|
|
|
34
|
|
|
} |
|
35
|
|
|
/** |
|
36
|
|
|
* Allows the annotation to modify the buffer before the script is compiled. |
|
37
|
|
|
* |
|
38
|
|
|
* @param Buffer $buffer |
|
39
|
|
|
* @return void |
|
40
|
|
|
*/ |
|
41
|
|
|
public function beforeScript(Buffer $buffer) |
|
42
|
|
|
{ |
|
43
|
|
|
$buffer->write('foreach ((array)'); |
|
44
|
|
|
$this->nodes[0]->compile($buffer); |
|
45
|
|
|
$buffer |
|
46
|
|
|
->raw(' as $_key => $_value) {')->eol()->indent(1) |
|
47
|
|
|
->writeln(sprintf('$z->push(\'%s\', $_key);', $this->key)) |
|
48
|
|
|
->writeln(sprintf('$z->push(\'%s\', $_value);', $this->value)) |
|
49
|
|
|
; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Allows the annotation to modify the buffer after the script is compiled. |
|
54
|
|
|
* |
|
55
|
|
|
* @param Buffer $buffer |
|
56
|
|
|
* @return void |
|
57
|
|
|
*/ |
|
58
|
|
|
public function afterScript(Buffer $buffer) |
|
59
|
|
|
{ |
|
60
|
|
|
$buffer->write(sprintf('$z->pop(\'%s\');', $this->key)); |
|
61
|
|
|
$buffer->write(sprintf('$z->pop(\'%s\');', $this->value)); |
|
62
|
|
|
$buffer->indent(-1); |
|
63
|
|
|
$buffer->writeln('}'); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Compiles the node into the buffer. |
|
68
|
|
|
* |
|
69
|
|
|
* @param \Zicht\Tool\Script\Buffer $buffer |
|
70
|
|
|
* @return void |
|
71
|
|
|
*/ |
|
72
|
|
|
public function compile(Buffer $buffer) |
|
73
|
|
|
{ |
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: