1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace CultuurNet\UDB3\Offer; |
4
|
|
|
|
5
|
|
|
use CultuurNet\UDB3\Model\ValueObject\Audience\AgeRange as Udb3ModelAgeRange; |
6
|
|
|
use ValueObjects\Person\Age; |
7
|
|
|
|
8
|
|
|
class AgeRange |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* @var null|Age |
12
|
|
|
*/ |
13
|
|
|
private $from; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @var null|Age |
17
|
|
|
*/ |
18
|
|
|
private $to; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* AgeRange constructor. |
22
|
|
|
* @param null|Age $from |
23
|
|
|
* @param null|Age $to |
24
|
|
|
*/ |
25
|
|
|
public function __construct(Age $from = null, Age $to = null) |
26
|
|
|
{ |
27
|
|
|
$from = $from ?: new Age(0); |
28
|
|
|
|
29
|
|
|
$this->guardValidAgeRange($from, $to); |
30
|
|
|
|
31
|
|
|
$this->from = $from; |
32
|
|
|
$this->to = $to; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @param Age $from |
37
|
|
|
* @param null|Age $to |
38
|
|
|
* |
39
|
|
|
* @throws InvalidAgeRangeException |
40
|
|
|
*/ |
41
|
|
|
private function guardValidAgeRange(Age $from, Age $to = null) |
42
|
|
|
{ |
43
|
|
|
if ($from && $to && $from > $to) { |
44
|
|
|
throw new InvalidAgeRangeException('"from" age should not exceed "to" age'); |
45
|
|
|
} |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @return Age |
50
|
|
|
*/ |
51
|
|
|
public function getFrom() |
52
|
|
|
{ |
53
|
|
|
return $this->from; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @return null|Age |
58
|
|
|
*/ |
59
|
|
|
public function getTo() |
60
|
|
|
{ |
61
|
|
|
return $this->to; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @return string |
66
|
|
|
*/ |
67
|
|
|
public function __toString() |
68
|
|
|
{ |
69
|
|
|
$from = $this->from ? (string) $this->from : ''; |
70
|
|
|
$to = $this->to ? (string) $this->to : ''; |
71
|
|
|
|
72
|
|
|
return $from . '-' . $to; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @param string $ageRangeString |
77
|
|
|
* @return AgeRange |
78
|
|
|
* |
79
|
|
|
* @throws InvalidAgeRangeException |
80
|
|
|
*/ |
81
|
|
|
public static function fromString($ageRangeString) |
82
|
|
|
{ |
83
|
|
|
if (!is_string($ageRangeString)) { |
84
|
|
|
throw new InvalidAgeRangeException( |
85
|
|
|
'Date-range should be of type string.' |
86
|
|
|
); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
$stringValues = explode('-', $ageRangeString); |
90
|
|
|
|
91
|
|
|
if (empty($stringValues) || !isset($stringValues[1])) { |
92
|
|
|
throw new InvalidAgeRangeException( |
93
|
|
|
'Date-range string is not valid because it is missing a hyphen.' |
94
|
|
|
); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
if (count($stringValues) !== 2) { |
98
|
|
|
throw new InvalidAgeRangeException( |
99
|
|
|
'Date-range string is not valid because it has too many hyphens.' |
100
|
|
|
); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
$fromString = $stringValues[0]; |
104
|
|
|
$toString = $stringValues[1]; |
105
|
|
|
|
106
|
|
View Code Duplication |
if (is_numeric($fromString) || empty($fromString)) { |
|
|
|
|
107
|
|
|
$from = is_numeric($fromString) ? new Age($fromString) : null; |
108
|
|
|
} else { |
109
|
|
|
throw new InvalidAgeRangeException( |
110
|
|
|
'The "from" age should be a natural number or empty.' |
111
|
|
|
); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
View Code Duplication |
if (is_numeric($toString) || empty($toString)) { |
|
|
|
|
115
|
|
|
$to = is_numeric($toString) ? new Age($toString) : null; |
116
|
|
|
} else { |
117
|
|
|
throw new InvalidAgeRangeException( |
118
|
|
|
'The "to" age should be a natural number or empty.' |
119
|
|
|
); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
return new self($from, $to); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* @param AgeRange $otherAgeRange |
127
|
|
|
* @return bool |
128
|
|
|
*/ |
129
|
|
|
public function sameAs(AgeRange $otherAgeRange) |
130
|
|
|
{ |
131
|
|
|
return "$this" === "$otherAgeRange"; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* @param Udb3ModelAgeRange $udb3ModelAgeRange |
136
|
|
|
* @return AgeRange |
137
|
|
|
*/ |
138
|
|
|
public static function fromUbd3ModelAgeRange(Udb3ModelAgeRange $udb3ModelAgeRange) |
139
|
|
|
{ |
140
|
|
|
$from = null; |
|
|
|
|
141
|
|
|
if ($from = $udb3ModelAgeRange->getFrom()) { |
142
|
|
|
$from = new Age($from->toInteger()); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
$to = null; |
|
|
|
|
146
|
|
|
if ($to = $udb3ModelAgeRange->getTo()) { |
147
|
|
|
$to = new Age($to->toInteger()); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
return new AgeRange($from, $to); |
151
|
|
|
} |
152
|
|
|
} |
153
|
|
|
|
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.