ErrorElement.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. class ErrorElement
  16. {
  17. protected $context;
  18. protected $group;
  19. protected $constraintValidatorFactory;
  20. protected $stack = array();
  21. protected $propertyPaths = array();
  22. protected $subject;
  23. protected $current;
  24. protected $basePropertyPath;
  25. public function __construct($subject, ConstraintValidatorFactory $constraintValidatorFactory, ExecutionContext $context, $group)
  26. {
  27. $this->subject = $subject;
  28. $this->context = $context;
  29. $this->group = $group;
  30. $this->constraintValidatorFactory = $constraintValidatorFactory;
  31. $this->current = '';
  32. $this->basePropertyPath = $this->context->getPropertyPath();
  33. }
  34. public function __call($name, array $arguments = array())
  35. {
  36. if (substr($name, 0, 6) == 'assert') {
  37. $this->validate($this->newConstraint(
  38. substr($name, 6),
  39. isset($arguments[0]) ? $arguments[0] : array()
  40. ));
  41. } else {
  42. throw new \RunTimeException('Unable to recognize the command');
  43. }
  44. return $this;
  45. }
  46. public function with($name, $key = false)
  47. {
  48. $key = $key ? $name.'.'.$key : $name;
  49. $this->stack[] = $key;
  50. $this->current = implode('.', $this->stack);
  51. if (!isset($this->propertyPaths[$this->current])) {
  52. $this->propertyPaths[$this->current] = new PropertyPath($this->current);
  53. }
  54. return $this;
  55. }
  56. public function end()
  57. {
  58. array_pop($this->stack);
  59. $this->current = implode('.', $this->stack);
  60. return $this;
  61. }
  62. protected function validate($constraint, $messageTemplate = null, $messageParameters = array())
  63. {
  64. $validator = $this->constraintValidatorFactory->getInstance($constraint);
  65. $value = $this->getValue();
  66. $validator->isValid($value, $constraint);
  67. $this->context->setPropertyPath($this->getFullPropertyPath());
  68. $this->context->setGroup($this->group);
  69. $validator->initialize($this->context);
  70. if (!$validator->isValid($value, $constraint)) {
  71. $this->context->addViolation(
  72. $messageTemplate ?: $validator->getMessageTemplate(),
  73. array_merge($validator->getMessageParameters(), $messageParameters),
  74. $value
  75. );
  76. }
  77. }
  78. public function getFullPropertyPath()
  79. {
  80. if ($this->getCurrentPropertyPath()) {
  81. return sprintf('%s.%s', $this->basePropertyPath, $this->getCurrentPropertyPath());
  82. } else {
  83. return $this->basePropertyPath;
  84. }
  85. }
  86. /**
  87. * Return the value linked to
  88. *
  89. * @return mixed
  90. */
  91. protected function getValue()
  92. {
  93. return $this->getCurrentPropertyPath()->getValue($this->subject);
  94. }
  95. public function getSubject()
  96. {
  97. return $this->subject;
  98. }
  99. protected function newConstraint($name, $options)
  100. {
  101. if (strpos($name, '\\') !== false && class_exists($name)) {
  102. $className = (string) $name;
  103. } else {
  104. $className = 'Symfony\\Component\\Validator\\Constraints\\'.$name;
  105. }
  106. return new $className($options);
  107. }
  108. protected function getCurrentPropertyPath()
  109. {
  110. if (!isset($this->propertyPaths[$this->current])) {
  111. return null; //global error
  112. }
  113. return $this->propertyPaths[$this->current];
  114. }
  115. public function addViolation($message, $parameters = array(), $value = null)
  116. {
  117. $this->context->setPropertyPath($this->getFullPropertyPath());
  118. $this->context->setGroup($this->group);
  119. if (!is_array($message)) {
  120. $this->context->addViolation($message, $parameters, $value);
  121. } else {
  122. $this->context->addViolation(
  123. isset($error[0]) ? $error[0] : 'error',
  124. isset($error[1]) ? (array)$error[1] : array(),
  125. isset($error[2]) ? $error[2] : $value
  126. );
  127. }
  128. return $this;
  129. }
  130. }