CardScheme.php 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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. * Metadata for the CardSchemeValidator.
  14. *
  15. * @Annotation
  16. * @Target({"PROPERTY", "METHOD", "ANNOTATION"})
  17. *
  18. * @author Tim Nagel <t.nagel@infinite.net.au>
  19. * @author Bernhard Schussek <bschussek@gmail.com>
  20. */
  21. #[\Attribute(\Attribute::TARGET_PROPERTY | \Attribute::TARGET_METHOD | \Attribute::IS_REPEATABLE)]
  22. class CardScheme extends Constraint
  23. {
  24. public const AMEX = 'AMEX';
  25. public const CHINA_UNIONPAY = 'CHINA_UNIONPAY';
  26. public const DINERS = 'DINERS';
  27. public const DISCOVER = 'DISCOVER';
  28. public const INSTAPAYMENT = 'INSTAPAYMENT';
  29. public const JCB = 'JCB';
  30. public const LASER = 'LASER';
  31. public const MAESTRO = 'MAESTRO';
  32. public const MASTERCARD = 'MASTERCARD';
  33. public const MIR = 'MIR';
  34. public const UATP = 'UATP';
  35. public const VISA = 'VISA';
  36. public const NOT_NUMERIC_ERROR = 'a2ad9231-e827-485f-8a1e-ef4d9a6d5c2e';
  37. public const INVALID_FORMAT_ERROR = 'a8faedbf-1c2f-4695-8d22-55783be8efed';
  38. protected static $errorNames = [
  39. self::NOT_NUMERIC_ERROR => 'NOT_NUMERIC_ERROR',
  40. self::INVALID_FORMAT_ERROR => 'INVALID_FORMAT_ERROR',
  41. ];
  42. public $message = 'Unsupported card type or invalid card number.';
  43. public $schemes;
  44. /**
  45. * {@inheritdoc}
  46. *
  47. * @param array|string $schemes The schemes to validate against or a set of options
  48. */
  49. public function __construct($schemes, string $message = null, array $groups = null, $payload = null, array $options = [])
  50. {
  51. if (\is_array($schemes) && \is_string(key($schemes))) {
  52. $options = array_merge($schemes, $options);
  53. } else {
  54. $options['value'] = $schemes;
  55. }
  56. parent::__construct($options, $groups, $payload);
  57. $this->message = $message ?? $this->message;
  58. }
  59. public function getDefaultOption()
  60. {
  61. return 'schemes';
  62. }
  63. public function getRequiredOptions()
  64. {
  65. return ['schemes'];
  66. }
  67. }