|
24 | 24 |
|
25 | 25 | final class NumericField extends FieldEvaluator implements Field |
26 | 26 | { |
| 27 | + public function __construct( |
| 28 | + public readonly int|float|null $min = null, |
| 29 | + public readonly int|float|null $max = null, |
| 30 | + float $confidenceThreshold = 0.8 |
| 31 | + ) { |
| 32 | + if (null !== $min && null !== $max && $min > $max) { |
| 33 | + throw new \ValueError('Minimum length can not be greater than maximum length.'); |
| 34 | + } |
| 35 | + |
| 36 | + parent::__construct($confidenceThreshold); |
| 37 | + } |
| 38 | + |
| 39 | + public static function min(int $value, float $confidenceThreshold = 0.8): self |
| 40 | + { |
| 41 | + return new self(min: $value, max: null, confidenceThreshold: $confidenceThreshold); |
| 42 | + } |
| 43 | + |
| 44 | + public static function max(int $value, float $confidenceThreshold = 0.8): self |
| 45 | + { |
| 46 | + return new self(min: null, max: $value, confidenceThreshold: $confidenceThreshold); |
| 47 | + } |
| 48 | + |
| 49 | + public static function fixed(int $value, float $confidenceThreshold = 0.8): self |
| 50 | + { |
| 51 | + return new self(min: $value, max: $value, confidenceThreshold: $confidenceThreshold); |
| 52 | + } |
| 53 | + |
| 54 | + public static function between(int $min, int $max, float $confidenceThreshold = 0.8): self |
| 55 | + { |
| 56 | + return new self(min: $min, max: $max, confidenceThreshold: $confidenceThreshold); |
| 57 | + } |
| 58 | + |
| 59 | + public static function positive(float $confidenceThreshold = 0.8): self |
| 60 | + { |
| 61 | + return new self(min: 0, confidenceThreshold: $confidenceThreshold); |
| 62 | + } |
| 63 | + |
| 64 | + public static function negative(float $confidenceThreshold = 0.8): self |
| 65 | + { |
| 66 | + return new self(max: 0, confidenceThreshold: $confidenceThreshold); |
| 67 | + } |
| 68 | + |
27 | 69 | public function type(): FieldType |
28 | 70 | { |
29 | 71 | return FieldType::Numeric; |
30 | 72 | } |
31 | 73 |
|
32 | 74 | public function name(): string |
33 | 75 | { |
34 | | - return FieldType::Numeric->value; |
| 76 | + $range = (null === $this->min && null === $this->max) |
| 77 | + ? '' : |
| 78 | + ( |
| 79 | + $this->min === $this->max |
| 80 | + ? '['.$this->min.']' |
| 81 | + : '['.$this->min.','.$this->max.']' |
| 82 | + ); |
| 83 | + |
| 84 | + return FieldType::Numeric->value.$range; |
35 | 85 | } |
36 | 86 |
|
37 | 87 | public function parse(mixed $value): int|float|null |
38 | 88 | { |
39 | | - if (is_float($value) || is_int($value)) { |
40 | | - return $value; |
| 89 | + if (is_string($value)) { |
| 90 | + $value = trim($value); |
| 91 | + if ('' === $value || !is_numeric($value)) { |
| 92 | + return null; |
| 93 | + } |
| 94 | + |
| 95 | + $filterValue = filter_var($value, FILTER_VALIDATE_INT); |
| 96 | + $value = false === $filterValue ? (float) $value : $filterValue; |
41 | 97 | } |
42 | 98 |
|
43 | | - if (!is_string($value)) { |
| 99 | + if (!is_float($value) && !is_int($value)) { |
44 | 100 | return null; |
45 | 101 | } |
46 | 102 |
|
47 | | - $value = trim($value); |
48 | | - if ('' === $value || !is_numeric($value)) { |
| 103 | + if (null !== $this->min && $value < $this->min) { |
49 | 104 | return null; |
50 | 105 | } |
51 | 106 |
|
52 | | - $filterValue = filter_var($value, FILTER_VALIDATE_INT); |
| 107 | + if (null !== $this->max && $value > $this->max) { |
| 108 | + return null; |
| 109 | + } |
53 | 110 |
|
54 | | - return false === $filterValue ? (float) $value : $filterValue; |
| 111 | + return $value; |
55 | 112 | } |
56 | 113 |
|
57 | 114 | public function metadata(): FieldMetadata |
58 | 115 | { |
59 | | - return new FieldMetadata(); |
| 116 | + return new FieldMetadata([ |
| 117 | + 'constraints' => [ |
| 118 | + 'min_value' => $this->min, |
| 119 | + 'max_value' => $this->max, |
| 120 | + ], |
| 121 | + ]); |
60 | 122 | } |
61 | 123 | } |
0 commit comments