Beispiel #1
0
 public function __invoke(Evaluate\Chunk $chunk, Evaluate\Context $context, Evaluate\Bodies $bodies, Evaluate\Parameters $params)
 {
     //load value
     if (!isset($params->{'value'})) {
         $chunk->setError('Value parameter required');
     }
     $value = $params->{'value'};
     //load select info
     $selectInfo = $context->get('__selectInfo');
     //load key
     $key = NULL;
     if (isset($params->{'key'})) {
         $key = $params->{'key'};
     } elseif ($selectInfo != NULL) {
         $key = $selectInfo->key;
     } else {
         $chunk->setError('Must be in select or have key parameter');
     }
     if ($bodies->block === null) {
         $bodies->block = new \Dust\Ast\Body(0);
     }
     //check
     if ($this->isValid($key, $value)) {
         if ($selectInfo != NULL) {
             $selectInfo->selectComparisonSatisfied = true;
         }
         return $chunk->render($bodies->block, $context);
     } elseif (isset($bodies['else'])) {
         return $chunk->render($bodies['else'], $context);
     } else {
         return $chunk;
     }
 }
Beispiel #2
0
 public function __invoke(Evaluate\Chunk $chunk, Evaluate\Context $context, Evaluate\Bodies $bodies, Evaluate\Parameters $params)
 {
     $selectInfo = $context->get('__selectInfo');
     if ($selectInfo != NULL) {
         $selectInfo->selectComparisonSatisfied = true;
     }
     return $chunk->render($bodies->block, $context);
 }
Beispiel #3
0
 public function __invoke(Evaluate\Chunk $chunk, Evaluate\Context $context, Evaluate\Bodies $bodies, Evaluate\Parameters $params)
 {
     //evaluate key here
     if (!isset($params->{'key'})) {
         $chunk->setError('Key parameter required');
     }
     $key = $params->{'key'};
     //just eval body with some special state
     return $chunk->render($bodies->block, $context->pushState(new Evaluate\State((object) ['__selectInfo' => (object) ['selectComparisonSatisfied' => false, 'key' => $key]])));
 }
Beispiel #4
0
 public function __invoke(Evaluate\Chunk $chunk, Evaluate\Context $context, Evaluate\Bodies $bodies)
 {
     $iterationCount = $context->get('$iter');
     if ($iterationCount === NULL) {
         $chunk->setError('First must be inside an array');
     }
     $len = $context->get('$len');
     if ($iterationCount == 0) {
         return $chunk->render($bodies->block, $context);
     } else {
         return $chunk;
     }
 }
Beispiel #5
0
 public function __invoke(Evaluate\Chunk $chunk, Evaluate\Context $context, Evaluate\Bodies $bodies)
 {
     $selectInfo = $context->get('__selectInfo');
     if ($selectInfo == NULL) {
         $chunk->setError('Default must be inside select');
     }
     //check
     if (!$selectInfo->selectComparisonSatisfied) {
         return $chunk->render($bodies->block, $context);
     } else {
         return $chunk;
     }
 }
Beispiel #6
0
 public function __invoke(Evaluate\Chunk $chunk, Evaluate\Context $context, Evaluate\Bodies $bodies)
 {
     //scary and dumb! won't include in default...
     $cond = $context->get('cond');
     if ($cond === NULL) {
         $chunk->setError('Unable to find cond for if');
     }
     if (eval('return ' . $cond . ';')) {
         return $chunk->render($bodies->block, $context);
     } elseif (isset($bodies['else'])) {
         return $chunk->render($bodies['else'], $context);
     } else {
         return $chunk;
     }
 }
Beispiel #7
0
 public function __invoke(Evaluate\Chunk $chunk, Evaluate\Context $context, Evaluate\Bodies $bodies)
 {
     $result = $context->get('key');
     if ($result === NULL) {
         $chunk->setError('Key required');
     }
     $method = $context->get('method');
     if ($method === NULL) {
         $chunk->setError('Method required');
     }
     $operand = $context->get('operand');
     switch ($method) {
         case 'add':
             $result += $operand;
             break;
         case 'subtract':
             $result -= $operand;
             break;
         case 'multiply':
             $result *= $operand;
             break;
         case 'divide':
             $result /= $operand;
             break;
         case 'mod':
             $result %= $operand;
             break;
         case 'abs':
             $result = abs($result);
             break;
         case 'floor':
             $result = floor($result);
             break;
         case 'ceil':
             $result = ceil($result);
             break;
         default:
             $chunk->setError('Unknown method: ' . $method);
     }
     //no bodies means just write
     if ($bodies == NULL || $bodies->block == NULL) {
         return $chunk->write($result);
     } else {
         //just eval body with some special state
         return $chunk->render($bodies->block, $context->pushState(new Evaluate\State((object) ['__selectInfo' => (object) ['selectComparisonSatisfied' => false, 'key' => $result]])));
     }
 }
Beispiel #8
0
 public function evaluateParameters(array $params, Context $ctx)
 {
     $ret = [];
     foreach ($params as $value) {
         if ($value instanceof Ast\NumericParameter) {
             if (strpos($value->value, '.') === false) {
                 $ret[$value->key] = intval($value->value);
             } else {
                 $ret[$value->key] = floatval($value->value);
             }
         } elseif ($value instanceof Ast\IdentifierParameter) {
             $ret[$value->key] = $ctx->resolve($value->value);
         } else {
             //we just set this as the actual AST since it is resolved where it's emitted
             $ret[$value->key] = $value->value;
         }
     }
     return $ret;
 }