1 | <?php |
||
23 | final class LogicalOperatorProcessor |
||
24 | { |
||
25 | /** |
||
26 | * @var array Of operator precedences & associativity. |
||
27 | */ |
||
28 | private $operatorPrecedence = [ |
||
29 | Token::LOGICAL_OR => [ |
||
30 | 'precedence' => 0, |
||
31 | 'associativity' => 'left' |
||
32 | ], |
||
33 | Token::LOGICAL_AND => [ |
||
34 | 'precedence' => 1, |
||
35 | 'associativity' => 'left' |
||
36 | ] |
||
37 | ]; |
||
38 | |||
39 | /** |
||
40 | * Accepts an array of VersionRanges & logical operator tokens & returns a single version range implementing those |
||
41 | * constraints. |
||
42 | * |
||
43 | * @param array $tokenList |
||
44 | * |
||
45 | * @return VersionRangeInterface |
||
46 | */ |
||
47 | 3 | public function run(array $tokenList) |
|
53 | |||
54 | /** |
||
55 | * Accepts an array of ComparatorVersions & logical operators in reverse polish notation & returns a single |
||
56 | * instance of a class implementing VersionRangeInterface. |
||
57 | * |
||
58 | * @param array $resultList |
||
59 | * |
||
60 | * @return VersionRangeInterface |
||
61 | */ |
||
62 | 3 | private function buildRanges(array $resultList) |
|
90 | |||
91 | /** |
||
92 | * Re-order token stream into reverse polish notation. |
||
93 | * |
||
94 | * @param array $tokenList |
||
95 | * |
||
96 | * @return array of ComparatorVersions and logical operator tokens |
||
97 | */ |
||
98 | 3 | private function shuntingYard(array $tokenList) |
|
99 | { |
||
100 | 3 | $operatorStack = new \SplStack(); |
|
101 | 3 | $output = new \SplQueue(); |
|
102 | |||
103 | 3 | foreach ($tokenList as $token) { |
|
104 | // Accumulate Versions & Comparators |
||
105 | 3 | if ($token instanceof VersionRangeInterface) { |
|
106 | 3 | $output->enqueue($token); |
|
107 | |||
108 | // Handle operators |
||
109 | 3 | } elseif ($token instanceof Token) { |
|
110 | // Loop while the current token has higher precedence then the stack token |
||
111 | 3 | $operator1 = $token; |
|
112 | while ( |
||
113 | 3 | $this->hasOperator($operatorStack) |
|
114 | 3 | && ($operator2 = $operatorStack->top()) |
|
115 | 3 | && $this->hasLowerPrecedence($operator1, $operator2) |
|
116 | 3 | ) { |
|
117 | 1 | $output->enqueue($operatorStack->pop()); |
|
118 | 1 | } |
|
119 | |||
120 | 3 | $operatorStack->push($operator1); |
|
121 | 3 | } else { |
|
122 | throw new \RuntimeException('Invalid version number'); |
||
123 | } |
||
124 | 3 | } |
|
125 | |||
126 | // Merge remaining operators onto output list |
||
127 | 3 | while ($this->hasOperator($operatorStack)) { |
|
128 | 3 | $output->enqueue($operatorStack->pop()); |
|
129 | 3 | } |
|
130 | |||
131 | 3 | return iterator_to_array($output); |
|
132 | } |
||
133 | |||
134 | /** |
||
135 | * Returns true if we have an operator on the stack. |
||
136 | * |
||
137 | * @param \SplStack $stack |
||
138 | * @return bool |
||
139 | */ |
||
140 | 3 | private function hasOperator(\SplStack $stack) |
|
144 | |||
145 | /** |
||
146 | * Returns true if the first operator has lower precedence than the second. |
||
147 | * |
||
148 | * @param Token $operator1 |
||
149 | * @param Token $operator2 |
||
150 | * |
||
151 | * @return bool |
||
152 | */ |
||
153 | 1 | private function hasLowerPrecedence(Token $operator1, Token $operator2) |
|
163 | } |
||
164 |