vendor/elao/form-translation-bundle/Model/FormTree.php line 21

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the ElaoFormTranslation bundle.
  4.  *
  5.  * Copyright (C) Elao
  6.  *
  7.  * @author Elao <contact@elao.com>
  8.  */
  9. namespace Elao\Bundle\FormTranslationBundle\Model;
  10. /**
  11.  * A form Tree
  12.  *
  13.  * @author Thomas Jarrand <thomas.jarrand@gmail.com>
  14.  *
  15.  * @implements \Iterator<FormTreeNode>
  16.  * @implements \ArrayAccess<int,FormTreeNode>
  17.  */
  18. class FormTree implements \Iterator\Countable\ArrayAccess
  19. {
  20.     /**
  21.      * The FormTreeNode elements
  22.      *
  23.      * @var array<FormTreeNode>
  24.      */
  25.     private array $nodes;
  26.     /**
  27.      * Current position in the loop
  28.      */
  29.     private int $position 0;
  30.     /**
  31.      * @param array<FormTreeNode> $nodes
  32.      */
  33.     public function __construct(array $nodes = [])
  34.     {
  35.         $this->nodes $nodes;
  36.     }
  37.     /**
  38.      * Add a parent node to the beginning of the tree
  39.      *
  40.      * @param FormTreeNode $node The node
  41.      *
  42.      * @return int The new number of elements in the Tree
  43.      */
  44.     public function addParent(FormTreeNode $node): int
  45.     {
  46.         return array_unshift($this->nodes$node);
  47.     }
  48.     /**
  49.      * Add a child node to the end of the tree
  50.      *
  51.      * @param FormTreeNode $node The node
  52.      *
  53.      * @return int The new number of elements in the Tree
  54.      */
  55.     public function addChild(FormTreeNode $node): int
  56.     {
  57.         return array_push($this->nodes$node);
  58.     }
  59.     /**
  60.      * Set the loop back to the start
  61.      */
  62.     #[\ReturnTypeWillChange]
  63.     public function rewind(): void
  64.     {
  65.         $this->position 0;
  66.     }
  67.     /**
  68.      * Return the length of the tree
  69.      */
  70.     public function count(): int
  71.     {
  72.         return \count($this->nodes);
  73.     }
  74.     /**
  75.      * Return the current Node in the loop
  76.      */
  77.     #[\ReturnTypeWillChange]
  78.     public function current(): FormTreeNode
  79.     {
  80.         return $this->nodes[$this->position];
  81.     }
  82.     /**
  83.      * Return the current position in the loop
  84.      */
  85.     #[\ReturnTypeWillChange]
  86.     public function key(): int
  87.     {
  88.         return $this->position;
  89.     }
  90.     /**
  91.      * Increment current position
  92.      */
  93.     #[\ReturnTypeWillChange]
  94.     public function next(): void
  95.     {
  96.         ++$this->position;
  97.     }
  98.     /**
  99.      * Return whether or not the current position is valid
  100.      */
  101.     #[\ReturnTypeWillChange]
  102.     public function valid(): bool
  103.     {
  104.         return $this->offsetExists($this->position);
  105.     }
  106.     /**
  107.      * Return whether or not the given offset exists
  108.      */
  109.     #[\ReturnTypeWillChange]
  110.     public function offsetExists($offset): bool
  111.     {
  112.         return isset($this->nodes[$offset]);
  113.     }
  114.     /**
  115.      * Get the node at the given offset
  116.      */
  117.     #[\ReturnTypeWillChange]
  118.     public function offsetGet($offset): ?FormTreeNode
  119.     {
  120.         return $this->offsetExists($offset) ? $this->nodes[$offset] : null;
  121.     }
  122.     /**
  123.      * Set the node at the given offset
  124.      */
  125.     #[\ReturnTypeWillChange]
  126.     public function offsetSet($offset$value): void
  127.     {
  128.         /* Not implemented: Use addParent and addChild methods */
  129.     }
  130.     /**
  131.      * Unset node at the given offset
  132.      */
  133.     #[\ReturnTypeWillChange]
  134.     public function offsetUnset($offset): void
  135.     {
  136.         /* Not implemented: FormTree nodes should not be unsetable */
  137.     }
  138. }