ErrorElement.php 5.7 KB

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