ConstraintViolationList.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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 = array();
  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 = array())
  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($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($offset)
  76. {
  77. return isset($this->violations[$offset]);
  78. }
  79. /**
  80. * {@inheritdoc}
  81. */
  82. public function set($offset, ConstraintViolationInterface $violation)
  83. {
  84. $this->violations[$offset] = $violation;
  85. }
  86. /**
  87. * {@inheritdoc}
  88. */
  89. public function remove($offset)
  90. {
  91. unset($this->violations[$offset]);
  92. }
  93. /**
  94. * {@inheritdoc}
  95. */
  96. public function getIterator()
  97. {
  98. return new \ArrayIterator($this->violations);
  99. }
  100. /**
  101. * {@inheritdoc}
  102. */
  103. public function count()
  104. {
  105. return count($this->violations);
  106. }
  107. /**
  108. * {@inheritdoc}
  109. */
  110. public function offsetExists($offset)
  111. {
  112. return $this->has($offset);
  113. }
  114. /**
  115. * {@inheritdoc}
  116. */
  117. public function offsetGet($offset)
  118. {
  119. return $this->get($offset);
  120. }
  121. /**
  122. * {@inheritdoc}
  123. */
  124. public function offsetSet($offset, $violation)
  125. {
  126. if (null === $offset) {
  127. $this->add($violation);
  128. } else {
  129. $this->set($offset, $violation);
  130. }
  131. }
  132. /**
  133. * {@inheritdoc}
  134. */
  135. public function offsetUnset($offset)
  136. {
  137. $this->remove($offset);
  138. }
  139. }