Exemplo n.º 1
0
    /**
     * @param int $depth
     */
    private function writeNode(NodeInterface $node, $depth = 0)
    {
        $comments = array();
        $default = '';
        $defaultArray = null;
        $children = null;
        $example = $node->getExample();

        // defaults
        if ($node instanceof ArrayNode) {
            $children = $node->getChildren();

            if ($node instanceof PrototypedArrayNode) {
                $prototype = $node->getPrototype();

                if ($prototype instanceof ArrayNode) {
                    $children = $prototype->getChildren();
                }

                // check for attribute as key
                if ($key = $node->getKeyAttribute()) {
                    $keyNode = new ArrayNode($key, $node);
                    $keyNode->setInfo('Prototype');

                    // add children
                    foreach ($children as $childNode) {
                        $keyNode->addChild($childNode);
                    }
                    $children = array($key => $keyNode);
                }
            }

            if (!$children) {
                if ($node->hasDefaultValue() && count($defaultArray = $node->getDefaultValue())) {
                    $default = '';
                } elseif (!is_array($example)) {
                    $default = '[]';
                }
            }
        } else {
            $default = '~';

            if ($node->hasDefaultValue()) {
                $default = $node->getDefaultValue();

                if (true === $default) {
                    $default = 'true';
                } elseif (false === $default) {
                    $default = 'false';
                } elseif (null === $default) {
                    $default = '~';
                }
            }
        }

        // required?
        if ($node->isRequired()) {
            $comments[] = 'Required';
        }

        // example
        if ($example && !is_array($example)) {
            $comments[] = 'Example: '.$example;
        }

        $default = (string) $default != '' ? ' '.$default : '';
        $comments = count($comments) ? '# '.implode(', ', $comments) : '';

        $text = sprintf('%-20s %s %s', $node->getName().':', $default, $comments);

        if ($info = $node->getInfo()) {
            $this->writeLine('');
            $this->writeLine('# '.$info, $depth * 4);
        }

        $this->writeLine($text, $depth * 4);

        // output defaults
        if ($defaultArray) {
            $this->writeLine('');

            $message = count($defaultArray) > 1 ? 'Defaults' : 'Default';

            $this->writeLine('# '.$message.':', $depth * 4 + 4);

            $this->writeArray($defaultArray, $depth + 1);
        }

        if (is_array($example)) {
            $this->writeLine('');

            $message = count($example) > 1 ? 'Examples' : 'Example';

            $this->writeLine('# '.$message.':', $depth * 4 + 4);

            $this->writeArray($example, $depth + 1);
        }

        if ($children) {
            foreach ($children as $childNode) {
                $this->writeNode($childNode, $depth + 1);
            }
        }
    }
Exemplo n.º 2
0
 /**
  * Parses a template string into a template node tree, starting
  * at the specified offset. All created nodes are added to the
  * given sequence node.
  *
  * @param ArrayNode $node   template node to build
  * @param string    $string string to parse
  * @param int       $pos    offset in string
  *
  * @return int      new offset in the string
  */
 private static function parseTemplate(ArrayNode $node, $string, $pos)
 {
     $len = strlen($string);
     while ($pos < $len) {
         $next_pos = strpos($string, self::$tag_start, $pos);
         if ($next_pos === false) {
             $child = new TextNode(substr($string, $pos));
             $node->addChild($child);
             break;
         }
         if ($next_pos > $pos) {
             $child = new TextNode(substr($string, $pos, $next_pos - $pos));
             $node->addChild($child);
         }
         $pos = $next_pos + strlen(self::$tag_start);
         $next_pos = self::skipTokens($string, $pos);
         $scanner = new Scanner(substr($string, $pos, $next_pos - $pos));
         $pos = $next_pos + strlen(self::$tag_end);
         switch ($scanner->nextToken()) {
             case T_FOREACH:
                 $scanner->nextToken();
                 $child = new IteratorNode(self::parseExpr($scanner));
                 $pos = self::parseTemplate($child, $string, $pos);
                 $node->addChild($child);
                 break;
             case T_ENDFOREACH:
                 return $pos;
             case T_IF:
                 $scanner->nextToken();
                 $child = new ConditionNode(self::parseExpr($scanner));
                 $pos = self::parseTemplate($child, $string, $pos);
                 $node->addChild($child);
                 break;
             case T_ELSEIF:
                 $scanner->nextToken();
                 $child = new ConditionNode(self::parseExpr($scanner));
                 $node->addElse();
                 $node->addChild($child);
                 return self::parseTemplate($child, $string, $pos);
             case T_ELSE:
                 $scanner->nextToken();
                 $node->addElse();
                 break;
             case T_ENDIF:
                 return $pos;
             default:
                 $child = new ExpressionNode(self::parseExpr($scanner));
                 $node->addChild($child);
         }
         if ($scanner->tokenType() !== false) {
             throw new TemplateParserException('syntax error', $scanner);
         }
     }
     return $pos;
 }
Exemplo n.º 3
0
 /**
  * Returns a string representation of this node.
  *
  * @param Context $context  symbol table
  */
 public function render($context)
 {
     if ($this->condition->value($context)) {
         return parent::render($context);
     }
     return $this->else_node ? $this->else_node->render($context) : '';
 }