edmondscommerce /
doctrine-static-meta
| 1 | <?php declare(strict_types=1); |
||
| 2 | |||
| 3 | namespace EdmondsCommerce\DoctrineStaticMeta\Entity\Fields\Traits\String; |
||
| 4 | |||
| 5 | use Doctrine\DBAL\Types\Type; |
||
| 6 | use Doctrine\ORM\Mapping\Builder\ClassMetadataBuilder; |
||
| 7 | use Doctrine\ORM\Mapping\Builder\FieldBuilder; |
||
| 8 | use EdmondsCommerce\DoctrineStaticMeta\Entity\Fields\Interfaces\String\ShortIndexedRequiredStringFieldInterface; |
||
| 9 | use EdmondsCommerce\DoctrineStaticMeta\MappingHelper; |
||
| 10 | use Symfony\Component\Validator\Constraints\NotBlank; |
||
| 11 | use Symfony\Component\Validator\Constraints\NotEqualTo; |
||
| 12 | use Symfony\Component\Validator\Mapping\ClassMetadata as ValidatorClassMetaData; |
||
| 13 | |||
| 14 | /** |
||
| 15 | * This trait provides a short (50 characters) string which is non uniquely indexed and is required |
||
| 16 | * |
||
| 17 | * Trait ShortIndexedRequiredStringFieldTrait |
||
| 18 | * |
||
| 19 | * @package EdmondsCommerce\DoctrineStaticMeta\Entity\Fields\Traits\String |
||
| 20 | */ |
||
| 21 | trait ShortIndexedRequiredStringFieldTrait |
||
| 22 | { |
||
| 23 | |||
| 24 | /** |
||
| 25 | * @var string |
||
| 26 | */ |
||
| 27 | private $shortIndexedRequiredString; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * @SuppressWarnings(PHPMD.StaticAccess) |
||
| 31 | * @param ClassMetadataBuilder $builder |
||
| 32 | */ |
||
| 33 | public static function metaForShortIndexedRequiredString(ClassMetadataBuilder $builder): void |
||
| 34 | { |
||
| 35 | $columnName = MappingHelper::getColumnNameForField( |
||
| 36 | ShortIndexedRequiredStringFieldInterface::PROP_SHORT_INDEXED_REQUIRED_STRING |
||
| 37 | ); |
||
| 38 | $fieldBuilder = new FieldBuilder( |
||
| 39 | $builder, |
||
| 40 | [ |
||
| 41 | 'fieldName' => ShortIndexedRequiredStringFieldInterface::PROP_SHORT_INDEXED_REQUIRED_STRING, |
||
| 42 | 'type' => Type::STRING, |
||
| 43 | 'default' => ShortIndexedRequiredStringFieldInterface::DEFAULT_SHORT_INDEXED_REQUIRED_STRING, |
||
| 44 | ] |
||
| 45 | ); |
||
| 46 | $fieldBuilder |
||
| 47 | ->columnName($columnName) |
||
| 48 | ->nullable(false) |
||
| 49 | ->unique(false) |
||
| 50 | ->length(50) |
||
| 51 | ->build(); |
||
| 52 | |||
| 53 | $builder->addIndex([$columnName], $columnName . '_idx'); |
||
| 54 | } |
||
| 55 | |||
| 56 | /** |
||
| 57 | * @param ValidatorClassMetaData $metadata |
||
| 58 | */ |
||
| 59 | protected static function validatorMetaForPropertyShortIndexedRequiredString(ValidatorClassMetaData $metadata): void |
||
| 60 | { |
||
| 61 | $metadata->addPropertyConstraints( |
||
| 62 | ShortIndexedRequiredStringFieldInterface::PROP_SHORT_INDEXED_REQUIRED_STRING, |
||
| 63 | [ |
||
| 64 | new NotEqualTo(ShortIndexedRequiredStringFieldInterface::DEFAULT_SHORT_INDEXED_REQUIRED_STRING), |
||
| 65 | new NotBlank(), |
||
| 66 | ] |
||
| 67 | ); |
||
| 68 | } |
||
| 69 | |||
| 70 | /** |
||
| 71 | * @return string |
||
| 72 | */ |
||
| 73 | public function getShortIndexedRequiredString(): string |
||
| 74 | { |
||
| 75 | if (null === $this->shortIndexedRequiredString) { |
||
| 76 | return ShortIndexedRequiredStringFieldInterface::DEFAULT_SHORT_INDEXED_REQUIRED_STRING; |
||
| 77 | } |
||
| 78 | |||
| 79 | return $this->shortIndexedRequiredString; |
||
| 80 | } |
||
| 81 | |||
| 82 | /** |
||
| 83 | * @param string|null $shortIndexedRequiredString |
||
| 84 | * |
||
| 85 | * @return self |
||
| 86 | */ |
||
| 87 | private function setShortIndexedRequiredString(string $shortIndexedRequiredString): self |
||
| 88 | { |
||
| 89 | $this->updatePropertyValue( |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 90 | ShortIndexedRequiredStringFieldInterface::PROP_SHORT_INDEXED_REQUIRED_STRING, |
||
| 91 | $shortIndexedRequiredString |
||
| 92 | ); |
||
| 93 | |||
| 94 | return $this; |
||
| 95 | } |
||
| 96 | |||
| 97 | private function initShortIndexedRequiredString(): void |
||
| 98 | { |
||
| 99 | $this->shortIndexedRequiredString = |
||
| 100 | ShortIndexedRequiredStringFieldInterface::DEFAULT_SHORT_INDEXED_REQUIRED_STRING; |
||
| 101 | } |
||
| 102 | } |
||
| 103 |