ConstraintViolationList.php 3.8 KB

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