ErrorElement.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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 mixed $subject
  29. * @param \Symfony\Bundle\FrameworkBundle\Validator\ConstraintValidatorFactory $constraintValidatorFactory
  30. * @param \Symfony\Component\Validator\ExecutionContext $context
  31. * @param string $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. *
  45. * @param string $name
  46. * @param array $arguments
  47. *
  48. * @return ErrorElement
  49. */
  50. public function __call($name, array $arguments = array())
  51. {
  52. if (substr($name, 0, 6) == 'assert') {
  53. $this->validate($this->newConstraint(substr($name, 6), isset($arguments[0]) ? $arguments[0] : array()));
  54. } else {
  55. throw new \RunTimeException('Unable to recognize the command');
  56. }
  57. return $this;
  58. }
  59. /**
  60. * @param string $name
  61. * @param bool $key
  62. *
  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. *
  89. * @return void
  90. */
  91. protected function validate(Constraint $constraint, $messageTemplate = null, $messageParameters = array())
  92. {
  93. $validator = $this->constraintValidatorFactory->getInstance($constraint);
  94. $value = $this->getValue();
  95. $validator->isValid($value, $constraint);
  96. $this->context->setPropertyPath($this->getFullPropertyPath());
  97. $this->context->setGroup($this->group);
  98. $validator->initialize($this->context);
  99. if (!$validator->isValid($value, $constraint)) {
  100. $this->context->addViolation(
  101. $messageTemplate ?: $validator->getMessageTemplate(),
  102. array_merge($validator->getMessageParameters(), $messageParameters),
  103. $value
  104. );
  105. }
  106. }
  107. /**
  108. * @return string
  109. */
  110. public function getFullPropertyPath()
  111. {
  112. if ($this->getCurrentPropertyPath()) {
  113. return sprintf('%s.%s', $this->basePropertyPath, $this->getCurrentPropertyPath());
  114. } else {
  115. return $this->basePropertyPath;
  116. }
  117. }
  118. /**
  119. * Return the value linked to
  120. *
  121. * @return mixed
  122. */
  123. protected function getValue()
  124. {
  125. return $this->getCurrentPropertyPath()->getValue($this->subject);
  126. }
  127. /**
  128. * @return mixed
  129. */
  130. public function getSubject()
  131. {
  132. return $this->subject;
  133. }
  134. /**
  135. * @param string $name
  136. * @param array $options
  137. *
  138. * @return
  139. */
  140. protected function newConstraint($name, array $options = array())
  141. {
  142. if (strpos($name, '\\') !== false && class_exists($name)) {
  143. $className = (string)$name;
  144. } else {
  145. $className = 'Symfony\\Component\\Validator\\Constraints\\' . $name;
  146. }
  147. return new $className($options);
  148. }
  149. /**
  150. * @return null
  151. */
  152. protected function getCurrentPropertyPath()
  153. {
  154. if (!isset($this->propertyPaths[$this->current])) {
  155. return null; //global error
  156. }
  157. return $this->propertyPaths[$this->current];
  158. }
  159. /**
  160. * @param string|array $message
  161. * @param array $parameters
  162. * @param null $value
  163. *
  164. * @return ErrorElement
  165. */
  166. public function addViolation($message, $parameters = array(), $value = null)
  167. {
  168. $this->context->setPropertyPath($this->getFullPropertyPath());
  169. $this->context->setGroup($this->group);
  170. if (is_array($message)) {
  171. $value = isset($message[2]) ? $message[2] : $value;
  172. $parameters = isset($message[1]) ? (array)$message[1] : array();
  173. $message = isset($message[0]) ? $message[0] : 'error';
  174. }
  175. $this->context->addViolation($message, $parameters, $value);
  176. $this->errors[] = array($message, $parameters, $value);
  177. return $this;
  178. }
  179. /**
  180. * @return array
  181. */
  182. public function getErrors()
  183. {
  184. return $this->errors;
  185. }
  186. }