edmondscommerce /
doctrine-static-meta
| 1 | <?php |
||
| 2 | |||
| 3 | declare(strict_types=1); |
||
| 4 | |||
| 5 | namespace EdmondsCommerce\DoctrineStaticMeta\Entity\Fields\Traits\String; |
||
| 6 | |||
| 7 | // phpcs:disable |
||
| 8 | |||
| 9 | use Doctrine\ORM\Mapping\Builder\ClassMetadataBuilder; |
||
| 10 | use EdmondsCommerce\DoctrineStaticMeta\Entity\Fields\Interfaces\String\NullableStringFieldInterface; |
||
| 11 | use EdmondsCommerce\DoctrineStaticMeta\MappingHelper; |
||
| 12 | use EdmondsCommerce\DoctrineStaticMeta\Schema\Database; |
||
| 13 | use Symfony\Component\Validator\Constraints\Length; |
||
| 14 | use Symfony\Component\Validator\Exception\ConstraintDefinitionException; |
||
| 15 | use Symfony\Component\Validator\Exception\InvalidOptionsException; |
||
| 16 | use Symfony\Component\Validator\Exception\MissingOptionsException; |
||
| 17 | use Symfony\Component\Validator\Mapping\ClassMetadata as ValidatorClassMetaData; |
||
| 18 | |||
| 19 | // phpcs:enable |
||
| 20 | trait NullableStringFieldTrait |
||
| 21 | { |
||
| 22 | |||
| 23 | /** |
||
| 24 | * @var string|null |
||
| 25 | */ |
||
| 26 | private $nullableString; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * @SuppressWarnings(PHPMD.StaticAccess) |
||
| 30 | * @param ClassMetadataBuilder $builder |
||
| 31 | */ |
||
| 32 | public static function metaForNullableString(ClassMetadataBuilder $builder): void |
||
| 33 | { |
||
| 34 | MappingHelper::setSimpleStringFields( |
||
| 35 | [NullableStringFieldInterface::PROP_NULLABLE_STRING], |
||
| 36 | $builder, |
||
| 37 | NullableStringFieldInterface::DEFAULT_NULLABLE_STRING |
||
| 38 | ); |
||
| 39 | } |
||
| 40 | |||
| 41 | /** |
||
| 42 | * This method sets the validation for this field. |
||
| 43 | * |
||
| 44 | * You should add in as many relevant property constraints as you see fit. |
||
| 45 | * |
||
| 46 | * Remove the PHPMD suppressed warning once you start setting constraints |
||
| 47 | * |
||
| 48 | * @SuppressWarnings(PHPMD.UnusedFormalParameter) |
||
| 49 | * @see https://symfony.com/doc/current/validation.html#supported-constraints |
||
| 50 | * |
||
| 51 | * @param ValidatorClassMetaData $metadata |
||
| 52 | * |
||
| 53 | * @throws MissingOptionsException |
||
| 54 | * @throws InvalidOptionsException |
||
| 55 | * @throws ConstraintDefinitionException |
||
| 56 | */ |
||
| 57 | protected static function validatorMetaForPropertyNullableString(ValidatorClassMetaData $metadata): void |
||
| 58 | { |
||
| 59 | $metadata->addPropertyConstraint( |
||
| 60 | NullableStringFieldInterface::PROP_NULLABLE_STRING, |
||
| 61 | new Length( |
||
| 62 | [ |
||
| 63 | 'min' => 0, |
||
| 64 | 'max' => Database::MAX_VARCHAR_LENGTH, |
||
| 65 | ] |
||
| 66 | ) |
||
| 67 | ); |
||
| 68 | } |
||
| 69 | |||
| 70 | /** |
||
| 71 | * @return string|null |
||
| 72 | */ |
||
| 73 | public function getNullableString(): ?string |
||
| 74 | { |
||
| 75 | if (null === $this->nullableString) { |
||
| 76 | return NullableStringFieldInterface::DEFAULT_NULLABLE_STRING; |
||
| 77 | } |
||
| 78 | |||
| 79 | return $this->nullableString; |
||
| 80 | } |
||
| 81 | |||
| 82 | /** |
||
| 83 | * @param string|null $nullableString |
||
| 84 | * |
||
| 85 | * @return self |
||
| 86 | */ |
||
| 87 | private function setNullableString(?string $nullableString): self |
||
| 88 | { |
||
| 89 | $this->updatePropertyValue( |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 90 | NullableStringFieldInterface::PROP_NULLABLE_STRING, |
||
| 91 | $nullableString |
||
| 92 | ); |
||
| 93 | |||
| 94 | return $this; |
||
| 95 | } |
||
| 96 | } |
||
| 97 |