RecursiveValidator.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  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 Symfony\Component\Validator\Validator;
  11. use Symfony\Component\Validator\ConstraintValidatorFactoryInterface;
  12. use Symfony\Component\Validator\Context\ExecutionContextFactoryInterface;
  13. use Symfony\Component\Validator\Context\ExecutionContextInterface;
  14. use Symfony\Component\Validator\Mapping\Factory\MetadataFactoryInterface;
  15. use Symfony\Component\Validator\ObjectInitializerInterface;
  16. /**
  17. * Recursive implementation of {@link ValidatorInterface}.
  18. *
  19. * @author Bernhard Schussek <bschussek@gmail.com>
  20. */
  21. class RecursiveValidator implements ValidatorInterface
  22. {
  23. protected $contextFactory;
  24. protected $metadataFactory;
  25. protected $validatorFactory;
  26. protected $objectInitializers;
  27. /**
  28. * Creates a new validator.
  29. *
  30. * @param ObjectInitializerInterface[] $objectInitializers The object initializers
  31. */
  32. public function __construct(ExecutionContextFactoryInterface $contextFactory, MetadataFactoryInterface $metadataFactory, ConstraintValidatorFactoryInterface $validatorFactory, array $objectInitializers = [])
  33. {
  34. $this->contextFactory = $contextFactory;
  35. $this->metadataFactory = $metadataFactory;
  36. $this->validatorFactory = $validatorFactory;
  37. $this->objectInitializers = $objectInitializers;
  38. }
  39. /**
  40. * {@inheritdoc}
  41. */
  42. public function startContext($root = null)
  43. {
  44. return new RecursiveContextualValidator(
  45. $this->contextFactory->createContext($this, $root),
  46. $this->metadataFactory,
  47. $this->validatorFactory,
  48. $this->objectInitializers
  49. );
  50. }
  51. /**
  52. * {@inheritdoc}
  53. */
  54. public function inContext(ExecutionContextInterface $context)
  55. {
  56. return new RecursiveContextualValidator(
  57. $context,
  58. $this->metadataFactory,
  59. $this->validatorFactory,
  60. $this->objectInitializers
  61. );
  62. }
  63. /**
  64. * {@inheritdoc}
  65. */
  66. public function getMetadataFor($object)
  67. {
  68. return $this->metadataFactory->getMetadataFor($object);
  69. }
  70. /**
  71. * {@inheritdoc}
  72. */
  73. public function hasMetadataFor($object)
  74. {
  75. return $this->metadataFactory->hasMetadataFor($object);
  76. }
  77. /**
  78. * {@inheritdoc}
  79. */
  80. public function validate($value, $constraints = null, $groups = null)
  81. {
  82. return $this->startContext($value)
  83. ->validate($value, $constraints, $groups)
  84. ->getViolations();
  85. }
  86. /**
  87. * {@inheritdoc}
  88. */
  89. public function validateProperty($object, string $propertyName, $groups = null)
  90. {
  91. return $this->startContext($object)
  92. ->validateProperty($object, $propertyName, $groups)
  93. ->getViolations();
  94. }
  95. /**
  96. * {@inheritdoc}
  97. */
  98. public function validatePropertyValue($objectOrClass, string $propertyName, $value, $groups = null)
  99. {
  100. // If a class name is passed, take $value as root
  101. return $this->startContext(\is_object($objectOrClass) ? $objectOrClass : $value)
  102. ->validatePropertyValue($objectOrClass, $propertyName, $value, $groups)
  103. ->getViolations();
  104. }
  105. }