| 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) |
|
| 48 | { |
||
| 49 | 3 | return $this->buildRanges( |
|
| 50 | 3 | $this->shuntingYard($tokenList) |
|
| 51 | ); |
||
| 52 | } |
||
| 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) |
|
| 63 | { |
||
| 64 | 3 | $stack = new \SplStack(); |
|
| 65 | |||
| 66 | 3 | foreach ($resultList as $result) { |
|
| 67 | 3 | if ($result instanceof VersionRangeInterface) { |
|
| 68 | 3 | $stack->push($result); |
|
| 69 | } else { |
||
| 70 | 3 | $operator1 = $stack->pop(); |
|
| 71 | 3 | $operator2 = $stack->pop(); |
|
| 72 | |||
| 73 | /** @var Token $result */ |
||
| 74 | 3 | if (Token::LOGICAL_AND === $result->getType()) { |
|
| 75 | 2 | $stack->push(new LogicalAnd( |
|
| 76 | $operator2, |
||
| 77 | $operator1 |
||
| 78 | )); |
||
| 79 | } else { |
||
| 80 | 3 | $stack->push(new LogicalOr( |
|
| 81 | $operator2, |
||
| 82 | $operator1 |
||
| 83 | )); |
||
| 84 | } |
||
| 85 | } |
||
| 86 | } |
||
| 87 | |||
| 88 | 3 | return $stack->pop(); |
|
| 89 | } |
||
| 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) { |
|
|
1 ignored issue
–
show
|
|||
| 104 | |||
| 105 | // Accumulate Versions & Comparators |
||
| 106 | 3 | if ($token instanceof VersionRangeInterface) { |
|
| 107 | 3 | $output->enqueue($token); |
|
| 108 | |||
| 109 | // Handle operators |
||
| 110 | } elseif ($token instanceof Token) { |
||
|
1 ignored issue
–
show
|
|||
| 111 | |||
| 112 | // Loop while the current token has higher precedence then the stack token |
||
| 113 | 3 | $operator1 = $token; |
|
| 114 | while ( |
||
| 115 | 3 | $this->hasOperator($operatorStack) |
|
| 116 | 3 | && ($operator2 = $operatorStack->top()) |
|
| 117 | 3 | && $this->hasLowerPrecedence($operator1, $operator2) |
|
| 118 | ) { |
||
| 119 | 1 | $output->enqueue($operatorStack->pop()); |
|
| 120 | } |
||
| 121 | |||
| 122 | 3 | $operatorStack->push($operator1); |
|
| 123 | |||
|
1 ignored issue
–
show
|
|||
| 124 | } else { |
||
| 125 | 3 | throw new \RuntimeException('Invalid version number'); |
|
| 126 | } |
||
| 127 | } |
||
| 128 | |||
| 129 | // Merge remaining operators onto output list |
||
| 130 | 3 | while ($this->hasOperator($operatorStack)) { |
|
| 131 | 3 | $output->enqueue($operatorStack->pop()); |
|
| 132 | } |
||
| 133 | |||
| 134 | 3 | return iterator_to_array($output); |
|
| 135 | } |
||
| 136 | |||
| 137 | /** |
||
| 138 | * Returns true if we have an operator on the stack. |
||
| 139 | * |
||
| 140 | * @param \SplStack $stack |
||
| 141 | * @return bool |
||
| 142 | */ |
||
| 143 | 3 | private function hasOperator(\SplStack $stack) |
|
| 147 | |||
| 148 | /** |
||
| 149 | * Returns true if the first operator has lower precedence than the second. |
||
| 150 | * |
||
| 151 | * @param Token $operator1 |
||
| 152 | * @param Token $operator2 |
||
| 153 | * |
||
| 154 | * @return bool |
||
| 155 | */ |
||
| 156 | 1 | private function hasLowerPrecedence(Token $operator1, Token $operator2) |
|
| 166 | } |
||
| 167 |