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