|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
|
|
3
|
|
|
|
|
4
|
|
|
namespace EdmondsCommerce\DoctrineStaticMeta\Entity\Fields\Traits\Attribute; |
|
5
|
|
|
|
|
6
|
|
|
use Doctrine\DBAL\Types\Type; |
|
7
|
|
|
use Doctrine\ORM\Mapping\Builder\ClassMetadataBuilder; |
|
8
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Entity\Fields\Interfaces\Attribute\IpAddressFieldInterface; |
|
9
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Entity\Interfaces\ValidatedEntityInterface; |
|
10
|
|
|
use Symfony\Component\Validator\Constraints\Ip; |
|
11
|
|
|
use Symfony\Component\Validator\Mapping\ClassMetadata as ValidatorClassMetaData; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Any valid IP address including version 4 and 6 |
|
15
|
|
|
* |
|
16
|
|
|
* Trait IpAddressFieldTrait |
|
17
|
|
|
* |
|
18
|
|
|
* @package EdmondsCommerce\DoctrineStaticMeta\Entity\Fields\Traits\Attribute |
|
19
|
|
|
*/ |
|
20
|
|
|
trait IpAddressFieldTrait |
|
21
|
|
|
{ |
|
22
|
|
|
|
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @var string |
|
26
|
|
|
*/ |
|
27
|
|
|
private $ipAddress; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @see https://stackoverflow.com/a/1076755/543455 |
|
31
|
|
|
* |
|
32
|
|
|
* @param ClassMetadataBuilder $builder |
|
33
|
|
|
*/ |
|
34
|
|
|
protected static function metaForIpAddress(ClassMetadataBuilder $builder): void |
|
35
|
|
|
{ |
|
36
|
|
|
$builder->createField(IpAddressFieldInterface::PROP_IP_ADDRESS, Type::STRING) |
|
37
|
|
|
->length(45) |
|
38
|
|
|
->nullable(true) |
|
39
|
|
|
->build(); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @see https://symfony.com/doc/current/reference/constraints/Ip.html |
|
44
|
|
|
* |
|
45
|
|
|
* @param ValidatorClassMetaData $metadata |
|
46
|
|
|
* |
|
47
|
|
|
* @throws \Symfony\Component\Validator\Exception\MissingOptionsException |
|
48
|
|
|
* @throws \Symfony\Component\Validator\Exception\InvalidOptionsException |
|
49
|
|
|
* @throws \Symfony\Component\Validator\Exception\ConstraintDefinitionException |
|
50
|
|
|
*/ |
|
51
|
|
|
protected static function validatorMetaForIpAddress(ValidatorClassMetaData $metadata): void |
|
52
|
|
|
{ |
|
53
|
|
|
$metadata->addPropertyConstraint( |
|
54
|
|
|
IpAddressFieldInterface::PROP_IP_ADDRESS, |
|
55
|
|
|
new Ip(static::IP_ADDRESS_VALIDATION_OPTIONS) |
|
|
|
|
|
|
56
|
|
|
); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Get ipAddress |
|
61
|
|
|
* |
|
62
|
|
|
* @return string |
|
63
|
|
|
*/ |
|
64
|
|
|
public function getIpAddress(): ?string |
|
65
|
|
|
{ |
|
66
|
|
|
return $this->ipAddress; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Set ipAddress |
|
71
|
|
|
* |
|
72
|
|
|
* @param string $ipAddress |
|
73
|
|
|
* |
|
74
|
|
|
* @return $this |
|
75
|
|
|
*/ |
|
76
|
|
|
public function setIpAddress(?string $ipAddress): self |
|
77
|
|
|
{ |
|
78
|
|
|
$this->ipAddress = $ipAddress; |
|
79
|
|
|
if ($this instanceof ValidatedEntityInterface) { |
|
80
|
|
|
$this->validateProperty(IpAddressFieldInterface::PROP_IP_ADDRESS); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
return $this; |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
|