Isbn.php 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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 The Whole Life To Learn <thewholelifetolearn@gmail.com>
  17. * @author Manuel Reinhard <manu@sprain.ch>
  18. * @author Bernhard Schussek <bschussek@gmail.com>
  19. */
  20. #[\Attribute(\Attribute::TARGET_PROPERTY | \Attribute::TARGET_METHOD | \Attribute::IS_REPEATABLE)]
  21. class Isbn extends Constraint
  22. {
  23. public const ISBN_10 = 'isbn10';
  24. public const ISBN_13 = 'isbn13';
  25. public const TOO_SHORT_ERROR = '949acbb0-8ef5-43ed-a0e9-032dfd08ae45';
  26. public const TOO_LONG_ERROR = '3171387d-f80a-47b3-bd6e-60598545316a';
  27. public const INVALID_CHARACTERS_ERROR = '23d21cea-da99-453d-98b1-a7d916fbb339';
  28. public const CHECKSUM_FAILED_ERROR = '2881c032-660f-46b6-8153-d352d9706640';
  29. public const TYPE_NOT_RECOGNIZED_ERROR = 'fa54a457-f042-441f-89c4-066ee5bdd3e1';
  30. protected static $errorNames = [
  31. self::TOO_SHORT_ERROR => 'TOO_SHORT_ERROR',
  32. self::TOO_LONG_ERROR => 'TOO_LONG_ERROR',
  33. self::INVALID_CHARACTERS_ERROR => 'INVALID_CHARACTERS_ERROR',
  34. self::CHECKSUM_FAILED_ERROR => 'CHECKSUM_FAILED_ERROR',
  35. self::TYPE_NOT_RECOGNIZED_ERROR => 'TYPE_NOT_RECOGNIZED_ERROR',
  36. ];
  37. public $isbn10Message = 'This value is not a valid ISBN-10.';
  38. public $isbn13Message = 'This value is not a valid ISBN-13.';
  39. public $bothIsbnMessage = 'This value is neither a valid ISBN-10 nor a valid ISBN-13.';
  40. public $type;
  41. public $message;
  42. /**
  43. * {@inheritdoc}
  44. *
  45. * @param string|array|null $type The ISBN standard to validate or a set of options
  46. */
  47. public function __construct(
  48. $type = null,
  49. string $message = null,
  50. string $isbn10Message = null,
  51. string $isbn13Message = null,
  52. string $bothIsbnMessage = null,
  53. array $groups = null,
  54. $payload = null,
  55. array $options = []
  56. ) {
  57. if (\is_array($type)) {
  58. $options = array_merge($type, $options);
  59. } elseif (null !== $type) {
  60. $options['value'] = $type;
  61. }
  62. parent::__construct($options, $groups, $payload);
  63. $this->message = $message ?? $this->message;
  64. $this->isbn10Message = $isbn10Message ?? $this->isbn10Message;
  65. $this->isbn13Message = $isbn13Message ?? $this->isbn13Message;
  66. $this->bothIsbnMessage = $bothIsbnMessage ?? $this->bothIsbnMessage;
  67. }
  68. /**
  69. * {@inheritdoc}
  70. */
  71. public function getDefaultOption()
  72. {
  73. return 'type';
  74. }
  75. }