Exemple #1
0
 protected function expression(Expression $expr)
 {
     if ($expr instanceof Expressions\Binary) {
         $this->codeEmitter->raw('(');
         $this->expression($expr->getLeft());
         $this->codeEmitter->raw(' ' . $expr->getOperation() . ' ');
         $this->expression($expr->getRight());
         $this->codeEmitter->raw(')');
     } else {
         if ($expr instanceof Expressions\Unary) {
             $showParens = !$this->canParensBeOmittedFor($expr->getExpression());
             $this->codeEmitter->raw($expr->getOperation());
             if ($showParens) {
                 $this->codeEmitter->raw('(');
             }
             $this->expression($expr->getExpression());
             if ($showParens) {
                 $this->codeEmitter->raw(')');
             }
         } else {
             if ($expr instanceof Expressions\MethodCall) {
                 $this->codeEmitter->raw('helpers' . $this->createJsVariableDereference($expr->getMethodName()) . '(');
                 $arguments = $expr->getArguments();
                 for ($i = 0; $i < count($arguments); $i++) {
                     $this->expression($arguments[$i]);
                     if ($i < count($arguments) - 1) {
                         $this->codeEmitter->raw(', ');
                     }
                 }
                 $this->codeEmitter->raw(')');
             } else {
                 if ($expr instanceof Expressions\GetAttribute) {
                     $this->expression($expr->getExpression());
                     $this->codeEmitter->raw($this->createJsVariableDereference($expr->getAttributeName()));
                 } else {
                     if ($expr instanceof Expressions\Variable) {
                         $this->codeEmitter->raw($this->currentContext . $this->createJsVariableDereference($expr->getVariableName()));
                     } else {
                         if ($expr instanceof Expressions\Scalar) {
                             $this->codeEmitter->raw($this->escapeScalar($expr->getValue()));
                         } else {
                             throw new \LogicException("Unknown expression class " . get_class($expr));
                         }
                     }
                 }
             }
         }
     }
 }
Exemple #2
0
 /**
  * Helper function which returns true if the expression requires boolean input values
  *
  * @param Expression $expr
  * @return bool
  */
 protected static function expressionRequiresBooleanInput(Expression $expr)
 {
     return $expr instanceof Expressions\Binary && in_array($expr->getOperation(), [Expressions\Binary::OPERATOR_AND, Expressions\Binary::OPERATOR_OR]) || $expr instanceof Expressions\Unary && $expr->getOperation() === Expressions\Unary::OPERATION_NOT;
 }
Exemple #3
0
 protected function expression(Expression $expr)
 {
     if ($expr instanceof Expressions\Binary) {
         fwrite($this->output, "(");
         $this->expression($expr->getLeft());
         fwrite($this->output, " " . $expr->getOperation() . " ");
         $this->expression($expr->getRight());
         fwrite($this->output, ")");
     } else {
         if ($expr instanceof Expressions\Unary) {
             $showParens = !$this->canParensBeOmittedFor($expr->getExpression());
             fwrite($this->output, $expr->getOperation());
             if ($showParens) {
                 fwrite($this->output, "(");
             }
             $this->expression($expr->getExpression());
             if ($showParens) {
                 fwrite($this->output, ")");
             }
         } else {
             if ($expr instanceof Expressions\MethodCall) {
                 fwrite($this->output, $expr->getMethodName() . "(");
                 $arguments = $expr->getArguments();
                 for ($i = 0; $i < count($arguments); $i++) {
                     $this->expression($arguments[$i]);
                     if ($i < count($arguments) - 1) {
                         fwrite($this->output, ", ");
                     }
                 }
                 fwrite($this->output, ")");
             } else {
                 if ($expr instanceof Expressions\GetAttribute) {
                     $this->expression($expr->getExpression());
                     fwrite($this->output, '[' . $this->escapeScalar($expr->getAttributeName()) . ']');
                 } else {
                     if ($expr instanceof Expressions\Variable) {
                         fwrite($this->output, '$' . $this->currentContext . '[' . $this->escapeScalar($expr->getVariableName()) . ']');
                     } else {
                         if ($expr instanceof Expressions\Scalar) {
                             fwrite($this->output, $this->escapeScalar($expr->getValue()));
                         } else {
                             throw new \LogicException("Unknown expression class " . get_class($expr));
                         }
                     }
                 }
             }
         }
     }
 }
Exemple #4
0
 protected function expression(Expression $expr, $omitParens = false)
 {
     // TODO: implemente a more intelligent algorithm to decide whether or not to render parentheses
     if ($expr instanceof Expressions\Binary) {
         if (!$omitParens) {
             fwrite($this->output, "(");
         }
         $this->expression($expr->getLeft());
         fwrite($this->output, " " . $expr->getOperation() . " ");
         $this->expression($expr->getRight());
         if (!$omitParens) {
             fwrite($this->output, ")");
         }
     } else {
         if ($expr instanceof Expressions\Unary) {
             $omitParens |= $this->canParensBeOmittedFor($expr->getExpression());
             fwrite($this->output, $expr->getOperation());
             if (!$omitParens) {
                 fwrite($this->output, "(");
             }
             $this->expression($expr->getExpression());
             if (!$omitParens) {
                 fwrite($this->output, ")");
             }
         } else {
             if ($expr instanceof Expressions\MethodCall) {
                 fwrite($this->output, $expr->getMethodName() . "(");
                 $arguments = $expr->getArguments();
                 for ($i = 0; $i < count($arguments); $i++) {
                     $this->expression($arguments[$i]);
                     if ($i < count($arguments) - 1) {
                         fwrite($this->output, ", ");
                     }
                 }
                 fwrite($this->output, ")");
             } else {
                 if ($expr instanceof Expressions\GetAttribute) {
                     $this->expression($expr->getExpression());
                     fwrite($this->output, '.' . $expr->getAttributeName());
                 } else {
                     if ($expr instanceof Expressions\Variable) {
                         fwrite($this->output, $expr->getVariableName());
                     } else {
                         if ($expr instanceof Expressions\Scalar) {
                             fwrite($this->output, $this->escapeScalar($expr->getValue()));
                         } else {
                             throw new \LogicException("Unknown expression class " . get_class($expr));
                         }
                     }
                 }
             }
         }
     }
 }