1
|
|
|
<?php |
2
|
|
|
namespace AlgoWeb\xsdTypes\Facets; |
3
|
|
|
|
4
|
|
|
//TODO: we need to extend this to handle dates. |
5
|
|
|
trait MinMaxTrait |
6
|
|
|
{ |
7
|
|
|
/** |
8
|
|
|
* @Exclude |
9
|
|
|
* @var integer|double|\DateTime Specifies the lower bounds for numeric values (the value must be greater than or equal to this value) |
10
|
|
|
*/ |
11
|
|
|
private $minInclusive = null; |
12
|
|
|
/** |
13
|
|
|
* @Exclude |
14
|
|
|
* @var integer|double|\DateTime Specifies the upper bounds for numeric values (the value must be less than or equal to this value) |
15
|
|
|
*/ |
16
|
|
|
private $maxInclusive = null; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @param integer|double|\DateTime $v Specifies the upper bounds for numeric values (the value must be less than this value) |
20
|
|
|
*/ |
21
|
|
View Code Duplication |
public function setMaxExclusive($v) |
|
|
|
|
22
|
|
|
{ |
23
|
|
|
if (is_int($v)) { |
24
|
|
|
$this->maxInclusive = $v - 1; |
25
|
|
|
} else { |
26
|
|
|
$this->minInclusive = $v - 0.000001; |
27
|
|
|
} |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @param integer|double|\DateTime $v Specifies the upper bounds for numeric values (the value must be less than or equal to this value) |
32
|
|
|
*/ |
33
|
|
|
public function setMaxInclusive($v) |
34
|
|
|
{ |
35
|
|
|
$this->maxInclusive = $v; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @param integer|double|\DateTime $v Specifies the lower bounds for numeric values (the value must be greater than this value) |
40
|
|
|
*/ |
41
|
|
View Code Duplication |
public function setMinExclusive($v) |
|
|
|
|
42
|
|
|
{ |
43
|
|
|
if (is_int($v)) { |
44
|
|
|
$this->minInclusive = $v + 1; |
45
|
|
|
} else { |
46
|
|
|
$this->minInclusive = $v + 0.000001; |
47
|
|
|
} |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @param integer|double|\DateTime $v Specifies the lower bounds for numeric values (the value must be greater than or equal to this value) |
52
|
|
|
*/ |
53
|
|
|
public function setMinInclusive($v) |
54
|
|
|
{ |
55
|
|
|
$this->minInclusive = $v; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public function CheckMinMax($v) |
59
|
|
|
{ |
60
|
|
|
if (null != $this->minInclusive) { |
61
|
|
|
$this->CheckMin($v); |
62
|
|
|
} |
63
|
|
|
if (null != $this->maxInclusive) { |
64
|
|
|
$this->CheckMax($v); |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
private function CheckMin($v) |
69
|
|
|
{ |
70
|
|
|
if ($v < $this->minInclusive) { |
71
|
|
|
throw new \InvalidArgumentException("Value less than allowed min value " . __CLASS__); |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
private function CheckMax($v) |
76
|
|
|
{ |
77
|
|
|
if ($v > $this->maxInclusive) { |
78
|
|
|
throw new \InvalidArgumentException("Value greater than allowed max value " . __CLASS__); |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.