DateTime.php 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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 DateTime extends Constraint
  20. {
  21. public const INVALID_FORMAT_ERROR = '1a9da513-2640-4f84-9b6a-4d99dcddc628';
  22. public const INVALID_DATE_ERROR = 'd52afa47-620d-4d99-9f08-f4d85b36e33c';
  23. public const INVALID_TIME_ERROR = '5e797c9d-74f7-4098-baa3-94390c447b27';
  24. protected static $errorNames = [
  25. self::INVALID_FORMAT_ERROR => 'INVALID_FORMAT_ERROR',
  26. self::INVALID_DATE_ERROR => 'INVALID_DATE_ERROR',
  27. self::INVALID_TIME_ERROR => 'INVALID_TIME_ERROR',
  28. ];
  29. public $format = 'Y-m-d H:i:s';
  30. public $message = 'This value is not a valid datetime.';
  31. /**
  32. * {@inheritdoc}
  33. *
  34. * @param string|array|null $format
  35. */
  36. public function __construct($format = null, string $message = null, array $groups = null, $payload = null, array $options = [])
  37. {
  38. if (\is_array($format)) {
  39. $options = array_merge($format, $options);
  40. } elseif (null !== $format) {
  41. $options['value'] = $format;
  42. }
  43. parent::__construct($options, $groups, $payload);
  44. $this->message = $message ?? $this->message;
  45. }
  46. public function getDefaultOption()
  47. {
  48. return 'format';
  49. }
  50. }