1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ValueObjects\Geography; |
4
|
|
|
|
5
|
|
|
use ValueObjects\StringLiteral\StringLiteral; |
6
|
|
|
use ValueObjects\Util\Util; |
7
|
|
|
use ValueObjects\ValueObjectInterface; |
8
|
|
|
|
9
|
|
|
class Street implements ValueObjectInterface |
10
|
|
|
{ |
11
|
|
|
/** @var StringLiteral */ |
12
|
|
|
protected $name; |
13
|
|
|
|
14
|
|
|
/** @var StringLiteral */ |
15
|
|
|
protected $number; |
16
|
|
|
|
17
|
|
|
/** @var StringLiteral Building, floor and unit */ |
18
|
|
|
protected $elements; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var StringLiteral __toString() format |
22
|
|
|
* Use properties corresponding placeholders: %name%, %number%, %elements% |
23
|
|
|
*/ |
24
|
|
|
protected $format; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Returns a new Street from native PHP string name and number. |
28
|
|
|
* |
29
|
|
|
* @param string $name |
|
|
|
|
30
|
|
|
* @param string $number |
|
|
|
|
31
|
|
|
* @param string $elements |
|
|
|
|
32
|
|
|
* |
33
|
|
|
* @return Street |
34
|
|
|
* @throws \BadFunctionCallException |
35
|
|
|
*/ |
36
|
2 |
|
public static function fromNative() |
37
|
|
|
{ |
38
|
2 |
|
$args = func_get_args(); |
39
|
|
|
|
40
|
2 |
|
if (\count($args) < 2) { |
41
|
1 |
|
throw new \BadMethodCallException('You must provide from 2 to 4 arguments: 1) street name, 2) street number, 3) elements, 4) format (optional)'); |
42
|
|
|
} |
43
|
|
|
|
44
|
1 |
|
$nameString = $args[0]; |
45
|
1 |
|
$numberString = $args[1]; |
46
|
1 |
|
$elementsString = isset($args[2]) ? $args[2] : NULL; |
47
|
1 |
|
$formatString = isset($args[3]) ? $args[3] : NULL; |
48
|
|
|
|
49
|
1 |
|
$name = new StringLiteral($nameString); |
50
|
1 |
|
$number = new StringLiteral($numberString); |
51
|
1 |
|
$elements = $elementsString ? new StringLiteral($elementsString) : NULL; |
52
|
1 |
|
$format = $formatString ? new StringLiteral($formatString) : NULL; |
53
|
|
|
|
54
|
1 |
|
return new static($name, $number, $elements, $format); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Returns a new Street object |
59
|
|
|
* |
60
|
|
|
* @param StringLiteral $name |
61
|
|
|
* @param StringLiteral $number |
62
|
|
|
*/ |
63
|
18 |
|
public function __construct(StringLiteral $name, StringLiteral $number, StringLiteral $elements = NULL, StringLiteral $format = NULL) |
64
|
|
|
{ |
65
|
18 |
|
$this->name = $name; |
66
|
18 |
|
$this->number = $number; |
67
|
|
|
|
68
|
18 |
|
if ($elements === NULL) { |
69
|
12 |
|
$elements = new StringLiteral(''); |
70
|
12 |
|
} |
71
|
18 |
|
$this->elements = $elements; |
72
|
|
|
|
73
|
18 |
|
if ($format === NULL) { |
74
|
13 |
|
$format = new StringLiteral('%number% %name%'); |
75
|
13 |
|
} |
76
|
18 |
|
$this->format = $format; |
77
|
18 |
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Tells whether two Street objects are equal |
81
|
|
|
* |
82
|
|
|
* @param ValueObjectInterface $street |
83
|
|
|
* |
84
|
|
|
* @return bool |
85
|
|
|
*/ |
86
|
5 |
|
public function sameValueAs(ValueObjectInterface $street) |
87
|
|
|
{ |
88
|
5 |
|
if (FALSE === Util::classEquals($this, $street)) { |
89
|
1 |
|
return FALSE; |
90
|
|
|
} |
91
|
|
|
|
92
|
5 |
|
return $this->getName()->sameValueAs($street->getName()) && |
|
|
|
|
93
|
5 |
|
$this->getNumber()->sameValueAs($street->getNumber()) && |
|
|
|
|
94
|
5 |
|
$this->getElements()->sameValueAs($street->getElements()) |
|
|
|
|
95
|
5 |
|
; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Returns street name |
100
|
|
|
* |
101
|
|
|
* @return StringLiteral |
102
|
|
|
*/ |
103
|
8 |
|
public function getName() |
104
|
|
|
{ |
105
|
8 |
|
return clone $this->name; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Returns street number |
110
|
|
|
* |
111
|
|
|
* @return StringLiteral |
112
|
|
|
*/ |
113
|
8 |
|
public function getNumber() |
114
|
|
|
{ |
115
|
8 |
|
return clone $this->number; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Returns street elements |
120
|
|
|
* @return StringLiteral |
121
|
|
|
*/ |
122
|
8 |
|
public function getElements() |
123
|
|
|
{ |
124
|
8 |
|
return clone $this->elements; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Returns a string representation of the StringLiteral in the format defined in the constructor |
129
|
|
|
* |
130
|
|
|
* @return string |
131
|
|
|
*/ |
132
|
2 |
|
public function __toString() |
133
|
|
|
{ |
134
|
|
|
$replacements = [ |
135
|
2 |
|
"%name%" => $this->getName(), |
136
|
2 |
|
"%number%" => $this->getNumber(), |
137
|
2 |
|
"%elements%" => $this->getElements(), |
138
|
2 |
|
]; |
139
|
|
|
|
140
|
2 |
|
$streetString = str_replace(array_keys($replacements), array_values($replacements), $this->format); |
141
|
|
|
|
142
|
2 |
|
return $streetString; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
function jsonSerialize() |
|
|
|
|
146
|
|
|
{ |
147
|
|
|
return [ |
148
|
|
|
'parts' => [ |
149
|
|
|
'name' => $this->getName(), |
150
|
|
|
'number' => $this->getNumber(), |
151
|
|
|
'elements' => $this->getElements(), |
152
|
|
|
], |
153
|
|
|
'formatted' => (string)$this, |
154
|
|
|
]; |
155
|
|
|
|
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
|
159
|
|
|
} |
160
|
|
|
|
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italy
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was removed, but the annotation was not.