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