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\MappingHelper; |
12
|
|
|
use Symfony\Component\Validator\Constraints\Bic; |
13
|
|
|
use Symfony\Component\Validator\Constraints\Length; |
14
|
|
|
use Symfony\Component\Validator\Exception\ConstraintDefinitionException; |
15
|
|
|
use Symfony\Component\Validator\Exception\InvalidOptionsException; |
16
|
|
|
use Symfony\Component\Validator\Exception\MissingOptionsException; |
17
|
|
|
use Symfony\Component\Validator\Mapping\ClassMetadata as ValidatorClassMetaData; |
18
|
|
|
|
19
|
|
|
// phpcs:enable |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Trait BusinessIdentifierCodeFieldTrait |
23
|
|
|
* |
24
|
|
|
* A Business Identifier Code also known as SWIFT-BIC, BIC, SWIFT ID or SWIFT code |
25
|
|
|
* |
26
|
|
|
* @see https://en.wikipedia.org/wiki/ISO_9362 |
27
|
|
|
* |
28
|
|
|
* @package EdmondsCommerce\DoctrineStaticMeta\Entity\Fields\Traits\String |
29
|
|
|
*/ |
30
|
|
|
trait BusinessIdentifierCodeFieldTrait |
31
|
|
|
{ |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var string|null |
35
|
|
|
*/ |
36
|
|
|
private $businessIdentifierCode; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @SuppressWarnings(PHPMD.StaticAccess) |
40
|
|
|
* @param ClassMetadataBuilder $builder |
41
|
|
|
*/ |
42
|
|
|
public static function metaForBusinessIdentifierCode(ClassMetadataBuilder $builder): void |
43
|
|
|
{ |
44
|
|
|
$fieldBuilder = new FieldBuilder( |
45
|
|
|
$builder, |
46
|
|
|
[ |
47
|
|
|
'fieldName' => BusinessIdentifierCodeFieldInterface::PROP_BUSINESS_IDENTIFIER_CODE, |
48
|
|
|
'type' => Type::STRING, |
49
|
|
|
'default' => BusinessIdentifierCodeFieldInterface::DEFAULT_BUSINESS_IDENTIFIER_CODE, |
50
|
|
|
] |
51
|
|
|
); |
52
|
|
|
$fieldBuilder |
53
|
|
|
->columnName(MappingHelper::getColumnNameForField( |
54
|
|
|
BusinessIdentifierCodeFieldInterface::PROP_BUSINESS_IDENTIFIER_CODE |
55
|
|
|
)) |
56
|
|
|
->nullable() |
57
|
|
|
->unique(false) |
58
|
|
|
->length(20) |
59
|
|
|
->build(); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* This method sets the validation for this field. |
64
|
|
|
* |
65
|
|
|
* You should add in as many relevant property constraints as you see fit. |
66
|
|
|
* |
67
|
|
|
* @see https://symfony.com/doc/current/validation.html#supported-constraints |
68
|
|
|
* |
69
|
|
|
* @param ValidatorClassMetaData $metadata |
70
|
|
|
* |
71
|
|
|
* @throws MissingOptionsException |
72
|
|
|
* @throws InvalidOptionsException |
73
|
|
|
* @throws ConstraintDefinitionException |
74
|
|
|
*/ |
75
|
|
|
protected static function validatorMetaForPropertyBusinessIdentifierCode(ValidatorClassMetaData $metadata): void |
76
|
|
|
{ |
77
|
|
|
$metadata->addPropertyConstraints( |
78
|
|
|
BusinessIdentifierCodeFieldInterface::PROP_BUSINESS_IDENTIFIER_CODE, |
79
|
|
|
[ |
80
|
|
|
new Bic(), |
81
|
|
|
new Length( |
82
|
|
|
[ |
83
|
|
|
'min' => 0, |
84
|
|
|
'max' => 20, |
85
|
|
|
] |
86
|
|
|
), |
87
|
|
|
] |
88
|
|
|
); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @return string|null |
93
|
|
|
*/ |
94
|
|
|
public function getBusinessIdentifierCode(): ?string |
95
|
|
|
{ |
96
|
|
|
if (null === $this->businessIdentifierCode) { |
97
|
|
|
return BusinessIdentifierCodeFieldInterface::DEFAULT_BUSINESS_IDENTIFIER_CODE; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
return $this->businessIdentifierCode; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @param string|null $businessIdentifierCode |
105
|
|
|
* |
106
|
|
|
* @return self |
107
|
|
|
*/ |
108
|
|
|
private function setBusinessIdentifierCode(?string $businessIdentifierCode): self |
109
|
|
|
{ |
110
|
|
|
$this->updatePropertyValue( |
|
|
|
|
111
|
|
|
BusinessIdentifierCodeFieldInterface::PROP_BUSINESS_IDENTIFIER_CODE, |
112
|
|
|
$businessIdentifierCode |
113
|
|
|
); |
114
|
|
|
|
115
|
|
|
return $this; |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|