vendor/symfony/form/FormErrorIterator.php line 31

Open in your IDE?
  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. use Symfony\Component\Form\Exception\InvalidArgumentException;
  13. use Symfony\Component\Form\Exception\OutOfBoundsException;
  14. use Symfony\Component\Validator\ConstraintViolation;
  15. /**
  16.  * Iterates over the errors of a form.
  17.  *
  18.  * This class supports recursive iteration. In order to iterate recursively,
  19.  * pass a structure of {@link FormError} and {@link FormErrorIterator} objects
  20.  * to the $errors constructor argument.
  21.  *
  22.  * You can also wrap the iterator into a {@link \RecursiveIteratorIterator} to
  23.  * flatten the recursive structure into a flat list of errors.
  24.  *
  25.  * @author Bernhard Schussek <bschussek@gmail.com>
  26.  */
  27. class FormErrorIterator implements \RecursiveIterator, \SeekableIterator, \ArrayAccess, \Countable
  28. {
  29.     /**
  30.      * The prefix used for indenting nested error messages.
  31.      */
  32.     public const INDENTATION '    ';
  33.     private $form;
  34.     private $errors;
  35.     /**
  36.      * @param FormError[]|self[] $errors An array of form errors and instances
  37.      *                                   of FormErrorIterator
  38.      *
  39.      * @throws InvalidArgumentException If the errors are invalid
  40.      */
  41.     public function __construct(FormInterface $form, array $errors)
  42.     {
  43.         foreach ($errors as $error) {
  44.             if (!($error instanceof FormError || $error instanceof self)) {
  45.                 throw new InvalidArgumentException(sprintf('The errors must be instances of "Symfony\Component\Form\FormError" or "%s". Got: "%s".'__CLASS__get_debug_type($error)));
  46.             }
  47.         }
  48.         $this->form $form;
  49.         $this->errors $errors;
  50.     }
  51.     /**
  52.      * Returns all iterated error messages as string.
  53.      *
  54.      * @return string The iterated error messages
  55.      */
  56.     public function __toString()
  57.     {
  58.         $string '';
  59.         foreach ($this->errors as $error) {
  60.             if ($error instanceof FormError) {
  61.                 $string .= 'ERROR: '.$error->getMessage()."\n";
  62.             } else {
  63.                 /* @var self $error */
  64.                 $string .= $error->form->getName().":\n";
  65.                 $string .= self::indent((string) $error);
  66.             }
  67.         }
  68.         return $string;
  69.     }
  70.     /**
  71.      * Returns the iterated form.
  72.      *
  73.      * @return FormInterface The form whose errors are iterated by this object
  74.      */
  75.     public function getForm()
  76.     {
  77.         return $this->form;
  78.     }
  79.     /**
  80.      * Returns the current element of the iterator.
  81.      *
  82.      * @return FormError|self An error or an iterator containing nested errors
  83.      */
  84.     public function current()
  85.     {
  86.         return current($this->errors);
  87.     }
  88.     /**
  89.      * Advances the iterator to the next position.
  90.      */
  91.     public function next()
  92.     {
  93.         next($this->errors);
  94.     }
  95.     /**
  96.      * Returns the current position of the iterator.
  97.      *
  98.      * @return int The 0-indexed position
  99.      */
  100.     public function key()
  101.     {
  102.         return key($this->errors);
  103.     }
  104.     /**
  105.      * Returns whether the iterator's position is valid.
  106.      *
  107.      * @return bool Whether the iterator is valid
  108.      */
  109.     public function valid()
  110.     {
  111.         return null !== key($this->errors);
  112.     }
  113.     /**
  114.      * Sets the iterator's position to the beginning.
  115.      *
  116.      * This method detects if errors have been added to the form since the
  117.      * construction of the iterator.
  118.      */
  119.     public function rewind()
  120.     {
  121.         reset($this->errors);
  122.     }
  123.     /**
  124.      * Returns whether a position exists in the iterator.
  125.      *
  126.      * @param int $position The position
  127.      *
  128.      * @return bool Whether that position exists
  129.      */
  130.     public function offsetExists($position)
  131.     {
  132.         return isset($this->errors[$position]);
  133.     }
  134.     /**
  135.      * Returns the element at a position in the iterator.
  136.      *
  137.      * @param int $position The position
  138.      *
  139.      * @return FormError|FormErrorIterator The element at the given position
  140.      *
  141.      * @throws OutOfBoundsException If the given position does not exist
  142.      */
  143.     public function offsetGet($position)
  144.     {
  145.         if (!isset($this->errors[$position])) {
  146.             throw new OutOfBoundsException('The offset '.$position.' does not exist.');
  147.         }
  148.         return $this->errors[$position];
  149.     }
  150.     /**
  151.      * Unsupported method.
  152.      *
  153.      * @return void
  154.      *
  155.      * @throws BadMethodCallException
  156.      */
  157.     public function offsetSet($position$value)
  158.     {
  159.         throw new BadMethodCallException('The iterator doesn\'t support modification of elements.');
  160.     }
  161.     /**
  162.      * Unsupported method.
  163.      *
  164.      * @return void
  165.      *
  166.      * @throws BadMethodCallException
  167.      */
  168.     public function offsetUnset($position)
  169.     {
  170.         throw new BadMethodCallException('The iterator doesn\'t support modification of elements.');
  171.     }
  172.     /**
  173.      * Returns whether the current element of the iterator can be recursed
  174.      * into.
  175.      *
  176.      * @return bool Whether the current element is an instance of this class
  177.      */
  178.     public function hasChildren()
  179.     {
  180.         return current($this->errors) instanceof self;
  181.     }
  182.     /**
  183.      * Alias of {@link current()}.
  184.      *
  185.      * @return self
  186.      */
  187.     public function getChildren()
  188.     {
  189.         return current($this->errors);
  190.     }
  191.     /**
  192.      * Returns the number of elements in the iterator.
  193.      *
  194.      * Note that this is not the total number of errors, if the constructor
  195.      * parameter $deep was set to true! In that case, you should wrap the
  196.      * iterator into a {@link \RecursiveIteratorIterator} with the standard mode
  197.      * {@link \RecursiveIteratorIterator::LEAVES_ONLY} and count the result.
  198.      *
  199.      *     $iterator = new \RecursiveIteratorIterator($form->getErrors(true));
  200.      *     $count = count(iterator_to_array($iterator));
  201.      *
  202.      * Alternatively, set the constructor argument $flatten to true as well.
  203.      *
  204.      *     $count = count($form->getErrors(true, true));
  205.      *
  206.      * @return int The number of iterated elements
  207.      */
  208.     public function count()
  209.     {
  210.         return \count($this->errors);
  211.     }
  212.     /**
  213.      * Sets the position of the iterator.
  214.      *
  215.      * @param int $position The new position
  216.      *
  217.      * @return void
  218.      *
  219.      * @throws OutOfBoundsException If the position is invalid
  220.      */
  221.     public function seek($position)
  222.     {
  223.         if (!isset($this->errors[$position])) {
  224.             throw new OutOfBoundsException('The offset '.$position.' does not exist.');
  225.         }
  226.         reset($this->errors);
  227.         while ($position !== key($this->errors)) {
  228.             next($this->errors);
  229.         }
  230.     }
  231.     /**
  232.      * Creates iterator for errors with specific codes.
  233.      *
  234.      * @param string|string[] $codes The codes to find
  235.      *
  236.      * @return static new instance which contains only specific errors
  237.      */
  238.     public function findByCodes($codes)
  239.     {
  240.         $codes = (array) $codes;
  241.         $errors = [];
  242.         foreach ($this as $error) {
  243.             $cause $error->getCause();
  244.             if ($cause instanceof ConstraintViolation && \in_array($cause->getCode(), $codestrue)) {
  245.                 $errors[] = $error;
  246.             }
  247.         }
  248.         return new static($this->form$errors);
  249.     }
  250.     /**
  251.      * Utility function for indenting multi-line strings.
  252.      */
  253.     private static function indent(string $string): string
  254.     {
  255.         return rtrim(self::INDENTATION.str_replace("\n""\n".self::INDENTATION$string), ' ');
  256.     }
  257. }