edmondscommerce /
doctrine-static-meta
| 1 | <?php declare(strict_types=1); |
||
| 2 | // phpcs:disable |
||
| 3 | namespace TemplateNamespace\Entity\Relations\TemplateEntity\Traits; |
||
| 4 | |||
| 5 | use Doctrine\ORM\Mapping\Builder\ClassMetadataBuilder; |
||
| 6 | use EdmondsCommerce\DoctrineStaticMeta\DoctrineStaticMeta; |
||
| 7 | use Symfony\Component\Validator\Constraints\NotBlank; |
||
| 8 | use Symfony\Component\Validator\Constraints\Valid; |
||
| 9 | use Symfony\Component\Validator\Exception\ConstraintDefinitionException; |
||
| 10 | use Symfony\Component\Validator\Exception\InvalidOptionsException; |
||
| 11 | use Symfony\Component\Validator\Exception\MissingOptionsException; |
||
| 12 | use Symfony\Component\Validator\Mapping\ClassMetadata as ValidatorClassMetaData; |
||
| 13 | use TemplateNamespace\Entity\Interfaces\TemplateEntityInterface; |
||
| 14 | use TemplateNamespace\Entity\Relations\TemplateEntity\Interfaces\HasRequiredTemplateEntityInterface; |
||
| 15 | use TemplateNamespace\Entity\Relations\TemplateEntity\Interfaces\ReciprocatesTemplateEntityInterface; |
||
| 16 | |||
| 17 | /** |
||
| 18 | * The base trait for required relations to a single TemplateEntity |
||
| 19 | */ |
||
| 20 | // phpcs:enable |
||
| 21 | trait HasRequiredTemplateEntityAbstract |
||
| 22 | { |
||
| 23 | /** |
||
| 24 | * @var TemplateEntityInterface |
||
| 25 | */ |
||
| 26 | private $templateEntity; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * @param ClassMetadataBuilder $builder |
||
| 30 | * |
||
| 31 | * @return void |
||
| 32 | */ |
||
| 33 | abstract public static function metaForTemplateEntity( |
||
| 34 | ClassMetadataBuilder $builder |
||
| 35 | ): void; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * @param ValidatorClassMetaData $metadata |
||
| 39 | * |
||
| 40 | * @throws MissingOptionsException |
||
| 41 | * @throws InvalidOptionsException |
||
| 42 | * @throws ConstraintDefinitionException |
||
| 43 | */ |
||
| 44 | public static function validatorMetaForPropertyTemplateEntity( |
||
| 45 | ValidatorClassMetaData $metadata |
||
| 46 | ): void { |
||
| 47 | $validConstraint = new Valid(); |
||
| 48 | $validConstraint->traverse = false; |
||
| 49 | $metadata->addPropertyConstraint( |
||
| 50 | HasRequiredTemplateEntityInterface::PROPERTY_NAME_TEMPLATE_ENTITY, |
||
| 51 | new NotBlank() |
||
| 52 | ); |
||
| 53 | $metadata->addPropertyConstraint( |
||
| 54 | HasRequiredTemplateEntityInterface::PROPERTY_NAME_TEMPLATE_ENTITY, |
||
| 55 | $validConstraint |
||
| 56 | ); |
||
| 57 | } |
||
| 58 | |||
| 59 | private static function dsmInitRequiredRelationForTemplateEntity(DoctrineStaticMeta $dsm): void |
||
| 60 | { |
||
| 61 | $dsm->setRequiredRelationProperty( |
||
| 62 | new DoctrineStaticMeta\RequiredRelation( |
||
| 63 | HasRequiredTemplateEntityInterface::PROPERTY_NAME_TEMPLATE_ENTITY, |
||
| 64 | TemplateEntityInterface::class, |
||
| 65 | false |
||
| 66 | ) |
||
| 67 | ); |
||
| 68 | } |
||
| 69 | |||
| 70 | /** |
||
| 71 | * @return TemplateEntityInterface |
||
| 72 | */ |
||
| 73 | public function getTemplateEntity(): TemplateEntityInterface |
||
| 74 | { |
||
| 75 | return $this->templateEntity; |
||
| 76 | } |
||
| 77 | |||
| 78 | /** |
||
| 79 | * @param TemplateEntityInterface $templateEntity |
||
| 80 | * @param bool $recip |
||
| 81 | * |
||
| 82 | * @return $this |
||
| 83 | * @SuppressWarnings(PHPMD.BooleanArgumentFlag) |
||
| 84 | */ |
||
| 85 | public function setTemplateEntity( |
||
| 86 | TemplateEntityInterface $templateEntity, |
||
| 87 | bool $recip = true |
||
| 88 | ): self { |
||
| 89 | |||
| 90 | $this->setEntityAndNotify('templateEntity', $templateEntity); |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 91 | if ( |
||
| 92 | $this instanceof ReciprocatesTemplateEntityInterface |
||
| 93 | && true === $recip |
||
| 94 | ) { |
||
| 95 | $this->reciprocateRelationOnTemplateEntity($templateEntity); |
||
| 96 | } |
||
| 97 | |||
| 98 | return $this; |
||
| 99 | } |
||
| 100 | } |
||
| 101 |