|
1
|
|
|
<?php |
|
2
|
|
|
namespace AlgoWeb\xsdTypes\Facets; |
|
3
|
|
|
|
|
4
|
|
|
trait DigitsTrait |
|
5
|
|
|
{ |
|
6
|
|
|
/** |
|
7
|
|
|
* @Exclude |
|
8
|
|
|
* @var int Specifies the maximum number of decimal places allowed. Must be equal to or greater than zero |
|
9
|
|
|
*/ |
|
10
|
|
|
private $fractionDigits = null; |
|
11
|
|
|
/** |
|
12
|
|
|
* @Exclude |
|
13
|
|
|
* @var int Specifies the exact number of digits allowed. Must be greater than zero |
|
14
|
|
|
*/ |
|
15
|
|
|
private $totalDigits = null; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* @param int $fd Specifies the exact number of digits allowed. Must be greater than zero |
|
19
|
|
|
*/ |
|
20
|
|
|
public function setTotalDigits($fd) |
|
|
|
|
|
|
21
|
|
|
{ |
|
22
|
|
|
$this->checkDigitLength($fd); |
|
23
|
|
|
$this->totalDigits = $fd; |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @param int $fd |
|
28
|
|
|
*/ |
|
29
|
|
|
private function checkDigitLength($fd) |
|
|
|
|
|
|
30
|
|
|
{ |
|
31
|
|
|
if (!is_numeric($fd)) { |
|
32
|
|
|
throw new \InvalidArgumentException( |
|
33
|
|
|
'The provided fractionDigits for ' . get_class($this) . ' is non numeric.' |
|
34
|
|
|
); |
|
35
|
|
|
} |
|
36
|
|
|
if (abs($fd) != $fd) { |
|
37
|
|
|
throw new \InvalidArgumentException( |
|
38
|
|
|
'The provided fractionDigits for ' . get_class($this) . ' must be non negative.' |
|
39
|
|
|
); |
|
40
|
|
|
} |
|
41
|
|
|
if (round($fd) != $fd) { |
|
42
|
|
|
throw new \InvalidArgumentException( |
|
43
|
|
|
'The provided fractionDigits for ' . get_class($this) . ' must be a whole number.' |
|
44
|
|
|
); |
|
45
|
|
|
} |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
View Code Duplication |
public function checkTotalDigits($v) |
|
|
|
|
|
|
49
|
|
|
{ |
|
50
|
|
|
if (null == $this->totalDigits) { |
|
51
|
|
|
return; |
|
52
|
|
|
} |
|
53
|
|
|
$stringVal = explode('.', (string)$v); |
|
54
|
|
|
if ($this->totalDigits < strlen($stringVal[0])) { |
|
55
|
|
|
throw new \InvalidArgumentException( |
|
56
|
|
|
'The number of fractionDigits for ' . get_class($this) . ' is greater than allowed.' |
|
57
|
|
|
); |
|
58
|
|
|
} |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* @param int $fd Specifies the maximum number of decimal places allowed. Must be equal to or greater than zero |
|
63
|
|
|
*/ |
|
64
|
|
|
public function setFractionDigits($fd) |
|
|
|
|
|
|
65
|
|
|
{ |
|
66
|
|
|
$this->checkDigitLength($fd); |
|
67
|
|
|
$this->fractionDigits = $fd; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Checks the number of fraction digits to verify they are not longer then expected. |
|
72
|
|
|
* |
|
73
|
|
|
* @param float $v the value to check fraction digits on |
|
74
|
|
|
*/ |
|
75
|
|
View Code Duplication |
public function checkFractionDigits($v) |
|
|
|
|
|
|
76
|
|
|
{ |
|
77
|
|
|
if (null == $this->fractionDigits) { |
|
78
|
|
|
return; |
|
79
|
|
|
} |
|
80
|
|
|
$stringVal = explode('.', (string)$v); |
|
81
|
|
|
if (2 == count($stringVal)) { |
|
82
|
|
|
if ($this->fractionDigits < strlen($stringVal[1])) { |
|
83
|
|
|
throw new \InvalidArgumentException( |
|
84
|
|
|
'The number of fractionDigits for ' . get_class($this) . ' is greater than allowed.' |
|
85
|
|
|
); |
|
86
|
|
|
} |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* @param float $v the digit to be fixed |
|
92
|
|
|
* |
|
93
|
|
|
* @return float |
|
94
|
|
|
*/ |
|
95
|
|
|
public function fixFractionDigits($v) |
|
|
|
|
|
|
96
|
|
|
{ |
|
97
|
|
|
if (null != $this->fractionDigits) { |
|
98
|
|
|
return round($v, $this->fractionDigits); |
|
99
|
|
|
} |
|
100
|
|
|
return $v; |
|
101
|
|
|
} |
|
102
|
|
|
} |
|
103
|
|
|
|
Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.