Updated to Drupal 8.5. Core Media not yet in use.
[yaffs-website] / vendor / symfony / validator / ConstraintViolationList.php
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\Validator;
13
14 /**
15  * Default implementation of {@ConstraintViolationListInterface}.
16  *
17  * @author Bernhard Schussek <bschussek@gmail.com>
18  */
19 class ConstraintViolationList implements \IteratorAggregate, ConstraintViolationListInterface
20 {
21     /**
22      * @var ConstraintViolationInterface[]
23      */
24     private $violations = array();
25
26     /**
27      * Creates a new constraint violation list.
28      *
29      * @param ConstraintViolationInterface[] $violations The constraint violations to add to the list
30      */
31     public function __construct(array $violations = array())
32     {
33         foreach ($violations as $violation) {
34             $this->add($violation);
35         }
36     }
37
38     /**
39      * Converts the violation into a string for debugging purposes.
40      *
41      * @return string The violation as string
42      */
43     public function __toString()
44     {
45         $string = '';
46
47         foreach ($this->violations as $violation) {
48             $string .= $violation."\n";
49         }
50
51         return $string;
52     }
53
54     /**
55      * {@inheritdoc}
56      */
57     public function add(ConstraintViolationInterface $violation)
58     {
59         $this->violations[] = $violation;
60     }
61
62     /**
63      * {@inheritdoc}
64      */
65     public function addAll(ConstraintViolationListInterface $otherList)
66     {
67         foreach ($otherList as $violation) {
68             $this->violations[] = $violation;
69         }
70     }
71
72     /**
73      * {@inheritdoc}
74      */
75     public function get($offset)
76     {
77         if (!isset($this->violations[$offset])) {
78             throw new \OutOfBoundsException(sprintf('The offset "%s" does not exist.', $offset));
79         }
80
81         return $this->violations[$offset];
82     }
83
84     /**
85      * {@inheritdoc}
86      */
87     public function has($offset)
88     {
89         return isset($this->violations[$offset]);
90     }
91
92     /**
93      * {@inheritdoc}
94      */
95     public function set($offset, ConstraintViolationInterface $violation)
96     {
97         $this->violations[$offset] = $violation;
98     }
99
100     /**
101      * {@inheritdoc}
102      */
103     public function remove($offset)
104     {
105         unset($this->violations[$offset]);
106     }
107
108     /**
109      * {@inheritdoc}
110      *
111      * @return \ArrayIterator|ConstraintViolationInterface[]
112      */
113     public function getIterator()
114     {
115         return new \ArrayIterator($this->violations);
116     }
117
118     /**
119      * {@inheritdoc}
120      */
121     public function count()
122     {
123         return count($this->violations);
124     }
125
126     /**
127      * {@inheritdoc}
128      */
129     public function offsetExists($offset)
130     {
131         return $this->has($offset);
132     }
133
134     /**
135      * {@inheritdoc}
136      */
137     public function offsetGet($offset)
138     {
139         return $this->get($offset);
140     }
141
142     /**
143      * {@inheritdoc}
144      */
145     public function offsetSet($offset, $violation)
146     {
147         if (null === $offset) {
148             $this->add($violation);
149         } else {
150             $this->set($offset, $violation);
151         }
152     }
153
154     /**
155      * {@inheritdoc}
156      */
157     public function offsetUnset($offset)
158     {
159         $this->remove($offset);
160     }
161
162     /**
163      * Creates iterator for errors with specific codes.
164      *
165      * @param string|string[] $codes The codes to find
166      *
167      * @return static new instance which contains only specific errors
168      */
169     public function findByCodes($codes)
170     {
171         $codes = (array) $codes;
172         $violations = array();
173         foreach ($this as $violation) {
174             if (in_array($violation->getCode(), $codes, true)) {
175                 $violations[] = $violation;
176             }
177         }
178
179         return new static($violations);
180     }
181 }