vendor/symfony/src/Symfony/Component/Form/FormView.php
changeset 0 7f95f8617b0b
equal deleted inserted replaced
-1:000000000000 0:7f95f8617b0b
       
     1 <?php
       
     2 
       
     3 /*
       
     4  * This file is part of the Symfony package.
       
     5  *
       
     6  * (c) Fabien Potencier <fabien@symfony.com>
       
     7  *
       
     8  * For the full copyright and license information, please view the LICENSE
       
     9  * file that was distributed with this source code.
       
    10  */
       
    11 
       
    12 namespace Symfony\Component\Form;
       
    13 
       
    14 class FormView implements \ArrayAccess, \IteratorAggregate, \Countable
       
    15 {
       
    16     private $vars = array(
       
    17         'value' => null,
       
    18         'attr'  => array(),
       
    19     );
       
    20 
       
    21     private $parent;
       
    22 
       
    23     private $children = array();
       
    24 
       
    25     /**
       
    26      * Is the form attached to this renderer rendered?
       
    27      *
       
    28      * Rendering happens when either the widget or the row method was called.
       
    29      * Row implicitly includes widget, however certain rendering mechanisms
       
    30      * have to skip widget rendering when a row is rendered.
       
    31      *
       
    32      * @var Boolean
       
    33      */
       
    34     private $rendered = false;
       
    35 
       
    36     /**
       
    37      * @param string $name
       
    38      * @param mixed $value
       
    39      *
       
    40      * @return FormView The current view
       
    41      */
       
    42     public function set($name, $value)
       
    43     {
       
    44         $this->vars[$name] = $value;
       
    45 
       
    46         return $this;
       
    47     }
       
    48 
       
    49     /**
       
    50      * @param $name
       
    51      * @return Boolean
       
    52      */
       
    53     public function has($name)
       
    54     {
       
    55         return array_key_exists($name, $this->vars);
       
    56     }
       
    57 
       
    58     /**
       
    59      * @param $name
       
    60      * @param $default
       
    61      *
       
    62      * @return mixed
       
    63      */
       
    64     public function get($name, $default = null)
       
    65     {
       
    66         if (false === $this->has($name)) {
       
    67             return $default;
       
    68         }
       
    69 
       
    70         return $this->vars[$name];
       
    71     }
       
    72 
       
    73     /**
       
    74      * @return array
       
    75      */
       
    76     public function all()
       
    77     {
       
    78         return $this->vars;
       
    79     }
       
    80 
       
    81     /**
       
    82      * Alias of all so it is possible to do `form.vars.foo`
       
    83      *
       
    84      * @return array
       
    85      */
       
    86     public function getVars()
       
    87     {
       
    88         return $this->all();
       
    89     }
       
    90 
       
    91     /**
       
    92      * Sets the value for an attribute.
       
    93      *
       
    94      * @param string $name  The name of the attribute
       
    95      * @param string $value The value
       
    96      *
       
    97      * @return FormView The current view
       
    98      */
       
    99     public function setAttribute($name, $value)
       
   100     {
       
   101         $this->vars['attr'][$name] = $value;
       
   102 
       
   103         return $this;
       
   104     }
       
   105 
       
   106     /**
       
   107      * Returns whether the attached form is rendered.
       
   108      *
       
   109      * @return Boolean Whether the form is rendered
       
   110      */
       
   111     public function isRendered()
       
   112     {
       
   113         $hasChildren = 0 < count($this->children);
       
   114 
       
   115         if (true === $this->rendered || !$hasChildren) {
       
   116             return $this->rendered;
       
   117         }
       
   118 
       
   119         if ($hasChildren) {
       
   120             foreach ($this->children as $child) {
       
   121                 if (!$child->isRendered()) {
       
   122                     return false;
       
   123                 }
       
   124             }
       
   125 
       
   126             return $this->rendered = true;
       
   127         }
       
   128 
       
   129         return false;
       
   130     }
       
   131 
       
   132     /**
       
   133      * Marks the attached form as rendered
       
   134      *
       
   135      * @return FormView The current view
       
   136      */
       
   137     public function setRendered()
       
   138     {
       
   139         $this->rendered = true;
       
   140 
       
   141         return $this;
       
   142     }
       
   143 
       
   144     /**
       
   145      * Sets the parent view.
       
   146      *
       
   147      * @param FormView $parent The parent view
       
   148      *
       
   149      * @return FormView The current view
       
   150      */
       
   151     public function setParent(FormView $parent = null)
       
   152     {
       
   153         $this->parent = $parent;
       
   154 
       
   155         return $this;
       
   156     }
       
   157 
       
   158     /**
       
   159      * Returns the parent view.
       
   160      *
       
   161      * @return FormView The parent view
       
   162      */
       
   163     public function getParent()
       
   164     {
       
   165         return $this->parent;
       
   166     }
       
   167 
       
   168     /**
       
   169      * Returns whether this view has a parent.
       
   170      *
       
   171      * @return Boolean Whether this view has a parent
       
   172      */
       
   173     public function hasParent()
       
   174     {
       
   175         return null !== $this->parent;
       
   176     }
       
   177 
       
   178     /**
       
   179      * Sets the children view.
       
   180      *
       
   181      * @param array $children The children as instances of FormView
       
   182      *
       
   183      * @return FormView The current view
       
   184      */
       
   185     public function setChildren(array $children)
       
   186     {
       
   187         $this->children = $children;
       
   188 
       
   189         return $this;
       
   190     }
       
   191 
       
   192     /**
       
   193      * Returns the children.
       
   194      *
       
   195      * @return array The children as instances of FormView
       
   196      */
       
   197     public function getChildren()
       
   198     {
       
   199         return $this->children;
       
   200     }
       
   201 
       
   202     /**
       
   203      * Returns a given child.
       
   204      *
       
   205      * @param string name The name of the child
       
   206      *
       
   207      * @return FormView The child view
       
   208      */
       
   209     public function getChild($name)
       
   210     {
       
   211         return $this->children[$name];
       
   212     }
       
   213 
       
   214     /**
       
   215      * Returns whether this view has children.
       
   216      *
       
   217      * @return Boolean Whether this view has children
       
   218      */
       
   219     public function hasChildren()
       
   220     {
       
   221         return count($this->children) > 0;
       
   222     }
       
   223 
       
   224     /**
       
   225      * Returns a child by name (implements \ArrayAccess).
       
   226      *
       
   227      * @param string $name The child name
       
   228      *
       
   229      * @return FormView The child view
       
   230      */
       
   231     public function offsetGet($name)
       
   232     {
       
   233         return $this->getChild($name);
       
   234     }
       
   235 
       
   236     /**
       
   237      * Returns whether the given child exists (implements \ArrayAccess).
       
   238      *
       
   239      * @param string $name The child name
       
   240      *
       
   241      * @return Boolean Whether the child view exists
       
   242      */
       
   243     public function offsetExists($name)
       
   244     {
       
   245         return isset($this->children[$name]);
       
   246     }
       
   247 
       
   248     /**
       
   249      * Implements \ArrayAccess.
       
   250      *
       
   251      * @throws \BadMethodCallException always as setting a child by name is not allowed
       
   252      */
       
   253     public function offsetSet($name, $value)
       
   254     {
       
   255         throw new \BadMethodCallException('Not supported');
       
   256     }
       
   257 
       
   258     /**
       
   259      * Removes a child (implements \ArrayAccess).
       
   260      *
       
   261      * @param string $name The child name
       
   262      */
       
   263     public function offsetUnset($name)
       
   264     {
       
   265         unset($this->children[$name]);
       
   266     }
       
   267 
       
   268     /**
       
   269      * Returns an iterator to iterate over children (implements \IteratorAggregate)
       
   270      *
       
   271      * @return \ArrayIterator The iterator
       
   272      */
       
   273     public function getIterator()
       
   274     {
       
   275         return new \ArrayIterator($this->children);
       
   276     }
       
   277 
       
   278     /**
       
   279      * Implements \Countable.
       
   280      *
       
   281      * @return integer The number of children views
       
   282      */
       
   283     public function count()
       
   284     {
       
   285         return count($this->children);
       
   286     }
       
   287 }