ErrorElement.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. <?php
  2. /*
  3. * This file is part of the Sonata package.
  4. *
  5. * (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
  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 Sonata\AdminBundle\Validator;
  11. use Symfony\Bundle\FrameworkBundle\Validator\ConstraintValidatorFactory;
  12. use Symfony\Component\Validator\ExecutionContext;
  13. use Symfony\Component\Form\Util\PropertyPath;
  14. use Symfony\Component\Validator\Constraint;
  15. class ErrorElement
  16. {
  17. protected $context;
  18. protected $group;
  19. protected $constraintValidatorFactory;
  20. protected $stack = array();
  21. protected $propertyPaths = array();
  22. protected $subject;
  23. protected $current;
  24. protected $basePropertyPath;
  25. protected $errors = array();
  26. /**
  27. * @param mixed $subject
  28. * @param \Symfony\Bundle\FrameworkBundle\Validator\ConstraintValidatorFactory $constraintValidatorFactory
  29. * @param \Symfony\Component\Validator\ExecutionContext $context
  30. * @param string $group
  31. */
  32. public function __construct($subject, ConstraintValidatorFactory $constraintValidatorFactory, ExecutionContext $context, $group)
  33. {
  34. $this->subject = $subject;
  35. $this->context = $context;
  36. $this->group = $group;
  37. $this->constraintValidatorFactory = $constraintValidatorFactory;
  38. $this->current = '';
  39. $this->basePropertyPath = $this->context->getPropertyPath();
  40. }
  41. /**
  42. * @throws \RunTimeException
  43. *
  44. * @param string $name
  45. * @param array $arguments
  46. *
  47. * @return ErrorElement
  48. */
  49. public function __call($name, array $arguments = array())
  50. {
  51. if (substr($name, 0, 6) == 'assert') {
  52. $this->validate($this->newConstraint(substr($name, 6), isset($arguments[0]) ? $arguments[0] : array()));
  53. } else {
  54. throw new \RunTimeException('Unable to recognize the command');
  55. }
  56. return $this;
  57. }
  58. /**
  59. * @param Constraint $constraint
  60. *
  61. * @return ErrorElement
  62. */
  63. public function addConstraint(Constraint $constraint)
  64. {
  65. $this->validate($constraint);
  66. return $this;
  67. }
  68. /**
  69. * @param string $name
  70. * @param bool $key
  71. *
  72. * @return ErrorElement
  73. */
  74. public function with($name, $key = false)
  75. {
  76. $key = $key ? $name . '.' . $key : $name;
  77. $this->stack[] = $key;
  78. $this->current = implode('.', $this->stack);
  79. if (!isset($this->propertyPaths[$this->current])) {
  80. $this->propertyPaths[$this->current] = new PropertyPath($this->current);
  81. }
  82. return $this;
  83. }
  84. /**
  85. * @return ErrorElement
  86. */
  87. public function end()
  88. {
  89. array_pop($this->stack);
  90. $this->current = implode('.', $this->stack);
  91. return $this;
  92. }
  93. /**
  94. * @param \Symfony\Component\Validator\Constraint $constraint
  95. *
  96. * @return void
  97. */
  98. protected function validate(Constraint $constraint)
  99. {
  100. $subPath = (string) $this->getCurrentPropertyPath();
  101. $this->context->validateValue($this->getValue(), $constraint, $subPath, $this->group);
  102. }
  103. /**
  104. * @return string
  105. */
  106. public function getFullPropertyPath()
  107. {
  108. if ($this->getCurrentPropertyPath()) {
  109. return sprintf('%s.%s', $this->basePropertyPath, $this->getCurrentPropertyPath());
  110. } else {
  111. return $this->basePropertyPath;
  112. }
  113. }
  114. /**
  115. * Return the value linked to
  116. *
  117. * @return mixed
  118. */
  119. protected function getValue()
  120. {
  121. return $this->getCurrentPropertyPath()->getValue($this->subject);
  122. }
  123. /**
  124. * @return mixed
  125. */
  126. public function getSubject()
  127. {
  128. return $this->subject;
  129. }
  130. /**
  131. * @param string $name
  132. * @param array $options
  133. *
  134. * @return
  135. */
  136. protected function newConstraint($name, array $options = array())
  137. {
  138. if (strpos($name, '\\') !== false && class_exists($name)) {
  139. $className = (string) $name;
  140. } else {
  141. $className = 'Symfony\\Component\\Validator\\Constraints\\' . $name;
  142. }
  143. return new $className($options);
  144. }
  145. /**
  146. * @return null|PropertyPath
  147. */
  148. protected function getCurrentPropertyPath()
  149. {
  150. if (!isset($this->propertyPaths[$this->current])) {
  151. return null; //global error
  152. }
  153. return $this->propertyPaths[$this->current];
  154. }
  155. /**
  156. * @param string|array $message
  157. * @param array $parameters
  158. * @param null $value
  159. *
  160. * @return ErrorElement
  161. */
  162. public function addViolation($message, $parameters = array(), $value = null)
  163. {
  164. if (is_array($message)) {
  165. $value = isset($message[2]) ? $message[2] : $value;
  166. $parameters = isset($message[1]) ? (array) $message[1] : array();
  167. $message = isset($message[0]) ? $message[0] : 'error';
  168. }
  169. $subPath = (string) $this->getCurrentPropertyPath();
  170. $this->context->addViolationAt($subPath, $message, $parameters, $value);
  171. $this->errors[] = array($message, $parameters, $value);
  172. return $this;
  173. }
  174. /**
  175. * @return array
  176. */
  177. public function getErrors()
  178. {
  179. return $this->errors;
  180. }
  181. }