vendor/symfony/form/FormView.php line 22

  1. <?php
  2. /*
  3.  * This file is part of the Symfony package.
  4.  *
  5.  * (c) Fabien Potencier <fabien@symfony.com>
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. namespace Symfony\Component\Form;
  11. use Symfony\Component\Form\Exception\BadMethodCallException;
  12. /**
  13.  * @author Bernhard Schussek <bschussek@gmail.com>
  14.  *
  15.  * @implements \ArrayAccess<int|string, FormView>
  16.  * @implements \IteratorAggregate<int|string, FormView>
  17.  */
  18. class FormView implements \ArrayAccess\IteratorAggregate\Countable
  19. {
  20.     /**
  21.      * The variables assigned to this view.
  22.      */
  23.     public $vars = [
  24.         'value' => null,
  25.         'attr' => [],
  26.     ];
  27.     /**
  28.      * The parent view.
  29.      */
  30.     public $parent;
  31.     /**
  32.      * The child views.
  33.      *
  34.      * @var array<int|string, FormView>
  35.      */
  36.     public $children = [];
  37.     /**
  38.      * Is the form attached to this renderer rendered?
  39.      *
  40.      * Rendering happens when either the widget or the row method was called.
  41.      * Row implicitly includes widget, however certain rendering mechanisms
  42.      * have to skip widget rendering when a row is rendered.
  43.      */
  44.     private bool $rendered false;
  45.     private bool $methodRendered false;
  46.     public function __construct(self $parent null)
  47.     {
  48.         $this->parent $parent;
  49.     }
  50.     /**
  51.      * Returns whether the view was already rendered.
  52.      */
  53.     public function isRendered(): bool
  54.     {
  55.         if (true === $this->rendered || === \count($this->children)) {
  56.             return $this->rendered;
  57.         }
  58.         foreach ($this->children as $child) {
  59.             if (!$child->isRendered()) {
  60.                 return false;
  61.             }
  62.         }
  63.         return $this->rendered true;
  64.     }
  65.     /**
  66.      * Marks the view as rendered.
  67.      *
  68.      * @return $this
  69.      */
  70.     public function setRendered(): static
  71.     {
  72.         $this->rendered true;
  73.         return $this;
  74.     }
  75.     public function isMethodRendered(): bool
  76.     {
  77.         return $this->methodRendered;
  78.     }
  79.     /**
  80.      * @return void
  81.      */
  82.     public function setMethodRendered()
  83.     {
  84.         $this->methodRendered true;
  85.     }
  86.     /**
  87.      * Returns a child by name (implements \ArrayAccess).
  88.      *
  89.      * @param int|string $name The child name
  90.      */
  91.     public function offsetGet(mixed $name): self
  92.     {
  93.         return $this->children[$name];
  94.     }
  95.     /**
  96.      * Returns whether the given child exists (implements \ArrayAccess).
  97.      *
  98.      * @param int|string $name The child name
  99.      */
  100.     public function offsetExists(mixed $name): bool
  101.     {
  102.         return isset($this->children[$name]);
  103.     }
  104.     /**
  105.      * Implements \ArrayAccess.
  106.      *
  107.      * @throws BadMethodCallException always as setting a child by name is not allowed
  108.      */
  109.     public function offsetSet(mixed $namemixed $value): void
  110.     {
  111.         throw new BadMethodCallException('Not supported.');
  112.     }
  113.     /**
  114.      * Removes a child (implements \ArrayAccess).
  115.      *
  116.      * @param int|string $name The child name
  117.      */
  118.     public function offsetUnset(mixed $name): void
  119.     {
  120.         unset($this->children[$name]);
  121.     }
  122.     /**
  123.      * Returns an iterator to iterate over children (implements \IteratorAggregate).
  124.      *
  125.      * @return \ArrayIterator<int|string, FormView>
  126.      */
  127.     public function getIterator(): \ArrayIterator
  128.     {
  129.         return new \ArrayIterator($this->children);
  130.     }
  131.     public function count(): int
  132.     {
  133.         return \count($this->children);
  134.     }
  135. }