Compound.php 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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\Constraints;
  11. use Symfony\Component\Validator\Constraint;
  12. use Symfony\Component\Validator\Exception\ConstraintDefinitionException;
  13. /**
  14. * Extend this class to create a reusable set of constraints.
  15. *
  16. * @author Maxime Steinhausser <maxime.steinhausser@gmail.com>
  17. */
  18. abstract class Compound extends Composite
  19. {
  20. /** @var Constraint[] */
  21. public $constraints = [];
  22. public function __construct($options = null)
  23. {
  24. if (isset($options[$this->getCompositeOption()])) {
  25. throw new ConstraintDefinitionException(sprintf('You can\'t redefine the "%s" option. Use the "%s::getConstraints()" method instead.', $this->getCompositeOption(), __CLASS__));
  26. }
  27. $this->constraints = $this->getConstraints($this->normalizeOptions($options));
  28. parent::__construct($options);
  29. }
  30. final protected function getCompositeOption()
  31. {
  32. return 'constraints';
  33. }
  34. final public function validatedBy()
  35. {
  36. return CompoundValidator::class;
  37. }
  38. /**
  39. * @return Constraint[]
  40. */
  41. abstract protected function getConstraints(array $options): array;
  42. }