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\MappingHelper; |
||
| 10 | use Symfony\Component\Validator\Constraints\Isbn; |
||
| 11 | use Symfony\Component\Validator\Mapping\ClassMetadata as ValidatorClassMetaData; |
||
| 12 | |||
| 13 | // phpcs:enable |
||
| 14 | trait IsbnFieldTrait |
||
| 15 | { |
||
| 16 | |||
| 17 | /** |
||
| 18 | * @var string|null |
||
| 19 | */ |
||
| 20 | private $isbn; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * @SuppressWarnings(PHPMD.StaticAccess) |
||
| 24 | * @param ClassMetadataBuilder $builder |
||
| 25 | */ |
||
| 26 | public static function metaForIsbn(ClassMetadataBuilder $builder): void |
||
| 27 | { |
||
| 28 | MappingHelper::setSimpleStringFields( |
||
| 29 | [IsbnFieldInterface::PROP_ISBN], |
||
| 30 | $builder, |
||
| 31 | IsbnFieldInterface::DEFAULT_ISBN, |
||
| 32 | true |
||
| 33 | ); |
||
| 34 | } |
||
| 35 | |||
| 36 | /** |
||
| 37 | * This method sets the validation for this field. |
||
| 38 | * |
||
| 39 | * You should add in as many relevant property constraints as you see fit. |
||
| 40 | * |
||
| 41 | * @param ValidatorClassMetaData $metadata |
||
| 42 | * |
||
| 43 | * @throws \Symfony\Component\Validator\Exception\MissingOptionsException |
||
| 44 | * @throws \Symfony\Component\Validator\Exception\InvalidOptionsException |
||
| 45 | * @throws \Symfony\Component\Validator\Exception\ConstraintDefinitionException |
||
| 46 | */ |
||
| 47 | protected static function validatorMetaForPropertyIsbn(ValidatorClassMetaData $metadata): void |
||
| 48 | { |
||
| 49 | $metadata->addPropertyConstraint( |
||
| 50 | IsbnFieldInterface::PROP_ISBN, |
||
| 51 | new Isbn() |
||
| 52 | ); |
||
| 53 | } |
||
| 54 | |||
| 55 | /** |
||
| 56 | * @return string|null |
||
| 57 | */ |
||
| 58 | public function getIsbn(): ?string |
||
| 59 | { |
||
| 60 | if (null === $this->isbn) { |
||
| 61 | return IsbnFieldInterface::DEFAULT_ISBN; |
||
| 62 | } |
||
| 63 | |||
| 64 | return $this->isbn; |
||
| 65 | } |
||
| 66 | |||
| 67 | /** |
||
| 68 | * @param string|null $isbn |
||
| 69 | * |
||
| 70 | * @return self |
||
| 71 | */ |
||
| 72 | private function setIsbn(?string $isbn): self |
||
| 73 | { |
||
| 74 | $this->updatePropertyValue( |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 75 | IsbnFieldInterface::PROP_ISBN, |
||
| 76 | $isbn |
||
| 77 | ); |
||
| 78 | |||
| 79 | return $this; |
||
| 80 | } |
||
| 81 | } |
||
| 82 |