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\EnumFieldInterface; |
||
| 9 | use EdmondsCommerce\DoctrineStaticMeta\Entity\Interfaces\ValidatedEntityInterface; |
||
| 10 | use EdmondsCommerce\DoctrineStaticMeta\MappingHelper; |
||
| 11 | use Symfony\Component\Validator\Constraints\Choice; |
||
| 12 | use Symfony\Component\Validator\Mapping\ClassMetadata as ValidatorClassMetaData; |
||
| 13 | |||
| 14 | // phpcs:enable |
||
| 15 | trait EnumFieldTrait |
||
| 16 | { |
||
| 17 | |||
| 18 | /** |
||
| 19 | * @var string |
||
| 20 | */ |
||
| 21 | private $enum; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * @SuppressWarnings(PHPMD.StaticAccess) |
||
| 25 | * @param ClassMetadataBuilder $builder |
||
| 26 | */ |
||
| 27 | 1 | public static function metaForEnum(ClassMetadataBuilder $builder): void |
|
| 28 | { |
||
| 29 | 1 | MappingHelper::setSimpleStringFields( |
|
| 30 | 1 | [EnumFieldInterface::PROP_ENUM], |
|
| 31 | 1 | $builder, |
|
| 32 | 1 | EnumFieldInterface::DEFAULT_ENUM, |
|
| 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 | * @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 | 1 | protected static function validatorMetaForEnum(ValidatorClassMetaData $metadata): void |
|
| 49 | { |
||
| 50 | 1 | $metadata->addPropertyConstraint( |
|
| 51 | 1 | EnumFieldInterface::PROP_ENUM, |
|
| 52 | 1 | new Choice(EnumFieldInterface::ENUM_OPTIONS) |
|
| 53 | ); |
||
| 54 | 1 | } |
|
| 55 | |||
| 56 | 2 | private function initEnum(): void |
|
| 57 | { |
||
| 58 | 2 | $this->enum = EnumFieldInterface::DEFAULT_ENUM; |
|
| 59 | 2 | } |
|
| 60 | |||
| 61 | /** |
||
| 62 | * @return string |
||
| 63 | */ |
||
| 64 | 2 | public function getEnum(): string |
|
| 65 | { |
||
| 66 | 2 | if (null === $this->enum) { |
|
| 67 | return EnumFieldInterface::DEFAULT_ENUM; |
||
| 68 | } |
||
| 69 | |||
| 70 | 2 | return $this->enum; |
|
| 71 | } |
||
| 72 | |||
| 73 | /** |
||
| 74 | * Uses the Symfony Validator and fails back to basic in_array validation with exception |
||
| 75 | * |
||
| 76 | * @param string $enum |
||
| 77 | * |
||
| 78 | * @return self |
||
| 79 | */ |
||
| 80 | 2 | public function setEnum(string $enum): self |
|
| 81 | { |
||
| 82 | 2 | $this->updatePropertyValueThenValidateAndNotify( |
|
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 83 | 2 | EnumFieldInterface::PROP_ENUM, |
|
| 84 | 2 | $enum |
|
| 85 | ); |
||
| 86 | |||
| 87 | 2 | return $this; |
|
| 88 | } |
||
| 89 | } |
||
| 90 |