ErrorElement.php 5.7 KB

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