1 | <?php |
||
20 | class BoundingBox implements BoundingBoxInterface |
||
21 | { |
||
22 | /** |
||
23 | * @var PolygonInterface |
||
24 | */ |
||
25 | private $polygon; |
||
26 | |||
27 | /** |
||
28 | * The latitude of the north coordinate |
||
29 | * |
||
30 | * @var float|string|integer |
||
31 | */ |
||
32 | private $north; |
||
33 | |||
34 | /** |
||
35 | * The longitude of the east coordinate |
||
36 | * |
||
37 | * @var float|string|integer |
||
38 | */ |
||
39 | private $east; |
||
40 | |||
41 | /** |
||
42 | * The latitude of the south coordinate |
||
43 | * |
||
44 | * @var float|string|integer |
||
45 | */ |
||
46 | private $south; |
||
47 | |||
48 | /** |
||
49 | * The longitude of the west coordinate |
||
50 | * |
||
51 | * @var float|string|integer |
||
52 | */ |
||
53 | private $west; |
||
54 | |||
55 | /** |
||
56 | * @var boolean |
||
57 | */ |
||
58 | private $hasCoordinate = false; |
||
59 | |||
60 | /** |
||
61 | * @var integer |
||
62 | */ |
||
63 | private $precision = 8; |
||
64 | |||
65 | /** |
||
66 | * @param PolygonInterface|CoordinateInterface $object |
||
67 | */ |
||
68 | 15 | public function __construct($object = null) |
|
69 | { |
||
70 | 15 | if ($object instanceof PolygonInterface) { |
|
71 | 15 | $this->setPolygon($object); |
|
72 | } elseif ($object instanceof CoordinateInterface) { |
||
73 | 1 | $this->addCoordinate($object); |
|
74 | 2 | } elseif (null !== $object) { |
|
75 | 1 | throw new \InvalidArgumentException; |
|
76 | } |
||
77 | 15 | } |
|
78 | |||
79 | 15 | private function createBoundingBoxForPolygon() |
|
80 | { |
||
81 | 15 | $this->hasCoordinate = false; |
|
82 | 15 | $this->west = $this->east = $this->north = $this->south = null; |
|
83 | 15 | foreach ($this->polygon->getCoordinates() as $coordinate) { |
|
84 | 9 | $this->addCoordinate($coordinate); |
|
85 | } |
||
86 | 15 | } |
|
87 | |||
88 | /** |
||
89 | * @param CoordinateInterface $coordinate |
||
90 | */ |
||
91 | 10 | private function addCoordinate(CoordinateInterface $coordinate) |
|
92 | { |
||
93 | 10 | $latitude = $coordinate->getLatitude(); |
|
94 | 10 | $longitude = $coordinate->getLongitude(); |
|
95 | |||
96 | 10 | if (!$this->hasCoordinate) { |
|
97 | 10 | $this->setNorth($latitude); |
|
98 | 10 | $this->setSouth($latitude); |
|
99 | 10 | $this->setEast($longitude); |
|
100 | 10 | $this->setWest($longitude); |
|
101 | } else { |
||
102 | 9 | if (bccomp($latitude, $this->getSouth(), $this->getPrecision()) === -1) { |
|
103 | 9 | $this->setSouth($latitude); |
|
104 | } |
||
105 | 9 | if (bccomp($latitude, $this->getNorth(), $this->getPrecision()) === 1) { |
|
106 | 9 | $this->setNorth($latitude); |
|
107 | } |
||
108 | 9 | if (bccomp($longitude, $this->getEast(), $this->getPrecision()) === 1) { |
|
109 | 9 | $this->setEast($longitude); |
|
110 | } |
||
111 | 9 | if (bccomp($longitude, $this->getWest(), $this->getPrecision()) === -1) { |
|
112 | $this->setWest($longitude); |
||
113 | } |
||
114 | } |
||
115 | |||
116 | 10 | $this->hasCoordinate = true; |
|
117 | 10 | } |
|
118 | |||
119 | /** |
||
120 | * @param CoordinateInterface $coordinate |
||
121 | * @return bool |
||
122 | */ |
||
123 | 2 | public function pointInBoundingBox(CoordinateInterface $coordinate) |
|
124 | { |
||
125 | if ( |
||
126 | 2 | bccomp($coordinate->getLatitude(), $this->getSouth(), $this->getPrecision()) === -1 || |
|
127 | 2 | bccomp($coordinate->getLatitude(), $this->getNorth(), $this->getPrecision()) === 1 || |
|
128 | 2 | bccomp($coordinate->getLongitude(), $this->getEast(), $this->getPrecision()) === 1 || |
|
129 | 2 | bccomp($coordinate->getLongitude(), $this->getWest(), $this->getPrecision()) === -1 |
|
130 | ) { |
||
131 | 1 | return false; |
|
132 | } |
||
133 | |||
134 | 1 | return true; |
|
135 | } |
||
136 | |||
137 | /** |
||
138 | * @return PolygonInterface |
||
139 | */ |
||
140 | public function getPolygon() |
||
144 | |||
145 | /** |
||
146 | * @param PolygonInterface $polygon |
||
147 | * @return $this |
||
148 | */ |
||
149 | 15 | public function setPolygon(PolygonInterface $polygon) |
|
156 | |||
157 | /** |
||
158 | * {@inheritDoc} |
||
159 | */ |
||
160 | 9 | public function getNorth() |
|
164 | |||
165 | /** |
||
166 | * @param float|string|integer $north |
||
167 | * @return $this |
||
168 | */ |
||
169 | 10 | public function setNorth($north) |
|
175 | |||
176 | /** |
||
177 | * {@inheritDoc} |
||
178 | */ |
||
179 | 9 | public function getEast() |
|
183 | |||
184 | /** |
||
185 | * @param float|string|integer $east |
||
186 | * @return $this |
||
187 | */ |
||
188 | 10 | public function setEast($east) |
|
194 | |||
195 | /** |
||
196 | * {@inheritDoc} |
||
197 | */ |
||
198 | 9 | public function getSouth() |
|
202 | |||
203 | /** |
||
204 | * @param float|string|integer $south |
||
205 | * @return $this |
||
206 | */ |
||
207 | 10 | public function setSouth($south) |
|
213 | |||
214 | /** |
||
215 | * {@inheritDoc} |
||
216 | */ |
||
217 | 9 | public function getWest() |
|
221 | |||
222 | /** |
||
223 | * @param float|string|integer $west |
||
224 | * @return $this |
||
225 | */ |
||
226 | 10 | public function setWest($west) |
|
232 | |||
233 | /** |
||
234 | * @return integer |
||
235 | */ |
||
236 | 9 | public function getPrecision() |
|
240 | |||
241 | /** |
||
242 | * @param integer $precision |
||
243 | * @return $this |
||
244 | */ |
||
245 | public function setPrecision($precision) |
||
251 | } |
||
252 |