1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace EdmondsCommerce\DoctrineStaticMeta\Entity\Fields\Traits\Numeric; |
4
|
|
|
|
5
|
|
|
use Doctrine\ORM\Mapping\Builder\ClassMetadataBuilder; |
6
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Entity\Fields\Interfaces\Numeric\IntegerWithinRangeFieldInterface; |
7
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\MappingHelper; |
8
|
|
|
use Symfony\Component\Validator\Constraints\Range; |
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
|
|
|
|
14
|
|
|
trait IntegerWithinRangeFieldTrait |
15
|
|
|
{ |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var int|null |
19
|
|
|
*/ |
20
|
|
|
private $integerWithinRange; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @SuppressWarnings(PHPMD.StaticAccess) |
24
|
|
|
* @param ClassMetadataBuilder $builder |
25
|
|
|
*/ |
26
|
6 |
|
public static function metaForIntegerWithinRange(ClassMetadataBuilder $builder): void |
27
|
|
|
{ |
28
|
6 |
|
MappingHelper::setSimpleIntegerFields( |
29
|
6 |
|
[IntegerWithinRangeFieldInterface::PROP_INTEGER_WITHIN_RANGE], |
30
|
6 |
|
$builder, |
31
|
6 |
|
IntegerWithinRangeFieldInterface::DEFAULT_INTEGER_WITHIN_RANGE |
32
|
|
|
); |
33
|
6 |
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* This method sets the validation for this field. |
37
|
|
|
* |
38
|
|
|
* You should add in as many relevant property constraints as you see fit. |
39
|
|
|
* |
40
|
|
|
* @see https://symfony.com/doc/current/validation.html#supported-constraints |
41
|
|
|
* |
42
|
|
|
* @param ValidatorClassMetaData $metadata |
43
|
|
|
* |
44
|
|
|
* @throws MissingOptionsException |
45
|
|
|
* @throws InvalidOptionsException |
46
|
|
|
* @throws ConstraintDefinitionException |
47
|
|
|
*/ |
48
|
6 |
|
protected static function validatorMetaForPropertyIntegerWithinRange(ValidatorClassMetaData $metadata): void |
49
|
|
|
{ |
50
|
6 |
|
$metadata->addPropertyConstraint( |
51
|
6 |
|
IntegerWithinRangeFieldInterface::PROP_INTEGER_WITHIN_RANGE, |
52
|
6 |
|
new Range( |
53
|
|
|
[ |
54
|
6 |
|
'min' => IntegerWithinRangeFieldInterface::MIN_INTEGER_WITHIN_RANGE, |
55
|
|
|
'max' => IntegerWithinRangeFieldInterface::MAX_INTEGER_WITHIN_RANGE, |
56
|
6 |
|
'minMessage' => IntegerWithinRangeFieldInterface::MIN_MESSAGE_INTEGER_WITHIN_RANGE, |
57
|
6 |
|
'maxMessage' => IntegerWithinRangeFieldInterface::MAX_MESSAGE_INTEGER_WITHIN_RANGE |
58
|
|
|
] |
59
|
|
|
) |
60
|
|
|
); |
61
|
6 |
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @return int|null |
65
|
|
|
*/ |
66
|
6 |
|
public function getIntegerWithinRange(): ?int |
67
|
|
|
{ |
68
|
6 |
|
if (null === $this->integerWithinRange) { |
69
|
6 |
|
return IntegerWithinRangeFieldInterface::DEFAULT_INTEGER_WITHIN_RANGE; |
70
|
|
|
} |
71
|
|
|
|
72
|
6 |
|
return $this->integerWithinRange; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @param int|null $integerWithinRange |
77
|
|
|
* |
78
|
|
|
* @return self |
79
|
|
|
*/ |
80
|
6 |
|
private function setIntegerWithinRange(?int $integerWithinRange): self |
81
|
|
|
{ |
82
|
6 |
|
$this->updatePropertyValue( |
|
|
|
|
83
|
6 |
|
IntegerWithinRangeFieldInterface::PROP_INTEGER_WITHIN_RANGE, |
84
|
6 |
|
$integerWithinRange |
85
|
|
|
); |
86
|
|
|
|
87
|
6 |
|
return $this; |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|