|
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\Expr\Op; |
|
10
|
|
|
|
|
11
|
|
|
use Zicht\Tool\Script\Node\Branch; |
|
12
|
|
|
use Zicht\Tool\Script\Node\Node; |
|
13
|
|
|
use Zicht\Tool\Script\Buffer; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Represents a binary expression node. |
|
17
|
|
|
*/ |
|
18
|
|
|
class Binary extends Branch |
|
19
|
|
|
{ |
|
20
|
|
|
/** |
|
21
|
|
|
* Constructor. |
|
22
|
|
|
* |
|
23
|
|
|
* @param string $operator |
|
24
|
|
|
* @param Node $left |
|
25
|
|
|
* @param Node $right |
|
26
|
|
|
*/ |
|
27
|
|
|
public function __construct($operator, $left, $right) |
|
28
|
|
|
{ |
|
29
|
|
|
parent::__construct(); |
|
30
|
|
|
$this->operator = $operator; |
|
|
|
|
|
|
31
|
|
|
$this->nodes[0] = $left; |
|
32
|
|
|
$this->nodes[1] = $right; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @{inheritDoc} |
|
38
|
|
|
*/ |
|
39
|
|
|
public function compile(Buffer $buffer) |
|
40
|
|
|
{ |
|
41
|
|
|
if ($this->operator === '=~') { |
|
42
|
|
|
$buffer->raw('(bool)preg_match((string)'); |
|
43
|
|
|
$this->nodes[1]->compile($buffer); |
|
44
|
|
|
$buffer->raw(', (string)'); |
|
45
|
|
|
$this->nodes[0]->compile($buffer); |
|
46
|
|
|
$buffer->raw(')'); |
|
47
|
|
|
} elseif ($this->operator === 'in') { |
|
48
|
|
|
$buffer->raw('in_array('); |
|
49
|
|
|
$this->nodes[0]->compile($buffer); |
|
50
|
|
|
$buffer->raw(', (array)'); |
|
51
|
|
|
$this->nodes[1]->compile($buffer); |
|
52
|
|
|
$buffer->raw(')'); |
|
53
|
|
|
} else { |
|
54
|
|
|
$this->nodes[0]->compile($buffer); |
|
55
|
|
|
$buffer->raw($this->operator); |
|
56
|
|
|
$this->nodes[1]->compile($buffer); |
|
57
|
|
|
} |
|
58
|
|
|
} |
|
59
|
|
|
} |
|
60
|
|
|
|
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: