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\IsbnFieldInterface; |
||
| 9 | use EdmondsCommerce\DoctrineStaticMeta\Entity\Interfaces\ValidatedEntityInterface; |
||
| 10 | use EdmondsCommerce\DoctrineStaticMeta\MappingHelper; |
||
| 11 | use Symfony\Component\Validator\Constraints\Isbn; |
||
| 12 | use Symfony\Component\Validator\Mapping\ClassMetadata as ValidatorClassMetaData; |
||
| 13 | |||
| 14 | // phpcs:enable |
||
| 15 | trait IsbnFieldTrait |
||
| 16 | { |
||
| 17 | |||
| 18 | /** |
||
| 19 | * @var string|null |
||
| 20 | */ |
||
| 21 | private $isbn; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * @SuppressWarnings(PHPMD.StaticAccess) |
||
| 25 | * @param ClassMetadataBuilder $builder |
||
| 26 | */ |
||
| 27 | 1 | public static function metaForIsbn(ClassMetadataBuilder $builder): void |
|
| 28 | { |
||
| 29 | 1 | MappingHelper::setSimpleStringFields( |
|
| 30 | 1 | [IsbnFieldInterface::PROP_ISBN], |
|
| 31 | 1 | $builder, |
|
| 32 | 1 | IsbnFieldInterface::DEFAULT_ISBN, |
|
| 33 | 1 | true |
|
| 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 | * @param ValidatorClassMetaData $metadata |
||
| 43 | * |
||
| 44 | * @throws \Symfony\Component\Validator\Exception\MissingOptionsException |
||
| 45 | * @throws \Symfony\Component\Validator\Exception\InvalidOptionsException |
||
| 46 | * @throws \Symfony\Component\Validator\Exception\ConstraintDefinitionException |
||
| 47 | */ |
||
| 48 | 4 | protected static function validatorMetaForIsbn(ValidatorClassMetaData $metadata) |
|
| 49 | { |
||
| 50 | 4 | $metadata->addPropertyConstraint( |
|
| 51 | 4 | IsbnFieldInterface::PROP_ISBN, |
|
| 52 | 4 | new Isbn() |
|
| 53 | ); |
||
| 54 | 4 | } |
|
| 55 | |||
| 56 | /** |
||
| 57 | * @return string|null |
||
| 58 | */ |
||
| 59 | 3 | public function getIsbn(): ?string |
|
| 60 | { |
||
| 61 | 3 | if (null === $this->isbn) { |
|
| 62 | 1 | return IsbnFieldInterface::DEFAULT_ISBN; |
|
| 63 | } |
||
| 64 | |||
| 65 | 3 | return $this->isbn; |
|
| 66 | } |
||
| 67 | |||
| 68 | /** |
||
| 69 | * @param string|null $isbn |
||
| 70 | * |
||
| 71 | * @return self |
||
| 72 | */ |
||
| 73 | 4 | public function setIsbn(?string $isbn): self |
|
| 74 | { |
||
| 75 | 4 | $this->updatePropertyValueThenValidateAndNotify( |
|
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 76 | 4 | IsbnFieldInterface::PROP_ISBN, |
|
| 77 | 4 | $isbn |
|
| 78 | ); |
||
| 79 | |||
| 80 | 3 | return $this; |
|
| 81 | } |
||
| 82 | } |
||
| 83 |