vendor/symfony/validator/Constraints/Type.php line 23

Open in your IDE?
  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. /**
  13. * @Annotation
  14. * @Target({"PROPERTY", "METHOD", "ANNOTATION"})
  15. *
  16. * @author Bernhard Schussek <bschussek@gmail.com>
  17. */
  18. #[\Attribute(\Attribute::TARGET_PROPERTY | \Attribute::TARGET_METHOD | \Attribute::IS_REPEATABLE)]
  19. class Type extends Constraint
  20. {
  21. public const INVALID_TYPE_ERROR = 'ba785a8c-82cb-4283-967c-3cf342181b40';
  22. protected static $errorNames = [
  23. self::INVALID_TYPE_ERROR => 'INVALID_TYPE_ERROR',
  24. ];
  25. public $message = 'This value should be of type {{ type }}.';
  26. public $type;
  27. /**
  28. * {@inheritdoc}
  29. *
  30. * @param string|array $type One ore multiple types to validate against or a set of options
  31. */
  32. public function __construct($type, ?string $message = null, ?array $groups = null, $payload = null, array $options = [])
  33. {
  34. if (\is_array($type) && \is_string(key($type))) {
  35. $options = array_merge($type, $options);
  36. } elseif (null !== $type) {
  37. $options['value'] = $type;
  38. }
  39. parent::__construct($options, $groups, $payload);
  40. $this->message = $message ?? $this->message;
  41. }
  42. /**
  43. * {@inheritdoc}
  44. */
  45. public function getDefaultOption()
  46. {
  47. return 'type';
  48. }
  49. /**
  50. * {@inheritdoc}
  51. */
  52. public function getRequiredOptions()
  53. {
  54. return ['type'];
  55. }
  56. }