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
|
|
|
* @return Street |
33
|
|
|
* @throws \BadFunctionCallException |
34
|
|
|
*/ |
35
|
2 |
|
public static function fromNative() |
36
|
|
|
{ |
37
|
2 |
|
$args = func_get_args(); |
38
|
|
|
|
39
|
2 |
|
if (\count($args) < 2) { |
40
|
1 |
|
throw new \BadMethodCallException('You must provide from 2 to 4 arguments: 1) street name, 2) street number, 3) elements, 4) format (optional)'); |
41
|
|
|
} |
42
|
|
|
|
43
|
1 |
|
$nameString = $args[0]; |
44
|
1 |
|
$numberString = $args[1]; |
45
|
1 |
|
$elementsString = isset($args[2]) ? $args[2] : null; |
46
|
1 |
|
$formatString = isset($args[3]) ? $args[3] : null; |
47
|
|
|
|
48
|
1 |
|
$name = new StringLiteral($nameString); |
49
|
1 |
|
$number = new StringLiteral($numberString); |
50
|
1 |
|
$elements = $elementsString ? new StringLiteral($elementsString) : null; |
51
|
1 |
|
$format = $formatString ? new StringLiteral($formatString) : null; |
52
|
|
|
|
53
|
1 |
|
return new static($name, $number, $elements, $format); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Returns a new Street object |
58
|
|
|
* |
59
|
|
|
* @param StringLiteral $name |
60
|
|
|
* @param StringLiteral $number |
61
|
|
|
*/ |
62
|
18 |
|
public function __construct(StringLiteral $name, StringLiteral $number, StringLiteral $elements = null, StringLiteral $format = null) |
63
|
|
|
{ |
64
|
18 |
|
$this->name = $name; |
65
|
18 |
|
$this->number = $number; |
66
|
|
|
|
67
|
18 |
|
if ($elements === null) { |
68
|
12 |
|
$elements = new StringLiteral(''); |
69
|
12 |
|
} |
70
|
18 |
|
$this->elements = $elements; |
71
|
|
|
|
72
|
18 |
|
if ($format === null) { |
73
|
13 |
|
$format = new StringLiteral('%number% %name%'); |
74
|
13 |
|
} |
75
|
18 |
|
$this->format = $format; |
76
|
18 |
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Tells whether two Street objects are equal |
80
|
|
|
* @param ValueObjectInterface $street |
81
|
|
|
* @return bool |
82
|
|
|
*/ |
83
|
5 |
|
public function sameValueAs(ValueObjectInterface $street) |
84
|
|
|
{ |
85
|
5 |
|
if (false === Util::classEquals($this, $street)) { |
86
|
1 |
|
return false; |
87
|
|
|
} |
88
|
|
|
|
89
|
5 |
|
return $this->getName()->sameValueAs($street->getName()) && |
|
|
|
|
90
|
5 |
|
$this->getNumber()->sameValueAs($street->getNumber()) && |
|
|
|
|
91
|
5 |
|
$this->getElements()->sameValueAs($street->getElements()) |
|
|
|
|
92
|
5 |
|
; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Returns street name |
97
|
|
|
* |
98
|
|
|
* @return StringLiteral |
99
|
|
|
*/ |
100
|
8 |
|
public function getName() |
101
|
|
|
{ |
102
|
8 |
|
return clone $this->name; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Returns street number |
107
|
|
|
* |
108
|
|
|
* @return StringLiteral |
109
|
|
|
*/ |
110
|
8 |
|
public function getNumber() |
111
|
|
|
{ |
112
|
8 |
|
return clone $this->number; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Returns street elements |
117
|
|
|
* @return StringLiteral |
118
|
|
|
*/ |
119
|
8 |
|
public function getElements() |
120
|
|
|
{ |
121
|
8 |
|
return clone $this->elements; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Returns a string representation of the StringLiteral in the format defined in the constructor |
126
|
|
|
* |
127
|
|
|
* @return string |
128
|
|
|
*/ |
129
|
2 |
|
public function __toString() |
130
|
|
|
{ |
131
|
|
|
$replacements = array( |
132
|
2 |
|
"%name%" => $this->getName(), |
133
|
2 |
|
"%number%" => $this->getNumber(), |
134
|
2 |
|
"%elements%" => $this->getElements() |
135
|
2 |
|
); |
136
|
|
|
|
137
|
2 |
|
$streetString = str_replace(array_keys($replacements), array_values($replacements), $this->format); |
138
|
|
|
|
139
|
2 |
|
return $streetString; |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
|
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.