protected static 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(self::$comparisonOperators[$operator])) {
$operator = self::$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(self::$logicalOperators[$element])) {
$currentOperator = self::$logicalOperators[$element];
$currentOperatorPrecedence = self::$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 self::evaluateLogicalOperator($leftSide, $operator, $rightSide);
}