|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the ILess |
|
5
|
|
|
* |
|
6
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
7
|
|
|
* file that was distributed with this source code. |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
namespace ILess\Node; |
|
11
|
|
|
|
|
12
|
|
|
use ILess\Context; |
|
13
|
|
|
use ILess\Node; |
|
14
|
|
|
use ILess\Util; |
|
15
|
|
|
use ILess\Visitor\VisitorInterface; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Condition. |
|
19
|
|
|
*/ |
|
20
|
|
|
class ConditionNode extends Node |
|
21
|
|
|
{ |
|
22
|
|
|
/** |
|
23
|
|
|
* Node type. |
|
24
|
|
|
* |
|
25
|
|
|
* @var string |
|
26
|
|
|
*/ |
|
27
|
|
|
protected $type = 'Condition'; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* The operator. |
|
31
|
|
|
* |
|
32
|
|
|
* @var string |
|
33
|
|
|
*/ |
|
34
|
|
|
private $op; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* The left operand. |
|
38
|
|
|
* |
|
39
|
|
|
* @var Node |
|
40
|
|
|
*/ |
|
41
|
|
|
private $lvalue; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* The right operand. |
|
45
|
|
|
* |
|
46
|
|
|
* @var Node |
|
47
|
|
|
*/ |
|
48
|
|
|
private $rvalue; |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Current index. |
|
52
|
|
|
* |
|
53
|
|
|
* @var int |
|
54
|
|
|
*/ |
|
55
|
|
|
private $index = 0; |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Negate the result? |
|
59
|
|
|
* |
|
60
|
|
|
* @var bool |
|
61
|
|
|
*/ |
|
62
|
|
|
private $negate = false; |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* Constructor. |
|
66
|
|
|
* |
|
67
|
|
|
* @param string $op The operator |
|
68
|
|
|
* @param Node $l The left operand |
|
69
|
|
|
* @param Node $r The right operand |
|
70
|
|
|
* @param int $i |
|
71
|
|
|
* @param bool $negate |
|
72
|
|
|
*/ |
|
73
|
|
|
public function __construct($op, Node $l, Node $r, $i = 0, $negate = false) |
|
74
|
|
|
{ |
|
75
|
|
|
$this->op = trim($op); |
|
76
|
|
|
$this->lvalue = $l; |
|
77
|
|
|
$this->rvalue = $r; |
|
78
|
|
|
$this->index = $i; |
|
79
|
|
|
$this->negate = (boolean) $negate; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* {@inheritdoc} |
|
84
|
|
|
*/ |
|
85
|
|
|
public function accept(VisitorInterface $visitor) |
|
86
|
|
|
{ |
|
87
|
|
|
$this->lvalue = $visitor->visit($this->lvalue); |
|
88
|
|
|
$this->rvalue = $visitor->visit($this->rvalue); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* Compiles the node. |
|
93
|
|
|
* |
|
94
|
|
|
* @param Context $context The context |
|
95
|
|
|
* @param array|null $arguments Array of arguments |
|
96
|
|
|
* @param bool|null $important Important flag |
|
97
|
|
|
* |
|
98
|
|
|
* @return bool |
|
99
|
|
|
*/ |
|
100
|
|
|
public function compile(Context $context, $arguments = null, $important = null) |
|
101
|
|
|
{ |
|
102
|
|
|
$a = $this->lvalue->compile($context); |
|
103
|
|
|
$b = $this->rvalue->compile($context); |
|
104
|
|
|
|
|
105
|
|
|
switch ($this->op) { |
|
106
|
|
|
case 'and': |
|
107
|
|
|
$result = $a && $b; |
|
108
|
|
|
break; |
|
109
|
|
|
case 'or': |
|
110
|
|
|
$result = $a || $b; |
|
111
|
|
|
break; |
|
112
|
|
|
default: |
|
113
|
|
|
$compared = Util::compareNodes($a, $b); |
|
114
|
|
|
// strict comparison, we cannot use switch here |
|
115
|
|
|
if ($compared === -1) { |
|
116
|
|
|
$result = $this->op === '<' || $this->op === '=<' || $this->op === '<='; |
|
117
|
|
|
} elseif ($compared === 0) { |
|
118
|
|
|
$result = $this->op === '=' || $this->op === '>=' || $this->op === '=<' || $this->op === '<='; |
|
119
|
|
|
} elseif ($compared === 1) { |
|
120
|
|
|
$result = $this->op === '>' || $this->op === '>=' || $this->op === '=>'; |
|
121
|
|
|
} else { |
|
122
|
|
|
$result = false; |
|
123
|
|
|
} |
|
124
|
|
|
break; |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
return $this->negate ? !$result : $result; |
|
|
|
|
|
|
128
|
|
|
} |
|
129
|
|
|
} |
|
130
|
|
|
|
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_functionexpects aPostobject, and outputs the author of the post. The base classPostreturns a simple string and outputting a simple string will work just fine. However, the child classBlogPostwhich is a sub-type ofPostinstead decided to return anobject, and is therefore violating the SOLID principles. If aBlogPostwere passed tomy_function, PHP would not complain, but ultimately fail when executing thestrtouppercall in its body.