1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Ivory Google Map package. |
5
|
|
|
* |
6
|
|
|
* (c) Eric GELOEN <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please read the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Ivory\GoogleMap\Service\Elevation\Request; |
13
|
|
|
|
14
|
|
|
use Ivory\GoogleMap\Service\Base\Location\LocationInterface; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @author GeLo <[email protected]> |
18
|
|
|
*/ |
19
|
|
|
class PositionalElevationRequest implements ElevationRequestInterface |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* @var LocationInterface[] |
23
|
|
|
*/ |
24
|
|
|
private $locations = []; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @param LocationInterface[] $locations |
28
|
|
|
*/ |
29
|
|
|
public function __construct(array $locations) |
30
|
|
|
{ |
31
|
|
|
$this->setLocations($locations); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @return bool |
36
|
|
|
*/ |
37
|
|
|
public function hasLocations() |
38
|
|
|
{ |
39
|
|
|
return !empty($this->locations); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @return LocationInterface[] |
44
|
|
|
*/ |
45
|
|
|
public function getLocations() |
46
|
|
|
{ |
47
|
|
|
return $this->locations; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @param LocationInterface[] $locations |
52
|
|
|
*/ |
53
|
|
|
public function setLocations(array $locations) |
54
|
|
|
{ |
55
|
|
|
$this->locations = []; |
56
|
|
|
$this->addLocations($locations); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @param LocationInterface[] $locations |
61
|
|
|
*/ |
62
|
|
|
public function addLocations(array $locations) |
63
|
|
|
{ |
64
|
|
|
foreach ($locations as $location) { |
65
|
|
|
$this->addLocation($location); |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @param LocationInterface $location |
71
|
|
|
* |
72
|
|
|
* @return bool |
73
|
|
|
*/ |
74
|
|
|
public function hasLocation(LocationInterface $location) |
75
|
|
|
{ |
76
|
|
|
return in_array($location, $this->locations, true); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @param LocationInterface $location |
81
|
|
|
*/ |
82
|
|
|
public function addLocation(LocationInterface $location) |
83
|
|
|
{ |
84
|
|
|
if (!$this->hasLocation($location)) { |
85
|
|
|
$this->locations[] = $location; |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @param LocationInterface $location |
91
|
|
|
*/ |
92
|
|
|
public function removeLocation(LocationInterface $location) |
93
|
|
|
{ |
94
|
|
|
unset($this->locations[array_search($location, $this->locations, true)]); |
95
|
|
|
$this->locations = array_values($this->locations); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* {@inheritdoc} |
100
|
|
|
*/ |
101
|
|
|
public function build() |
102
|
|
|
{ |
103
|
|
|
return ['locations' => implode('|', array_map(function (LocationInterface $location) { |
|
|
|
|
104
|
|
|
return $location->build(); |
105
|
|
|
}, $this->locations))]; |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_function
expects aPost
object, and outputs the author of the post. The base classPost
returns a simple string and outputting a simple string will work just fine. However, the child classBlogPost
which is a sub-type ofPost
instead decided to return anobject
, and is therefore violating the SOLID principles. If aBlogPost
were passed tomy_function
, PHP would not complain, but ultimately fail when executing thestrtoupper
call in its body.