4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\Validator;
15 * Default implementation of {@ConstraintViolationListInterface}.
17 * @author Bernhard Schussek <bschussek@gmail.com>
19 class ConstraintViolationList implements \IteratorAggregate, ConstraintViolationListInterface
22 * @var ConstraintViolationInterface[]
24 private $violations = array();
27 * Creates a new constraint violation list.
29 * @param ConstraintViolationInterface[] $violations The constraint violations to add to the list
31 public function __construct(array $violations = array())
33 foreach ($violations as $violation) {
34 $this->add($violation);
39 * Converts the violation into a string for debugging purposes.
41 * @return string The violation as string
43 public function __toString()
47 foreach ($this->violations as $violation) {
48 $string .= $violation."\n";
57 public function add(ConstraintViolationInterface $violation)
59 $this->violations[] = $violation;
65 public function addAll(ConstraintViolationListInterface $otherList)
67 foreach ($otherList as $violation) {
68 $this->violations[] = $violation;
75 public function get($offset)
77 if (!isset($this->violations[$offset])) {
78 throw new \OutOfBoundsException(sprintf('The offset "%s" does not exist.', $offset));
81 return $this->violations[$offset];
87 public function has($offset)
89 return isset($this->violations[$offset]);
95 public function set($offset, ConstraintViolationInterface $violation)
97 $this->violations[$offset] = $violation;
103 public function remove($offset)
105 unset($this->violations[$offset]);
111 * @return \ArrayIterator|ConstraintViolationInterface[]
113 public function getIterator()
115 return new \ArrayIterator($this->violations);
121 public function count()
123 return count($this->violations);
129 public function offsetExists($offset)
131 return $this->has($offset);
137 public function offsetGet($offset)
139 return $this->get($offset);
145 public function offsetSet($offset, $violation)
147 if (null === $offset) {
148 $this->add($violation);
150 $this->set($offset, $violation);
157 public function offsetUnset($offset)
159 $this->remove($offset);