ErrorElement.php 5.7 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\ExecutionContext;
  13. use Symfony\Component\PropertyAccess\PropertyAccess;
  14. use Symfony\Component\PropertyAccess\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 Constraint $constraint
  61. *
  62. * @return ErrorElement
  63. */
  64. public function addConstraint(Constraint $constraint)
  65. {
  66. $this->validate($constraint);
  67. return $this;
  68. }
  69. /**
  70. * @param string $name
  71. * @param bool $key
  72. *
  73. * @return ErrorElement
  74. */
  75. public function with($name, $key = false)
  76. {
  77. $key = $key ? $name . '.' . $key : $name;
  78. $this->stack[] = $key;
  79. $this->current = implode('.', $this->stack);
  80. if (!isset($this->propertyPaths[$this->current])) {
  81. $this->propertyPaths[$this->current] = new PropertyPath($this->current);
  82. }
  83. return $this;
  84. }
  85. /**
  86. * @return ErrorElement
  87. */
  88. public function end()
  89. {
  90. array_pop($this->stack);
  91. $this->current = implode('.', $this->stack);
  92. return $this;
  93. }
  94. /**
  95. * @param \Symfony\Component\Validator\Constraint $constraint
  96. *
  97. * @return void
  98. */
  99. protected function validate(Constraint $constraint)
  100. {
  101. $subPath = (string) $this->getCurrentPropertyPath();
  102. $this->context->validateValue($this->getValue(), $constraint, $subPath, $this->group);
  103. }
  104. /**
  105. * @return string
  106. */
  107. public function getFullPropertyPath()
  108. {
  109. if ($this->getCurrentPropertyPath()) {
  110. return sprintf('%s.%s', $this->basePropertyPath, $this->getCurrentPropertyPath());
  111. } else {
  112. return $this->basePropertyPath;
  113. }
  114. }
  115. /**
  116. * Return the value linked to
  117. *
  118. * @return mixed
  119. */
  120. protected function getValue()
  121. {
  122. if ($this->current == '') {
  123. return $this->subject;
  124. }
  125. $propertyAccessor = PropertyAccess::createPropertyAccessor();
  126. return $propertyAccessor->getValue($this->subject, $this->getCurrentPropertyPath());
  127. }
  128. /**
  129. * @return mixed
  130. */
  131. public function getSubject()
  132. {
  133. return $this->subject;
  134. }
  135. /**
  136. * @param string $name
  137. * @param array $options
  138. *
  139. * @return
  140. */
  141. protected function newConstraint($name, array $options = array())
  142. {
  143. if (strpos($name, '\\') !== false && class_exists($name)) {
  144. $className = (string) $name;
  145. } else {
  146. $className = 'Symfony\\Component\\Validator\\Constraints\\' . $name;
  147. }
  148. return new $className($options);
  149. }
  150. /**
  151. * @return null|PropertyPath
  152. */
  153. protected function getCurrentPropertyPath()
  154. {
  155. if (!isset($this->propertyPaths[$this->current])) {
  156. return null; //global error
  157. }
  158. return $this->propertyPaths[$this->current];
  159. }
  160. /**
  161. * @param string|array $message
  162. * @param array $parameters
  163. * @param null $value
  164. *
  165. * @return ErrorElement
  166. */
  167. public function addViolation($message, $parameters = array(), $value = null)
  168. {
  169. if (is_array($message)) {
  170. $value = isset($message[2]) ? $message[2] : $value;
  171. $parameters = isset($message[1]) ? (array) $message[1] : array();
  172. $message = isset($message[0]) ? $message[0] : 'error';
  173. }
  174. $subPath = (string) $this->getCurrentPropertyPath();
  175. $this->context->addViolationAt($subPath, $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. }