1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace EdmondsCommerce\DoctrineStaticMeta\Entity\Fields\Traits\Flag; |
4
|
|
|
|
5
|
|
|
// phpcs:disable |
6
|
|
|
|
7
|
|
|
use Doctrine\ORM\Mapping\Builder\ClassMetadataBuilder; |
8
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Entity\Fields\Interfaces\Flag\DefaultFieldInterface; |
9
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Entity\Interfaces\EntityInterface; |
10
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Entity\Interfaces\ValidatedEntityInterface; |
11
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\MappingHelper; |
12
|
|
|
use Symfony\Component\Validator\Constraints\NotNull; |
13
|
|
|
use Symfony\Component\Validator\Mapping\ClassMetadata as ValidatorClassMetaData; |
14
|
|
|
|
15
|
|
|
// phpcs:enable |
16
|
|
|
trait DefaultFieldTrait |
17
|
|
|
{ |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var bool |
21
|
|
|
*/ |
22
|
|
|
private $default; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @SuppressWarnings(PHPMD.StaticAccess) |
26
|
|
|
* @param ClassMetadataBuilder $builder |
27
|
|
|
*/ |
28
|
|
|
public static function getPropertyDoctrineMetaForIsDefault(ClassMetadataBuilder $builder): void |
29
|
|
|
{ |
30
|
|
|
MappingHelper::setSimpleBooleanFields( |
31
|
|
|
[DefaultFieldInterface::PROP_DEFAULT], |
32
|
|
|
$builder, |
33
|
|
|
false |
34
|
|
|
); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @param ValidatorClassMetaData $metadata |
39
|
|
|
* |
40
|
|
|
* @throws \Symfony\Component\Validator\Exception\MissingOptionsException |
41
|
|
|
* @throws \Symfony\Component\Validator\Exception\InvalidOptionsException |
42
|
|
|
* @throws \Symfony\Component\Validator\Exception\ConstraintDefinitionException |
43
|
|
|
*/ |
44
|
|
|
protected static function getPropertyValidatorMetaForIsDefault(ValidatorClassMetaData $metadata): void |
45
|
|
|
{ |
46
|
|
|
$metadata->addPropertyConstraint( |
47
|
|
|
DefaultFieldInterface::PROP_DEFAULT, |
48
|
|
|
new NotNull() |
49
|
|
|
); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @return bool |
54
|
|
|
*/ |
55
|
|
|
public function isDefault(): bool |
56
|
|
|
{ |
57
|
|
|
return $this->default; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @param bool $default |
62
|
|
|
* |
63
|
|
|
* @return $this|DefaultFieldInterface |
64
|
|
|
*/ |
65
|
|
|
public function setDefault(bool $default): self |
66
|
|
|
{ |
67
|
|
|
$this->default = $default; |
68
|
|
|
if ($this instanceof ValidatedEntityInterface) { |
69
|
|
|
$this->validateProperty(DefaultFieldInterface::PROP_DEFAULT); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
return $this; |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|