ErrorElement.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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\Component\Validator\ConstraintValidatorFactoryInterface;
  12. use Symfony\Component\Validator\ExecutionContextInterface;
  13. use Symfony\Component\PropertyAccess\PropertyAccess;
  14. use Symfony\Component\PropertyAccess\PropertyPath;
  15. use Symfony\Component\Validator\Constraint;
  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. * @return void
  107. */
  108. protected function validate(Constraint $constraint)
  109. {
  110. $subPath = (string) $this->getCurrentPropertyPath();
  111. $this->context->validateValue($this->getValue(), $constraint, $subPath, $this->group);
  112. }
  113. /**
  114. * @return string
  115. */
  116. public function getFullPropertyPath()
  117. {
  118. if ($this->getCurrentPropertyPath()) {
  119. return sprintf('%s.%s', $this->basePropertyPath, $this->getCurrentPropertyPath());
  120. } else {
  121. return $this->basePropertyPath;
  122. }
  123. }
  124. /**
  125. * Return the value linked to
  126. *
  127. * @return mixed
  128. */
  129. protected function getValue()
  130. {
  131. if ($this->current == '') {
  132. return $this->subject;
  133. }
  134. $propertyAccessor = PropertyAccess::createPropertyAccessor();
  135. return $propertyAccessor->getValue($this->subject, $this->getCurrentPropertyPath());
  136. }
  137. /**
  138. * @return mixed
  139. */
  140. public function getSubject()
  141. {
  142. return $this->subject;
  143. }
  144. /**
  145. * @param string $name
  146. * @param array $options
  147. *
  148. * @return
  149. */
  150. protected function newConstraint($name, array $options = array())
  151. {
  152. if (strpos($name, '\\') !== false && class_exists($name)) {
  153. $className = (string) $name;
  154. } else {
  155. $className = 'Symfony\\Component\\Validator\\Constraints\\' . $name;
  156. }
  157. return new $className($options);
  158. }
  159. /**
  160. * @return null|PropertyPath
  161. */
  162. protected function getCurrentPropertyPath()
  163. {
  164. if (!isset($this->propertyPaths[$this->current])) {
  165. return null; //global error
  166. }
  167. return $this->propertyPaths[$this->current];
  168. }
  169. /**
  170. * @param string|array $message
  171. * @param array $parameters
  172. * @param null $value
  173. *
  174. * @return ErrorElement
  175. */
  176. public function addViolation($message, $parameters = array(), $value = null)
  177. {
  178. if (is_array($message)) {
  179. $value = isset($message[2]) ? $message[2] : $value;
  180. $parameters = isset($message[1]) ? (array) $message[1] : array();
  181. $message = isset($message[0]) ? $message[0] : 'error';
  182. }
  183. $subPath = (string) $this->getCurrentPropertyPath();
  184. $this->context->addViolationAt($subPath, $message, $parameters, $value);
  185. $this->errors[] = array($message, $parameters, $value);
  186. return $this;
  187. }
  188. /**
  189. * @return array
  190. */
  191. public function getErrors()
  192. {
  193. return $this->errors;
  194. }
  195. }