Expression.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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\ExpressionLanguage\Expression as ExpressionObject;
  12. use Symfony\Component\ExpressionLanguage\ExpressionLanguage;
  13. use Symfony\Component\Validator\Constraint;
  14. use Symfony\Component\Validator\Exception\LogicException;
  15. /**
  16. * @Annotation
  17. * @Target({"CLASS", "PROPERTY", "METHOD", "ANNOTATION"})
  18. *
  19. * @author Fabien Potencier <fabien@symfony.com>
  20. * @author Bernhard Schussek <bschussek@gmail.com>
  21. */
  22. #[\Attribute(\Attribute::TARGET_PROPERTY | \Attribute::TARGET_METHOD | \Attribute::TARGET_CLASS | \Attribute::IS_REPEATABLE)]
  23. class Expression extends Constraint
  24. {
  25. public const EXPRESSION_FAILED_ERROR = '6b3befbc-2f01-4ddf-be21-b57898905284';
  26. protected static $errorNames = [
  27. self::EXPRESSION_FAILED_ERROR => 'EXPRESSION_FAILED_ERROR',
  28. ];
  29. public $message = 'This value is not valid.';
  30. public $expression;
  31. public $values = [];
  32. /**
  33. * {@inheritdoc}
  34. *
  35. * @param string|ExpressionObject|array $expression The expression to evaluate or an array of options
  36. */
  37. public function __construct(
  38. $expression,
  39. string $message = null,
  40. array $values = null,
  41. array $groups = null,
  42. $payload = null,
  43. array $options = []
  44. ) {
  45. if (!class_exists(ExpressionLanguage::class)) {
  46. throw new LogicException(sprintf('The "symfony/expression-language" component is required to use the "%s" constraint.', __CLASS__));
  47. }
  48. if (\is_array($expression)) {
  49. $options = array_merge($expression, $options);
  50. } elseif (!\is_string($expression) && !$expression instanceof ExpressionObject) {
  51. throw new \TypeError(sprintf('"%s": Expected argument $expression to be either a string, an instance of "%s" or an array, got "%s".', __METHOD__, ExpressionObject::class, get_debug_type($expression)));
  52. } else {
  53. $options['value'] = $expression;
  54. }
  55. parent::__construct($options, $groups, $payload);
  56. $this->message = $message ?? $this->message;
  57. $this->values = $values ?? $this->values;
  58. }
  59. /**
  60. * {@inheritdoc}
  61. */
  62. public function getDefaultOption()
  63. {
  64. return 'expression';
  65. }
  66. /**
  67. * {@inheritdoc}
  68. */
  69. public function getRequiredOptions()
  70. {
  71. return ['expression'];
  72. }
  73. /**
  74. * {@inheritdoc}
  75. */
  76. public function getTargets()
  77. {
  78. return [self::CLASS_CONSTRAINT, self::PROPERTY_CONSTRAINT];
  79. }
  80. /**
  81. * {@inheritdoc}
  82. */
  83. public function validatedBy()
  84. {
  85. return 'validator.expression';
  86. }
  87. }