Exemple #1
0
 /**
  * @throws \RuntimeException
  * @param array $stack
  * @return boolean
  */
 protected function evaluateStack(array $stack)
 {
     $stackCount = count($stack);
     if (0 === $stackCount) {
         return FALSE;
     } elseif (1 === $stackCount) {
         return BooleanNode::convertToBoolean(reset($stack));
     } elseif (3 === $stackCount) {
         list($leftSide, $operator, $rightSide) = array_values($stack);
         if (TRUE === is_string($operator) && TRUE === isset($this->comparisonOperators[$operator])) {
             $operator = $this->comparisonOperators[$operator];
             return BooleanNode::evaluateComparator($operator, $leftSide, $rightSide);
         }
     }
     $operator = FALSE;
     $operatorPrecedence = PHP_INT_MAX;
     $operatorIndex = FALSE;
     foreach ($stack as $index => $element) {
         if (TRUE === is_string($element) && TRUE === isset($this->logicalOperators[$element])) {
             $currentOperator = $this->logicalOperators[$element];
             $currentOperatorPrecedence = $this->operatorPrecedence[$currentOperator];
             if ($currentOperatorPrecedence <= $operatorPrecedence) {
                 $operator = $currentOperator;
                 $operatorPrecedence = $currentOperatorPrecedence;
                 $operatorIndex = $index;
             }
         }
     }
     if (FALSE === $operator) {
         throw new \RuntimeException('The stack was not comparable and did not include any logical operators.', 1385071197);
     }
     $operatorIndex = array_search($operatorIndex, array_keys($stack));
     if (0 === $operatorIndex || $operatorIndex + 1 >= $stackCount) {
         throw new \RuntimeException('The stack may not contain a logical operator at the first or last element.', 1385072228);
     }
     $leftSide = array_slice($stack, 0, $operatorIndex);
     $rightSide = array_slice($stack, $operatorIndex + 1);
     return $this->evaluateLogicalOperator($leftSide, $operator, $rightSide);
 }
 /**
  * @test
  */
 public function convertToBooleanProperlyConvertsObjects()
 {
     $this->assertFalse(\TYPO3\CMS\Fluid\Core\Parser\SyntaxTree\BooleanNode::convertToBoolean(NULL));
     $this->assertTrue(\TYPO3\CMS\Fluid\Core\Parser\SyntaxTree\BooleanNode::convertToBoolean(new \stdClass()));
 }
 /**
  * @param \TYPO3\CMS\Fluid\Core\Parser\SyntaxTree\BooleanNode $node
  * @return array
  * @see convert()
  */
 protected function convertBooleanNode(\TYPO3\CMS\Fluid\Core\Parser\SyntaxTree\BooleanNode $node)
 {
     $initializationPhpCode = '// Rendering Boolean node' . chr(10);
     if ($node->getComparator() !== NULL) {
         $convertedLeftSide = $this->convert($node->getLeftSide());
         $convertedRightSide = $this->convert($node->getRightSide());
         return array('initialization' => $initializationPhpCode . $convertedLeftSide['initialization'] . $convertedRightSide['initialization'], 'execution' => sprintf('TYPO3\\CMS\\Fluid\\Core\\Parser\\SyntaxTree\\BooleanNode::evaluateComparator(\'%s\', %s, %s)', $node->getComparator(), $convertedLeftSide['execution'], $convertedRightSide['execution']));
     } else {
         // simple case, no comparator.
         $converted = $this->convert($node->getSyntaxTreeNode());
         return array('initialization' => $initializationPhpCode . $converted['initialization'], 'execution' => sprintf('TYPO3\\CMS\\Fluid\\Core\\Parser\\SyntaxTree\\BooleanNode::convertToBoolean(%s)', $converted['execution']));
     }
 }