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
|
|
|
public static function metaForFloatWithinRange(ClassMetadataBuilder $builder): void |
23
|
|
|
{ |
24
|
|
|
MappingHelper::setSimpleFloatFields( |
25
|
|
|
[FloatWithinRangeFieldInterface::PROP_FLOAT_WITHIN_RANGE], |
26
|
|
|
$builder, |
27
|
|
|
FloatWithinRangeFieldInterface::DEFAULT_FLOAT_WITHIN_RANGE |
28
|
|
|
); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @return float|null |
33
|
|
|
*/ |
34
|
|
|
public function getFloatWithinRange(): ?float |
35
|
|
|
{ |
36
|
|
|
if (null === $this->floatWithinRange) { |
37
|
|
|
return FloatWithinRangeFieldInterface::DEFAULT_FLOAT_WITHIN_RANGE; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
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
|
|
|
protected static function validatorMetaForPropertyFloatWithinRange(ValidatorClassMetaData $metadata): void |
57
|
|
|
{ |
58
|
|
|
$metadata->addPropertyConstraint( |
59
|
|
|
FloatWithinRangeFieldInterface::PROP_FLOAT_WITHIN_RANGE, |
60
|
|
|
new Range( |
61
|
|
|
[ |
62
|
|
|
'min' => FloatWithinRangeFieldInterface::MIN_FLOAT_WITHIN_RANGE, |
63
|
|
|
'max' => FloatWithinRangeFieldInterface::MAX_FLOAT_WITHIN_RANGE, |
64
|
|
|
'minMessage' => FloatWithinRangeFieldInterface::MIN_MESSAGE_FLOAT_WITHIN_RANGE, |
65
|
|
|
'maxMessage' => FloatWithinRangeFieldInterface::MAX_MESSAGE_FLOAT_WITHIN_RANGE, |
66
|
|
|
] |
67
|
|
|
) |
68
|
|
|
); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @param float|null $floatWithinRange |
73
|
|
|
* |
74
|
|
|
* @return self |
75
|
|
|
*/ |
76
|
|
|
private function setFloatWithinRange(?float $floatWithinRange): self |
77
|
|
|
{ |
78
|
|
|
$this->updatePropertyValue( |
|
|
|
|
79
|
|
|
FloatWithinRangeFieldInterface::PROP_FLOAT_WITHIN_RANGE, |
80
|
|
|
$floatWithinRange |
81
|
|
|
); |
82
|
|
|
|
83
|
|
|
return $this; |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|