shop.balmet.com

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README

Node.php (6061B)


      1 <?php
      2 
      3 /*
      4  * This file is part of Twig.
      5  *
      6  * (c) 2009 Fabien Potencier
      7  * (c) 2009 Armin Ronacher
      8  *
      9  * For the full copyright and license information, please view the LICENSE
     10  * file that was distributed with this source code.
     11  */
     12 
     13 /**
     14  * Represents a node in the AST.
     15  *
     16  * @author Fabien Potencier <fabien@symfony.com>
     17  */
     18 class Twig_Node implements Twig_NodeInterface
     19 {
     20     protected $nodes;
     21     protected $attributes;
     22     protected $lineno;
     23     protected $tag;
     24 
     25     /**
     26      * Constructor.
     27      *
     28      * The nodes are automatically made available as properties ($this->node).
     29      * The attributes are automatically made available as array items ($this['name']).
     30      *
     31      * @param array  $nodes      An array of named nodes
     32      * @param array  $attributes An array of attributes (should not be nodes)
     33      * @param int    $lineno     The line number
     34      * @param string $tag        The tag name associated with the Node
     35      */
     36     public function __construct(array $nodes = array(), array $attributes = array(), $lineno = 0, $tag = null)
     37     {
     38         $this->nodes = $nodes;
     39         $this->attributes = $attributes;
     40         $this->lineno = $lineno;
     41         $this->tag = $tag;
     42     }
     43 
     44     public function __toString()
     45     {
     46         $attributes = array();
     47         foreach ($this->attributes as $name => $value) {
     48             $attributes[] = sprintf('%s: %s', $name, str_replace("\n", '', var_export($value, true)));
     49         }
     50 
     51         $repr = array(get_class($this).'('.implode(', ', $attributes));
     52 
     53         if (count($this->nodes)) {
     54             foreach ($this->nodes as $name => $node) {
     55                 $len = strlen($name) + 4;
     56                 $noderepr = array();
     57                 foreach (explode("\n", (string) $node) as $line) {
     58                     $noderepr[] = str_repeat(' ', $len).$line;
     59                 }
     60 
     61                 $repr[] = sprintf('  %s: %s', $name, ltrim(implode("\n", $noderepr)));
     62             }
     63 
     64             $repr[] = ')';
     65         } else {
     66             $repr[0] .= ')';
     67         }
     68 
     69         return implode("\n", $repr);
     70     }
     71 
     72     /**
     73      * @deprecated since 1.16.1 (to be removed in 2.0)
     74      */
     75     public function toXml($asDom = false)
     76     {
     77         @trigger_error(sprintf('%s is deprecated since version 1.16.1 and will be removed in 2.0.', __METHOD__), E_USER_DEPRECATED);
     78 
     79         $dom = new DOMDocument('1.0', 'UTF-8');
     80         $dom->formatOutput = true;
     81         $dom->appendChild($xml = $dom->createElement('twig'));
     82 
     83         $xml->appendChild($node = $dom->createElement('node'));
     84         $node->setAttribute('class', get_class($this));
     85 
     86         foreach ($this->attributes as $name => $value) {
     87             $node->appendChild($attribute = $dom->createElement('attribute'));
     88             $attribute->setAttribute('name', $name);
     89             $attribute->appendChild($dom->createTextNode($value));
     90         }
     91 
     92         foreach ($this->nodes as $name => $n) {
     93             if (null === $n) {
     94                 continue;
     95             }
     96 
     97             $child = $n->toXml(true)->getElementsByTagName('node')->item(0);
     98             $child = $dom->importNode($child, true);
     99             $child->setAttribute('name', $name);
    100 
    101             $node->appendChild($child);
    102         }
    103 
    104         return $asDom ? $dom : $dom->saveXML();
    105     }
    106 
    107     public function compile(Twig_Compiler $compiler)
    108     {
    109         foreach ($this->nodes as $node) {
    110             $node->compile($compiler);
    111         }
    112     }
    113 
    114     public function getLine()
    115     {
    116         return $this->lineno;
    117     }
    118 
    119     public function getNodeTag()
    120     {
    121         return $this->tag;
    122     }
    123 
    124     /**
    125      * Returns true if the attribute is defined.
    126      *
    127      * @param string $name The attribute name
    128      *
    129      * @return bool true if the attribute is defined, false otherwise
    130      */
    131     public function hasAttribute($name)
    132     {
    133         return array_key_exists($name, $this->attributes);
    134     }
    135 
    136     /**
    137      * Gets an attribute value by name.
    138      *
    139      * @param string $name
    140      *
    141      * @return mixed
    142      */
    143     public function getAttribute($name)
    144     {
    145         if (!array_key_exists($name, $this->attributes)) {
    146             throw new LogicException(sprintf('Attribute "%s" does not exist for Node "%s".', $name, get_class($this)));
    147         }
    148 
    149         return $this->attributes[$name];
    150     }
    151 
    152     /**
    153      * Sets an attribute by name to a value.
    154      *
    155      * @param string $name
    156      * @param mixed  $value
    157      */
    158     public function setAttribute($name, $value)
    159     {
    160         $this->attributes[$name] = $value;
    161     }
    162 
    163     /**
    164      * Removes an attribute by name.
    165      *
    166      * @param string $name
    167      */
    168     public function removeAttribute($name)
    169     {
    170         unset($this->attributes[$name]);
    171     }
    172 
    173     /**
    174      * Returns true if the node with the given name exists.
    175      *
    176      * @param string $name
    177      *
    178      * @return bool
    179      */
    180     public function hasNode($name)
    181     {
    182         return array_key_exists($name, $this->nodes);
    183     }
    184 
    185     /**
    186      * Gets a node by name.
    187      *
    188      * @param string $name
    189      *
    190      * @return Twig_Node
    191      */
    192     public function getNode($name)
    193     {
    194         if (!array_key_exists($name, $this->nodes)) {
    195             throw new LogicException(sprintf('Node "%s" does not exist for Node "%s".', $name, get_class($this)));
    196         }
    197 
    198         return $this->nodes[$name];
    199     }
    200 
    201     /**
    202      * Sets a node.
    203      *
    204      * @param string    $name
    205      * @param Twig_Node $node
    206      */
    207     public function setNode($name, $node = null)
    208     {
    209         $this->nodes[$name] = $node;
    210     }
    211 
    212     /**
    213      * Removes a node by name.
    214      *
    215      * @param string $name
    216      */
    217     public function removeNode($name)
    218     {
    219         unset($this->nodes[$name]);
    220     }
    221 
    222     public function count()
    223     {
    224         return count($this->nodes);
    225     }
    226 
    227     public function getIterator()
    228     {
    229         return new ArrayIterator($this->nodes);
    230     }
    231 }