edmondscommerce /
doctrine-static-meta
| 1 | <?php declare(strict_types=1); |
||
| 2 | |||
| 3 | namespace EdmondsCommerce\DoctrineStaticMeta\Entity\Fields\Traits\String; |
||
| 4 | |||
| 5 | // phpcs:disable |
||
| 6 | |||
| 7 | use Doctrine\DBAL\Types\Type; |
||
| 8 | use Doctrine\ORM\Mapping\Builder\ClassMetadataBuilder; |
||
| 9 | use Doctrine\ORM\Mapping\Builder\FieldBuilder; |
||
| 10 | use EdmondsCommerce\DoctrineStaticMeta\Entity\Fields\Interfaces\String\BusinessIdentifierCodeFieldInterface; |
||
| 11 | use EdmondsCommerce\DoctrineStaticMeta\Entity\Interfaces\ValidatedEntityInterface; |
||
| 12 | use EdmondsCommerce\DoctrineStaticMeta\MappingHelper; |
||
| 13 | use Symfony\Component\Validator\Constraints\Bic; |
||
| 14 | use Symfony\Component\Validator\Mapping\ClassMetadata as ValidatorClassMetaData; |
||
| 15 | |||
| 16 | // phpcs:enable |
||
| 17 | |||
| 18 | /** |
||
| 19 | * Trait BusinessIdentifierCodeFieldTrait |
||
| 20 | * |
||
| 21 | * A Business Identifier Code also known as SWIFT-BIC, BIC, SWIFT ID or SWIFT code |
||
| 22 | * |
||
| 23 | * @see https://en.wikipedia.org/wiki/ISO_9362 |
||
| 24 | * |
||
| 25 | * @package EdmondsCommerce\DoctrineStaticMeta\Entity\Fields\Traits\String |
||
| 26 | */ |
||
| 27 | trait BusinessIdentifierCodeFieldTrait |
||
| 28 | { |
||
| 29 | |||
| 30 | /** |
||
| 31 | * @var string|null |
||
| 32 | */ |
||
| 33 | private $businessIdentifierCode; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * @SuppressWarnings(PHPMD.StaticAccess) |
||
| 37 | */ |
||
| 38 | 1 | public static function metaForBusinessIdentifierCode(ClassMetadataBuilder $builder) |
|
| 39 | { |
||
| 40 | 1 | $fieldBuilder = new FieldBuilder( |
|
| 41 | 1 | $builder, |
|
| 42 | [ |
||
| 43 | 1 | 'fieldName' => BusinessIdentifierCodeFieldInterface::PROP_BUSINESS_IDENTIFIER_CODE, |
|
| 44 | 'type' => Type::STRING, |
||
| 45 | 'default' => BusinessIdentifierCodeFieldInterface::DEFAULT_BUSINESS_IDENTIFIER_CODE, |
||
| 46 | ] |
||
| 47 | ); |
||
| 48 | $fieldBuilder |
||
| 49 | 1 | ->columnName(MappingHelper::getColumnNameForField( |
|
| 50 | 1 | BusinessIdentifierCodeFieldInterface::PROP_BUSINESS_IDENTIFIER_CODE |
|
| 51 | )) |
||
| 52 | 1 | ->nullable(BusinessIdentifierCodeFieldInterface::DEFAULT_BUSINESS_IDENTIFIER_CODE === null) |
|
| 53 | 1 | ->unique(false) |
|
| 54 | 1 | ->length(20) |
|
| 55 | 1 | ->build(); |
|
| 56 | 1 | } |
|
| 57 | |||
| 58 | /** |
||
| 59 | * This method sets the validation for this field. |
||
| 60 | * |
||
| 61 | * You should add in as many relevant property constraints as you see fit. |
||
| 62 | * |
||
| 63 | * @see https://symfony.com/doc/current/validation.html#supported-constraints |
||
| 64 | * |
||
| 65 | * @param ValidatorClassMetaData $metadata |
||
| 66 | * |
||
| 67 | * @throws \Symfony\Component\Validator\Exception\MissingOptionsException |
||
| 68 | * @throws \Symfony\Component\Validator\Exception\InvalidOptionsException |
||
| 69 | * @throws \Symfony\Component\Validator\Exception\ConstraintDefinitionException |
||
| 70 | */ |
||
| 71 | 2 | protected static function validatorMetaForBusinessIdentifierCode(ValidatorClassMetaData $metadata) |
|
| 72 | { |
||
| 73 | 2 | $metadata->addPropertyConstraint( |
|
| 74 | 2 | BusinessIdentifierCodeFieldInterface::PROP_BUSINESS_IDENTIFIER_CODE, |
|
| 75 | 2 | new Bic() |
|
| 76 | ); |
||
| 77 | 2 | } |
|
| 78 | |||
| 79 | /** |
||
| 80 | * @return string|null |
||
| 81 | */ |
||
| 82 | 2 | public function getBusinessIdentifierCode(): ?string |
|
| 83 | { |
||
| 84 | 2 | if (null === $this->businessIdentifierCode) { |
|
| 85 | 1 | return BusinessIdentifierCodeFieldInterface::DEFAULT_BUSINESS_IDENTIFIER_CODE; |
|
| 86 | } |
||
| 87 | |||
| 88 | 2 | return $this->businessIdentifierCode; |
|
| 89 | } |
||
| 90 | |||
| 91 | /** |
||
| 92 | * @param string|null $businessIdentifierCode |
||
| 93 | * |
||
| 94 | * @return self |
||
| 95 | */ |
||
| 96 | 2 | public function setBusinessIdentifierCode(?string $businessIdentifierCode): self |
|
| 97 | { |
||
| 98 | 2 | $this->updatePropertyValueThenValidateAndNotify( |
|
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 99 | 2 | BusinessIdentifierCodeFieldInterface::PROP_BUSINESS_IDENTIFIER_CODE, |
|
| 100 | 2 | $businessIdentifierCode |
|
| 101 | ); |
||
| 102 | |||
| 103 | 2 | return $this; |
|
| 104 | } |
||
| 105 | } |
||
| 106 |