|
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\Mapping\ClassMetadata as ValidatorClassMetaData; |
|
10
|
|
|
|
|
11
|
|
|
trait IntegerWithinRangeFieldTrait |
|
12
|
|
|
{ |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* @var int|null |
|
16
|
|
|
*/ |
|
17
|
|
|
private $integerWithinRange; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @SuppressWarnings(PHPMD.StaticAccess) |
|
21
|
|
|
*/ |
|
22
|
6 |
|
public static function metaForIntegerWithinRange(ClassMetadataBuilder $builder): void |
|
23
|
|
|
{ |
|
24
|
6 |
|
MappingHelper::setSimpleIntegerFields( |
|
25
|
6 |
|
[IntegerWithinRangeFieldInterface::PROP_INTEGER_WITHIN_RANGE], |
|
26
|
6 |
|
$builder, |
|
27
|
6 |
|
IntegerWithinRangeFieldInterface::DEFAULT_INTEGER_WITHIN_RANGE |
|
28
|
|
|
); |
|
29
|
6 |
|
} |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* This method sets the validation for this field. |
|
33
|
|
|
* |
|
34
|
|
|
* You should add in as many relevant property constraints as you see fit. |
|
35
|
|
|
* |
|
36
|
|
|
* @see https://symfony.com/doc/current/validation.html#supported-constraints |
|
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
|
6 |
|
protected static function validatorMetaForPropertyIntegerWithinRange(ValidatorClassMetaData $metadata): void |
|
45
|
|
|
{ |
|
46
|
6 |
|
$metadata->addPropertyConstraint( |
|
47
|
6 |
|
IntegerWithinRangeFieldInterface::PROP_INTEGER_WITHIN_RANGE, |
|
48
|
6 |
|
new Range( |
|
49
|
|
|
[ |
|
50
|
6 |
|
'min' => IntegerWithinRangeFieldInterface::MIN_INTEGER_WITHIN_RANGE, |
|
51
|
|
|
'max' => IntegerWithinRangeFieldInterface::MAX_INTEGER_WITHIN_RANGE, |
|
52
|
6 |
|
'minMessage' => IntegerWithinRangeFieldInterface::MIN_MESSAGE_INTEGER_WITHIN_RANGE, |
|
53
|
6 |
|
'maxMessage' => IntegerWithinRangeFieldInterface::MAX_MESSAGE_INTEGER_WITHIN_RANGE |
|
54
|
|
|
] |
|
55
|
|
|
) |
|
56
|
|
|
); |
|
57
|
6 |
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* @return int|null |
|
61
|
|
|
*/ |
|
62
|
6 |
|
public function getIntegerWithinRange(): ?int |
|
63
|
|
|
{ |
|
64
|
6 |
|
if (null === $this->integerWithinRange) { |
|
65
|
6 |
|
return IntegerWithinRangeFieldInterface::DEFAULT_INTEGER_WITHIN_RANGE; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
6 |
|
return $this->integerWithinRange; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* @param int|null $integerWithinRange |
|
73
|
|
|
* |
|
74
|
|
|
* @return self |
|
75
|
|
|
*/ |
|
76
|
6 |
|
private function setIntegerWithinRange(?int $integerWithinRange): self |
|
77
|
|
|
{ |
|
78
|
6 |
|
$this->updatePropertyValue( |
|
|
|
|
|
|
79
|
6 |
|
IntegerWithinRangeFieldInterface::PROP_INTEGER_WITHIN_RANGE, |
|
80
|
6 |
|
$integerWithinRange |
|
81
|
|
|
); |
|
82
|
|
|
|
|
83
|
6 |
|
return $this; |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
|