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\ORM\Mapping\Builder\ClassMetadataBuilder; |
||
| 8 | use EdmondsCommerce\DoctrineStaticMeta\Entity\Fields\Interfaces\String\UrlFieldInterface; |
||
| 9 | use EdmondsCommerce\DoctrineStaticMeta\Entity\Interfaces\ValidatedEntityInterface; |
||
| 10 | use EdmondsCommerce\DoctrineStaticMeta\MappingHelper; |
||
| 11 | use Symfony\Component\Validator\Constraints\Url; |
||
| 12 | use Symfony\Component\Validator\Mapping\ClassMetadata as ValidatorClassMetaData; |
||
| 13 | |||
| 14 | // phpcs:enable |
||
| 15 | trait UrlFieldTrait |
||
| 16 | { |
||
| 17 | |||
| 18 | /** |
||
| 19 | * @var string|null |
||
| 20 | */ |
||
| 21 | private $url; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * @SuppressWarnings(PHPMD.StaticAccess) |
||
| 25 | * @param ClassMetadataBuilder $builder |
||
| 26 | */ |
||
| 27 | 1 | public static function metaForUrl(ClassMetadataBuilder $builder): void |
|
| 28 | { |
||
| 29 | 1 | MappingHelper::setSimpleStringFields( |
|
| 30 | 1 | [UrlFieldInterface::PROP_URL], |
|
| 31 | 1 | $builder, |
|
| 32 | 1 | UrlFieldInterface::DEFAULT_URL, |
|
| 33 | 1 | false |
|
| 34 | ); |
||
| 35 | 1 | } |
|
| 36 | |||
| 37 | /** |
||
| 38 | * This method sets the validation for this field. |
||
| 39 | * |
||
| 40 | * You should add in as many relevant property constraints as you see fit. |
||
| 41 | * |
||
| 42 | * Remove the PHPMD suppressed warning once you start setting constraints |
||
| 43 | * |
||
| 44 | * @see https://symfony.com/doc/current/validation.html#supported-constraints |
||
| 45 | * |
||
| 46 | * @param ValidatorClassMetaData $metadata |
||
| 47 | * |
||
| 48 | * @throws \Symfony\Component\Validator\Exception\MissingOptionsException |
||
| 49 | * @throws \Symfony\Component\Validator\Exception\InvalidOptionsException |
||
| 50 | * @throws \Symfony\Component\Validator\Exception\ConstraintDefinitionException |
||
| 51 | */ |
||
| 52 | 2 | protected static function validatorMetaForUrl(ValidatorClassMetaData $metadata) |
|
| 53 | { |
||
| 54 | 2 | $metadata->addPropertyConstraint( |
|
| 55 | 2 | UrlFieldInterface::PROP_URL, |
|
| 56 | 2 | new Url() |
|
| 57 | ); |
||
| 58 | 2 | } |
|
| 59 | |||
| 60 | /** |
||
| 61 | * @return string|null |
||
| 62 | */ |
||
| 63 | 2 | public function getUrl(): ?string |
|
| 64 | { |
||
| 65 | 2 | if (null === $this->url) { |
|
| 66 | 1 | return UrlFieldInterface::DEFAULT_URL; |
|
| 67 | } |
||
| 68 | |||
| 69 | 2 | return $this->url; |
|
| 70 | } |
||
| 71 | |||
| 72 | /** |
||
| 73 | * @param string|null $url |
||
| 74 | * |
||
| 75 | * @return self |
||
| 76 | */ |
||
| 77 | 2 | public function setUrl(?string $url): self |
|
| 78 | { |
||
| 79 | 2 | $this->updatePropertyValueThenValidateAndNotify( |
|
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 80 | 2 | UrlFieldInterface::PROP_URL, |
|
| 81 | 2 | $url |
|
| 82 | ); |
||
| 83 | |||
| 84 | 2 | return $this; |
|
| 85 | } |
||
| 86 | } |
||
| 87 |