Example #1
0
 public function __invoke(Evaluate\Chunk $chunk, Evaluate\Context $context, Evaluate\Bodies $bodies, Evaluate\Parameters $params)
 {
     //get config
     $current = !isset($params->{'key'}) || $params->{'key'} != 'full';
     $output = !isset($params->{'to'}) || $params->{'to'} != 'console';
     //ok, basically we're gonna give parent object w/ two extra values, "__forcedParent__", "__child__", and "__params__"
     $getContext = function (Evaluate\Context $ctx) use($current, &$getContext) {
         //first parent
         $parent = !$current && $ctx->parent != NULL ? $getContext($ctx->parent) : (object) [];
         //now start adding pieces
         $parent->__child__ = $ctx->head == NULL ? NULL : $ctx->head->value;
         if ($ctx->head != NULL && $ctx->head->forcedParent !== NULL) {
             $parent->__forcedParent__ = $ctx->head->forcedParent;
         }
         if ($ctx->head != NULL && !empty($ctx->head->params)) {
             $parent->__params__ = $ctx->head->params;
         }
         return $parent;
     };
     //now json_encode
     $str = $context->parent == NULL ? '{ }' : json_encode($getContext($context->parent), JSON_PRETTY_PRINT);
     //now put where necessary
     if ($output) {
         return $chunk->write($str);
     }
     echo $str . "\n";
     return $chunk;
 }
Example #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);
 }
Example #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]])));
 }
Example #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;
     }
 }
Example #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;
     }
 }
Example #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;
     }
 }
Example #7
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;
     }
 }
Example #8
0
 public function __invoke(Evaluate\Chunk $chunk, Evaluate\Context $context, Evaluate\Bodies $bodies, Evaluate\Parameters $params)
 {
     if (!isset($params->{'key'})) {
         $chunk->setError('Parameter required: key');
     }
     $key = $params->{'key'};
     if (is_null($key)) {
         return $chunk->write('0');
     }
     if (is_numeric($key)) {
         return $chunk->write($key);
     }
     if (is_string($key)) {
         return $chunk->write(strlen($key));
     }
     if (is_array($key) || $key instanceof \Countable) {
         return $chunk->write(count($key));
     }
     if (is_object($key)) {
         return $chunk->write(count(get_object_vars($key)));
     }
     return $chunk->write(strlen(strval($key)));
 }
Example #9
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]])));
     }
 }
Example #10
0
 public function evaluateBuffer(Ast\Buffer $buffer, Context $ctx, Chunk $chunk)
 {
     $chunk->write($buffer->contents);
     return $chunk;
 }