ErrorElement.php 4.0 KB

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