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