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\DBAL\Types\Type; |
||
| 8 | use Doctrine\ORM\Mapping\Builder\ClassMetadataBuilder; |
||
| 9 | use Doctrine\ORM\Mapping\Builder\FieldBuilder; |
||
| 10 | use EdmondsCommerce\DoctrineStaticMeta\Entity\Fields\Interfaces\String\UniqueStringFieldInterface; |
||
| 11 | use EdmondsCommerce\DoctrineStaticMeta\MappingHelper; |
||
| 12 | use EdmondsCommerce\DoctrineStaticMeta\Schema\Database; |
||
| 13 | use Symfony\Component\Validator\Mapping\ClassMetadata as ValidatorClassMetaData; |
||
| 14 | |||
| 15 | // phpcs:enable |
||
| 16 | trait UniqueStringFieldTrait |
||
| 17 | { |
||
| 18 | |||
| 19 | /** |
||
| 20 | * @var string|null |
||
| 21 | */ |
||
| 22 | private $uniqueString; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * @param ClassMetadataBuilder $builder |
||
| 26 | * @SuppressWarnings(PHPMD.StaticAccess) |
||
| 27 | */ |
||
| 28 | public static function metaForUniqueString(ClassMetadataBuilder $builder): void |
||
| 29 | { |
||
| 30 | $fieldBuilder = new FieldBuilder( |
||
| 31 | $builder, |
||
| 32 | [ |
||
| 33 | 'fieldName' => UniqueStringFieldInterface::PROP_UNIQUE_STRING, |
||
| 34 | 'type' => Type::STRING, |
||
| 35 | 'default' => UniqueStringFieldInterface::DEFAULT_UNIQUE_STRING, |
||
| 36 | ] |
||
| 37 | ); |
||
| 38 | $fieldBuilder |
||
| 39 | ->columnName(MappingHelper::getColumnNameForField(UniqueStringFieldInterface::PROP_UNIQUE_STRING)) |
||
| 40 | ->nullable(false) |
||
| 41 | ->unique(true) |
||
| 42 | ->length(Database::MAX_VARCHAR_LENGTH) |
||
| 43 | ->build(); |
||
| 44 | } |
||
| 45 | |||
| 46 | /** |
||
| 47 | * This method sets the validation for this field. |
||
| 48 | * |
||
| 49 | * You should add in as many relevant property constraints as you see fit. |
||
| 50 | * |
||
| 51 | * Remove the PHPMD suppressed warning once you start setting constraints |
||
| 52 | * |
||
| 53 | * @SuppressWarnings(PHPMD.UnusedFormalParameter) |
||
| 54 | * @see https://symfony.com/doc/current/validation.html#supported-constraints |
||
| 55 | * |
||
| 56 | * @param ValidatorClassMetaData $metadata |
||
| 57 | * |
||
| 58 | * @throws \Symfony\Component\Validator\Exception\MissingOptionsException |
||
| 59 | * @throws \Symfony\Component\Validator\Exception\InvalidOptionsException |
||
| 60 | * @throws \Symfony\Component\Validator\Exception\ConstraintDefinitionException |
||
| 61 | */ |
||
| 62 | protected static function validatorMetaForPropertyUniqueString(ValidatorClassMetaData $metadata): void |
||
| 63 | { |
||
| 64 | // $metadata->addPropertyConstraint( |
||
| 65 | // UniqueStringFieldInterface::PROP_UNIQUE_STRING, |
||
| 66 | // new NotBlank() |
||
| 67 | // ); |
||
| 68 | } |
||
| 69 | |||
| 70 | /** |
||
| 71 | * @return string|null |
||
| 72 | */ |
||
| 73 | public function getUniqueString(): ?string |
||
| 74 | { |
||
| 75 | if (null === $this->uniqueString) { |
||
| 76 | return UniqueStringFieldInterface::DEFAULT_UNIQUE_STRING; |
||
| 77 | } |
||
| 78 | |||
| 79 | return $this->uniqueString; |
||
| 80 | } |
||
| 81 | |||
| 82 | /** |
||
| 83 | * @param string|null $uniqueString |
||
| 84 | * |
||
| 85 | * @return self |
||
| 86 | */ |
||
| 87 | private function setUniqueString(?string $uniqueString): self |
||
| 88 | { |
||
| 89 | $this->updatePropertyValue( |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 90 | UniqueStringFieldInterface::PROP_UNIQUE_STRING, |
||
| 91 | $uniqueString |
||
| 92 | ); |
||
| 93 | |||
| 94 | return $this; |
||
| 95 | } |
||
| 96 | } |
||
| 97 |