|
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 Circle extends \MapsBaseFillableElement { |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @var LatLongValue |
|
20
|
|
|
*/ |
|
21
|
|
|
protected $circleCentre; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @var integer|float |
|
25
|
|
|
*/ |
|
26
|
|
|
protected $circleRadius; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @param LatLongValue $circleCentre |
|
30
|
|
|
* @param integer|float $circleRadius |
|
31
|
|
|
* |
|
32
|
|
|
* @throws InvalidArgumentException |
|
33
|
|
|
*/ |
|
34
|
7 |
|
public function __construct( LatLongValue $circleCentre, $circleRadius ) { |
|
35
|
7 |
|
if ( !is_float( $circleRadius ) && !is_int( $circleRadius ) ) { |
|
36
|
1 |
|
throw new InvalidArgumentException( '$circleRadius must be a float or int' ); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
6 |
|
if ( $circleRadius <= 0 ) { |
|
40
|
2 |
|
throw new InvalidArgumentException( '$circleRadius must be greater than zero' ); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
4 |
|
parent::__construct(); |
|
44
|
|
|
|
|
45
|
4 |
|
$this->setCircleCentre( $circleCentre ); |
|
46
|
4 |
|
$this->setCircleRadius( $circleRadius ); |
|
47
|
4 |
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @return LatLongValue |
|
51
|
|
|
*/ |
|
52
|
4 |
|
public function getCircleCentre() { |
|
53
|
4 |
|
return $this->circleCentre; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* @param LatLongValue $circleCentre |
|
58
|
|
|
*/ |
|
59
|
4 |
|
public function setCircleCentre( LatLongValue $circleCentre ) { |
|
60
|
4 |
|
$this->circleCentre = $circleCentre; |
|
61
|
4 |
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @return integer|float |
|
65
|
|
|
*/ |
|
66
|
4 |
|
public function getCircleRadius() { |
|
67
|
4 |
|
return $this->circleRadius; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* @param integer|float $circleRadius |
|
72
|
|
|
*/ |
|
73
|
4 |
|
public function setCircleRadius( $circleRadius ) { |
|
74
|
4 |
|
$this->circleRadius = $circleRadius; |
|
75
|
4 |
|
} |
|
76
|
|
|
|
|
77
|
|
|
public function getJSONObject( $defText = '' , $defTitle = '' ) { |
|
78
|
|
|
$parentArray = parent::getJSONObject( $defText , $defTitle ); |
|
79
|
|
|
|
|
80
|
|
|
$array = [ |
|
81
|
|
|
'centre' => [ |
|
82
|
|
|
'lon' => $this->getCircleCentre()->getLongitude(), |
|
83
|
|
|
'lat' => $this->getCircleCentre()->getLatitude() |
|
84
|
|
|
], |
|
85
|
|
|
'radius' => intval( $this->getCircleRadius() ), |
|
86
|
|
|
]; |
|
87
|
|
|
|
|
88
|
|
|
return array_merge( $parentArray, $array ); |
|
|
|
|
|
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
} |
|
92
|
|
|
|
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.