1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* This file is part of Wszetko Sitemap. |
7
|
|
|
* |
8
|
|
|
* (c) Paweł Kłopotek-Główczewski <[email protected]> |
9
|
|
|
* |
10
|
|
|
* This source file is subject to the MIT license that is bundled |
11
|
|
|
* with this source code in the file LICENSE. |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Wszetko\Sitemap\Items\DataTypes; |
15
|
|
|
|
16
|
|
|
use Wszetko\Sitemap\Interfaces\DataType; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Class FloatType. |
20
|
|
|
* |
21
|
|
|
* @package Wszetko\Sitemap\Items\DataTypes |
22
|
|
|
*/ |
23
|
|
|
class FloatType extends AbstractDataType |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* Number of decimal digits. |
27
|
|
|
* |
28
|
|
|
* @var int |
29
|
|
|
*/ |
30
|
|
|
protected $precision = 0; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Minimal value that DataType can handle. |
34
|
|
|
* |
35
|
|
|
* @var null|float|int |
36
|
|
|
*/ |
37
|
|
|
private $minValue; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Maximum value that DataType can handle. |
41
|
|
|
* |
42
|
|
|
* @var null|float|int |
43
|
|
|
*/ |
44
|
|
|
private $maxValue; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Return minimal value that DataType can handle. |
48
|
|
|
* |
49
|
|
|
* @return null|float |
50
|
|
|
*/ |
51
|
108 |
|
public function getMinValue(): ?float |
52
|
|
|
{ |
53
|
108 |
|
return $this->minValue; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Set minimal value that DataType can handle. |
58
|
|
|
* |
59
|
|
|
* @param float $minValue |
60
|
|
|
* |
61
|
|
|
* @return \Wszetko\Sitemap\Items\DataTypes\FloatType |
62
|
|
|
*/ |
63
|
400 |
|
public function setMinValue(float $minValue): self |
64
|
|
|
{ |
65
|
400 |
|
$this->minValue = $minValue; |
66
|
|
|
|
67
|
400 |
|
return $this; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Return maximum value that DataType can handle. |
72
|
|
|
* |
73
|
|
|
* @return null|float |
74
|
|
|
*/ |
75
|
96 |
|
public function getMaxValue(): ?float |
76
|
|
|
{ |
77
|
96 |
|
return $this->maxValue; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Set maximum value that DataType can handle. |
82
|
|
|
* |
83
|
|
|
* @param float $maxValue |
84
|
|
|
* |
85
|
|
|
* @return \Wszetko\Sitemap\Items\DataTypes\FloatType |
86
|
|
|
*/ |
87
|
400 |
|
public function setMaxValue(float $maxValue): self |
88
|
|
|
{ |
89
|
400 |
|
$this->maxValue = $maxValue; |
90
|
|
|
|
91
|
400 |
|
return $this; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Return number of decimal digits to use in DataType. |
96
|
|
|
* |
97
|
|
|
* @return int |
98
|
|
|
*/ |
99
|
84 |
|
public function getPrecision(): int |
100
|
|
|
{ |
101
|
84 |
|
return $this->precision; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Set number of decimal digits to use in DataType. |
106
|
|
|
* |
107
|
|
|
* @param int $precision |
108
|
|
|
* |
109
|
|
|
* @return \Wszetko\Sitemap\Items\DataTypes\FloatType |
110
|
|
|
*/ |
111
|
400 |
|
public function setPrecision(int $precision): self |
112
|
|
|
{ |
113
|
400 |
|
$this->precision = $precision; |
114
|
|
|
|
115
|
400 |
|
return $this; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* @inheritDoc |
120
|
|
|
*/ |
121
|
132 |
|
public function setValue($value, $parameters = []): DataType |
122
|
|
|
{ |
123
|
132 |
|
if (null === $value) { |
124
|
8 |
|
$this->value = null; |
125
|
|
|
|
126
|
8 |
|
return $this; |
127
|
|
|
} |
128
|
|
|
|
129
|
124 |
|
if (!is_numeric($value)) { |
130
|
16 |
|
return $this; |
131
|
|
|
} |
132
|
|
|
|
133
|
108 |
|
$value = (float) $value; |
134
|
|
|
|
135
|
108 |
|
if (null !== $this->getMinValue() && $value < $this->getMinValue()) { |
136
|
12 |
|
return $this; |
137
|
|
|
} |
138
|
|
|
|
139
|
96 |
|
if (null !== $this->getMaxValue() && $value > $this->getMaxValue()) { |
140
|
12 |
|
return $this; |
141
|
|
|
} |
142
|
|
|
|
143
|
84 |
|
$value = round($value, $this->getPrecision()); |
144
|
84 |
|
$value = number_format($value, $this->getPrecision(), '.', ''); |
145
|
|
|
|
146
|
84 |
|
parent::setValue($value, $parameters); |
147
|
|
|
|
148
|
84 |
|
return $this; |
149
|
|
|
} |
150
|
|
|
} |
151
|
|
|
|