Bic.php 3.6 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\Intl\Countries;
  12. use Symfony\Component\PropertyAccess\PropertyAccess;
  13. use Symfony\Component\PropertyAccess\PropertyPathInterface;
  14. use Symfony\Component\Validator\Constraint;
  15. use Symfony\Component\Validator\Exception\ConstraintDefinitionException;
  16. use Symfony\Component\Validator\Exception\LogicException;
  17. /**
  18. * @Annotation
  19. * @Target({"PROPERTY", "METHOD", "ANNOTATION"})
  20. *
  21. * @author Michael Hirschler <michael.vhirsch@gmail.com>
  22. */
  23. #[\Attribute(\Attribute::TARGET_PROPERTY | \Attribute::TARGET_METHOD | \Attribute::IS_REPEATABLE)]
  24. class Bic extends Constraint
  25. {
  26. public const INVALID_LENGTH_ERROR = '66dad313-af0b-4214-8566-6c799be9789c';
  27. public const INVALID_CHARACTERS_ERROR = 'f424c529-7add-4417-8f2d-4b656e4833e2';
  28. public const INVALID_BANK_CODE_ERROR = '00559357-6170-4f29-aebd-d19330aa19cf';
  29. public const INVALID_COUNTRY_CODE_ERROR = '1ce76f8d-3c1f-451c-9e62-fe9c3ed486ae';
  30. public const INVALID_CASE_ERROR = '11884038-3312-4ae5-9d04-699f782130c7';
  31. public const INVALID_IBAN_COUNTRY_CODE_ERROR = '29a2c3bb-587b-4996-b6f5-53081364cea5';
  32. protected static $errorNames = [
  33. self::INVALID_LENGTH_ERROR => 'INVALID_LENGTH_ERROR',
  34. self::INVALID_CHARACTERS_ERROR => 'INVALID_CHARACTERS_ERROR',
  35. self::INVALID_BANK_CODE_ERROR => 'INVALID_BANK_CODE_ERROR',
  36. self::INVALID_COUNTRY_CODE_ERROR => 'INVALID_COUNTRY_CODE_ERROR',
  37. self::INVALID_CASE_ERROR => 'INVALID_CASE_ERROR',
  38. ];
  39. public $message = 'This is not a valid Business Identifier Code (BIC).';
  40. public $ibanMessage = 'This Business Identifier Code (BIC) is not associated with IBAN {{ iban }}.';
  41. public $iban;
  42. public $ibanPropertyPath;
  43. /**
  44. * {@inheritdoc}
  45. *
  46. * @param string|PropertyPathInterface|null $ibanPropertyPath
  47. */
  48. public function __construct(array $options = null, string $message = null, string $iban = null, $ibanPropertyPath = null, string $ibanMessage = null, array $groups = null, $payload = null)
  49. {
  50. if (!class_exists(Countries::class)) {
  51. throw new LogicException('The Intl component is required to use the Bic constraint. Try running "composer require symfony/intl".');
  52. }
  53. if (null !== $ibanPropertyPath && !\is_string($ibanPropertyPath) && !$ibanPropertyPath instanceof PropertyPathInterface) {
  54. throw new \TypeError(sprintf('"%s": Expected argument $ibanPropertyPath to be either null, a string or an instance of "%s", got "%s".', __METHOD__, PropertyPathInterface::class, get_debug_type($ibanPropertyPath)));
  55. }
  56. parent::__construct($options, $groups, $payload);
  57. $this->message = $message ?? $this->message;
  58. $this->ibanMessage = $ibanMessage ?? $this->ibanMessage;
  59. $this->iban = $iban ?? $this->iban;
  60. $this->ibanPropertyPath = $ibanPropertyPath ?? $this->ibanPropertyPath;
  61. if (null !== $this->iban && null !== $this->ibanPropertyPath) {
  62. throw new ConstraintDefinitionException('The "iban" and "ibanPropertyPath" options of the Iban constraint cannot be used at the same time.');
  63. }
  64. if (null !== $this->ibanPropertyPath && !class_exists(PropertyAccess::class)) {
  65. throw new LogicException(sprintf('The "symfony/property-access" component is required to use the "%s" constraint with the "ibanPropertyPath" option.', self::class));
  66. }
  67. }
  68. }