|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Maps\Elements; |
|
4
|
|
|
|
|
5
|
|
|
use DataValues\Geo\Values\LatLongValue; |
|
6
|
|
|
use InvalidArgumentException; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* @since 3.0 |
|
10
|
|
|
* |
|
11
|
|
|
* |
|
12
|
|
|
* @licence GNU GPL v2+ |
|
13
|
|
|
* @author Kim Eik < [email protected] > |
|
14
|
|
|
* @author Jeroen De Dauw < [email protected] > |
|
15
|
|
|
*/ |
|
16
|
|
|
class Rectangle extends \MapsBaseFillableElement { |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @since 3.0 |
|
20
|
|
|
* @var LatLongValue |
|
21
|
|
|
*/ |
|
22
|
|
|
protected $rectangleNorthEast; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @since 3.0 |
|
26
|
|
|
* @var LatLongValue |
|
27
|
|
|
*/ |
|
28
|
|
|
protected $rectangleSouthWest; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @since 3.0 |
|
32
|
|
|
* |
|
33
|
|
|
* @param LatLongValue $rectangleNorthEast |
|
34
|
|
|
* @param LatLongValue $rectangleSouthWest |
|
35
|
|
|
* |
|
36
|
|
|
* @throws InvalidArgumentException |
|
37
|
|
|
*/ |
|
38
|
|
|
public function __construct( LatLongValue $rectangleNorthEast, LatLongValue $rectangleSouthWest ) { |
|
39
|
|
|
if ( $rectangleNorthEast->equals( $rectangleSouthWest ) ) { |
|
40
|
|
|
throw new InvalidArgumentException( '$rectangleNorthEast cannot be equal to $rectangleSouthWest' ); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
parent::__construct(); |
|
44
|
|
|
|
|
45
|
|
|
// TODO: validate bounds are correct, if not, flip |
|
46
|
|
|
$this->setRectangleNorthEast( $rectangleNorthEast ); |
|
47
|
|
|
$this->setRectangleSouthWest( $rectangleSouthWest ); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @since 3.0 |
|
52
|
|
|
* |
|
53
|
|
|
* @return LatLongValue |
|
54
|
|
|
*/ |
|
55
|
|
|
public function getRectangleNorthEast() { |
|
56
|
|
|
return $this->rectangleNorthEast; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* @since 3.0 |
|
61
|
|
|
* |
|
62
|
|
|
* @return LatLongValue |
|
63
|
|
|
*/ |
|
64
|
|
|
public function getRectangleSouthWest() { |
|
65
|
|
|
return $this->rectangleSouthWest; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* @since 3.0 |
|
70
|
|
|
* |
|
71
|
|
|
* @param LatLongValue $rectangleSouthWest |
|
72
|
|
|
*/ |
|
73
|
|
|
public function setRectangleSouthWest( LatLongValue $rectangleSouthWest ) { |
|
74
|
|
|
$this->rectangleSouthWest = $rectangleSouthWest; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* @since 3.0 |
|
79
|
|
|
* |
|
80
|
|
|
* @param LatLongValue $rectangleNorthEast |
|
81
|
|
|
*/ |
|
82
|
|
|
public function setRectangleNorthEast( LatLongValue $rectangleNorthEast ) { |
|
83
|
|
|
$this->rectangleNorthEast = $rectangleNorthEast; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* @since 3.0 |
|
88
|
|
|
* |
|
89
|
|
|
* @param string $defText |
|
90
|
|
|
* @param string $defTitle |
|
91
|
|
|
* |
|
92
|
|
|
* @return array |
|
93
|
|
|
*/ |
|
94
|
|
|
public function getJSONObject( $defText = '' , $defTitle = '' ) { |
|
95
|
|
|
$parentArray = parent::getJSONObject( $defText , $defTitle ); |
|
96
|
|
|
$array = [ |
|
97
|
|
|
'ne' => [ |
|
98
|
|
|
'lon' => $this->getRectangleNorthEast()->getLongitude(), |
|
99
|
|
|
'lat' => $this->getRectangleNorthEast()->getLatitude() |
|
100
|
|
|
], |
|
101
|
|
|
'sw' => [ |
|
102
|
|
|
'lon' => $this->getRectangleSouthWest()->getLongitude(), |
|
103
|
|
|
'lat' => $this->getRectangleSouthWest()->getLatitude() |
|
104
|
|
|
], |
|
105
|
|
|
]; |
|
106
|
|
|
|
|
107
|
|
|
return array_merge( $parentArray , $array ); |
|
|
|
|
|
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
} |
|
111
|
|
|
|
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_functionexpects aPostobject, and outputs the author of the post. The base classPostreturns a simple string and outputting a simple string will work just fine. However, the child classBlogPostwhich is a sub-type ofPostinstead decided to return anobject, and is therefore violating the SOLID principles. If aBlogPostwere passed tomy_function, PHP would not complain, but ultimately fail when executing thestrtouppercall in its body.