edmondscommerce /
doctrine-static-meta
| 1 | <?php declare(strict_types=1); |
||
| 2 | |||
| 3 | namespace EdmondsCommerce\DoctrineStaticMeta\Entity\Fields\Traits\Boolean; |
||
| 4 | |||
| 5 | // phpcs:disable |
||
| 6 | |||
| 7 | use Doctrine\ORM\Mapping\Builder\ClassMetadataBuilder; |
||
| 8 | use EdmondsCommerce\DoctrineStaticMeta\Entity\Fields\Interfaces\Boolean\DefaultsNullFieldInterface; |
||
| 9 | use EdmondsCommerce\DoctrineStaticMeta\Entity\Interfaces\ValidatedEntityInterface; |
||
| 10 | use EdmondsCommerce\DoctrineStaticMeta\MappingHelper; |
||
| 11 | use Symfony\Component\Validator\Mapping\ClassMetadata as ValidatorClassMetaData; |
||
| 12 | |||
| 13 | // phpcs:enable |
||
| 14 | trait DefaultsNullFieldTrait |
||
| 15 | { |
||
| 16 | |||
| 17 | /** |
||
| 18 | * @var bool|null |
||
| 19 | */ |
||
| 20 | private $defaultsNull; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * @SuppressWarnings(PHPMD.StaticAccess) |
||
| 24 | * @param ClassMetadataBuilder $builder |
||
| 25 | */ |
||
| 26 | 1 | public static function metaForDefaultsNull(ClassMetadataBuilder $builder): void |
|
| 27 | { |
||
| 28 | 1 | MappingHelper::setSimpleBooleanFields( |
|
| 29 | 1 | [DefaultsNullFieldInterface::PROP_DEFAULTS_NULL], |
|
| 30 | 1 | $builder, |
|
| 31 | 1 | DefaultsNullFieldInterface::DEFAULT_DEFAULTS_NULL |
|
| 32 | ); |
||
| 33 | 1 | } |
|
| 34 | |||
| 35 | /** |
||
| 36 | * This method sets the validation for this field. |
||
| 37 | * |
||
| 38 | * You should add in as many relevant property constraints as you see fit. |
||
| 39 | * |
||
| 40 | * Remove the PHPMD suppressed warning once you start setting constraints |
||
| 41 | * |
||
| 42 | * @SuppressWarnings(PHPMD.UnusedFormalParameter) |
||
| 43 | * @see https://symfony.com/doc/current/validation.html#supported-constraints |
||
| 44 | * |
||
| 45 | * @param ValidatorClassMetaData $metadata |
||
| 46 | * |
||
| 47 | * @throws \Symfony\Component\Validator\Exception\MissingOptionsException |
||
| 48 | * @throws \Symfony\Component\Validator\Exception\InvalidOptionsException |
||
| 49 | * @throws \Symfony\Component\Validator\Exception\ConstraintDefinitionException |
||
| 50 | */ |
||
| 51 | 2 | protected static function validatorMetaForDefaultsNull(ValidatorClassMetaData $metadata) |
|
| 52 | { |
||
| 53 | // $metadata->addPropertyConstraint( |
||
| 54 | // DefaultsNullFieldInterface::PROP_DEFAULTS_NULL, |
||
| 55 | // new NotBlank() |
||
| 56 | // ); |
||
| 57 | 2 | } |
|
| 58 | |||
| 59 | /** |
||
| 60 | * @return bool|null |
||
| 61 | */ |
||
| 62 | 2 | public function isDefaultsNull(): ?bool |
|
| 63 | { |
||
| 64 | 2 | return $this->defaultsNull; |
|
| 65 | } |
||
| 66 | |||
| 67 | /** |
||
| 68 | * @param bool|null $defaultsNull |
||
| 69 | * |
||
| 70 | * @return self |
||
| 71 | */ |
||
| 72 | 2 | public function setDefaultsNull(?bool $defaultsNull): self |
|
| 73 | { |
||
| 74 | 2 | $this->updatePropertyValueThenValidateAndNotify( |
|
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 75 | 2 | DefaultsNullFieldInterface::PROP_DEFAULTS_NULL, |
|
| 76 | 2 | $defaultsNull |
|
| 77 | ); |
||
| 78 | |||
| 79 | 2 | return $this; |
|
| 80 | } |
||
| 81 | } |
||
| 82 |