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\IpAddressFieldInterface; |
||
| 11 | use EdmondsCommerce\DoctrineStaticMeta\MappingHelper; |
||
| 12 | use Symfony\Component\Validator\Constraints\Ip; |
||
| 13 | use Symfony\Component\Validator\Mapping\ClassMetadata as ValidatorClassMetaData; |
||
| 14 | |||
| 15 | // phpcs:enable |
||
| 16 | trait IpAddressFieldTrait |
||
| 17 | { |
||
| 18 | |||
| 19 | /** |
||
| 20 | * @var string|null |
||
| 21 | */ |
||
| 22 | private $ipAddress; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * @SuppressWarnings(PHPMD.StaticAccess) |
||
| 26 | * @param ClassMetadataBuilder $builder |
||
| 27 | * |
||
| 28 | * @see https://stackoverflow.com/a/1076755/543455 |
||
| 29 | */ |
||
| 30 | public static function metaForIpAddress(ClassMetadataBuilder $builder): void |
||
| 31 | { |
||
| 32 | $fieldBuilder = new FieldBuilder( |
||
| 33 | $builder, |
||
| 34 | [ |
||
| 35 | 'fieldName' => IpAddressFieldInterface::PROP_IP_ADDRESS, |
||
| 36 | 'type' => Type::STRING, |
||
| 37 | 'default' => IpAddressFieldInterface::DEFAULT_IP_ADDRESS, |
||
| 38 | ] |
||
| 39 | ); |
||
| 40 | $fieldBuilder |
||
| 41 | ->columnName(MappingHelper::getColumnNameForField(IpAddressFieldInterface::PROP_IP_ADDRESS)) |
||
| 42 | ->nullable(true) |
||
| 43 | ->unique(false) |
||
| 44 | ->length(45) |
||
| 45 | ->build(); |
||
| 46 | } |
||
| 47 | |||
| 48 | /** |
||
| 49 | * This method sets the validation for this field. |
||
| 50 | * |
||
| 51 | * You should add in as many relevant property constraints as you see fit. |
||
| 52 | * |
||
| 53 | * @see https://symfony.com/doc/current/validation.html#supported-constraints |
||
| 54 | * |
||
| 55 | * @param ValidatorClassMetaData $metadata |
||
| 56 | * |
||
| 57 | * @throws \Symfony\Component\Validator\Exception\MissingOptionsException |
||
| 58 | * @throws \Symfony\Component\Validator\Exception\InvalidOptionsException |
||
| 59 | * @throws \Symfony\Component\Validator\Exception\ConstraintDefinitionException |
||
| 60 | */ |
||
| 61 | protected static function validatorMetaForPropertyIpAddress(ValidatorClassMetaData $metadata): void |
||
| 62 | { |
||
| 63 | $metadata->addPropertyConstraint( |
||
| 64 | IpAddressFieldInterface::PROP_IP_ADDRESS, |
||
| 65 | new Ip(IpAddressFieldInterface::IP_ADDRESS_VALIDATION_OPTIONS) |
||
| 66 | ); |
||
| 67 | } |
||
| 68 | |||
| 69 | /** |
||
| 70 | * @return string|null |
||
| 71 | */ |
||
| 72 | public function getIpAddress(): ?string |
||
| 73 | { |
||
| 74 | if (null === $this->ipAddress) { |
||
| 75 | return IpAddressFieldInterface::DEFAULT_IP_ADDRESS; |
||
| 76 | } |
||
| 77 | |||
| 78 | return $this->ipAddress; |
||
| 79 | } |
||
| 80 | |||
| 81 | /** |
||
| 82 | * @param string|null $ipAddress |
||
| 83 | * |
||
| 84 | * @return self |
||
| 85 | */ |
||
| 86 | private function setIpAddress(?string $ipAddress): self |
||
| 87 | { |
||
| 88 | $this->updatePropertyValue( |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 89 | IpAddressFieldInterface::PROP_IP_ADDRESS, |
||
| 90 | $ipAddress |
||
| 91 | ); |
||
| 92 | |||
| 93 | return $this; |
||
| 94 | } |
||
| 95 | } |
||
| 96 |