1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace EdmondsCommerce\DoctrineStaticMeta\Entity\Fields\Traits\String; |
4
|
|
|
|
5
|
|
|
use Doctrine\DBAL\Types\Type; |
6
|
|
|
use Doctrine\ORM\Mapping\Builder\ClassMetadataBuilder; |
7
|
|
|
use Doctrine\ORM\Mapping\Builder\FieldBuilder; |
8
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Entity\Fields\Interfaces\String\CountryCodeFieldInterface; |
9
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\MappingHelper; |
10
|
|
|
use Symfony\Component\Validator\Constraints\Country; |
11
|
|
|
use Symfony\Component\Validator\Mapping\ClassMetadata as ValidatorClassMetaData; |
12
|
|
|
|
13
|
|
|
trait CountryCodeFieldTrait |
14
|
|
|
{ |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @var string|null |
18
|
|
|
*/ |
19
|
|
|
private $countryCode; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @SuppressWarnings(PHPMD.StaticAccess) |
23
|
|
|
*/ |
24
|
|
|
public static function metaForCountryCode(ClassMetadataBuilder $builder): void |
25
|
|
|
{ |
26
|
|
|
$fieldBuilder = new FieldBuilder( |
27
|
|
|
$builder, |
28
|
|
|
[ |
29
|
|
|
'fieldName' => CountryCodeFieldInterface::PROP_COUNTRY_CODE, |
30
|
|
|
'type' => Type::STRING, |
31
|
|
|
'default' => CountryCodeFieldInterface::DEFAULT_COUNTRY_CODE, |
32
|
|
|
] |
33
|
|
|
); |
34
|
|
|
$fieldBuilder |
35
|
|
|
->columnName(MappingHelper::getColumnNameForField(CountryCodeFieldInterface::PROP_COUNTRY_CODE)) |
36
|
|
|
->nullable(true) |
37
|
|
|
->unique(false) |
38
|
|
|
->length(6) |
39
|
|
|
->build(); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* This method sets the validation for this field. |
44
|
|
|
* |
45
|
|
|
* You should add in as many relevant property constraints as you see fit. |
46
|
|
|
* |
47
|
|
|
* @see https://symfony.com/doc/current/validation.html#supported-constraints |
48
|
|
|
* |
49
|
|
|
* @param ValidatorClassMetaData $metadata |
50
|
|
|
* |
51
|
|
|
* @throws \Symfony\Component\Validator\Exception\MissingOptionsException |
52
|
|
|
* @throws \Symfony\Component\Validator\Exception\InvalidOptionsException |
53
|
|
|
* @throws \Symfony\Component\Validator\Exception\ConstraintDefinitionException |
54
|
|
|
*/ |
55
|
|
|
protected static function validatorMetaForPropertyCountryCode(ValidatorClassMetaData $metadata): void |
56
|
|
|
{ |
57
|
|
|
$metadata->addPropertyConstraint( |
58
|
|
|
CountryCodeFieldInterface::PROP_COUNTRY_CODE, |
59
|
|
|
new Country() |
60
|
|
|
); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @return string|null |
65
|
|
|
*/ |
66
|
|
|
public function getCountryCode(): ?string |
67
|
|
|
{ |
68
|
|
|
if (null === $this->countryCode) { |
69
|
|
|
return CountryCodeFieldInterface::DEFAULT_COUNTRY_CODE; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
return $this->countryCode; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @param string|null $countryCode |
77
|
|
|
* |
78
|
|
|
* @return self |
79
|
|
|
*/ |
80
|
|
|
private function setCountryCode(?string $countryCode): self |
81
|
|
|
{ |
82
|
|
|
$this->updatePropertyValue( |
|
|
|
|
83
|
|
|
CountryCodeFieldInterface::PROP_COUNTRY_CODE, |
84
|
|
|
$countryCode |
85
|
|
|
); |
86
|
|
|
|
87
|
|
|
return $this; |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|