Skip to content

Commit 931be95

Browse files
committed
Fix and improve polygon with assoc point array
1 parent 53a6df0 commit 931be95

File tree

2 files changed

+29
-5
lines changed

2 files changed

+29
-5
lines changed

src/Polygon.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public function close()
3737
$points = $this->points;
3838

3939
if (!$this->isClosed()) {
40-
$points[] = clone $this->points[0];
40+
$points[] = clone reset($this->points);
4141
}
4242

4343
return new self($points);
@@ -59,7 +59,7 @@ public function contains(LatLng $latLng)
5959
$x = $latLng->getLongitude();
6060
$y = $latLng->getLatitude();
6161

62-
$p = $points[count($points) - 1];
62+
$p = end($points);
6363

6464
$x0 = $p->getLongitude();
6565
$y0 = $p->getLatitude();

tests/PolygonTest.php

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,15 @@ public function testConstructorShouldAcceptArrayOfLatLngs()
1010
new LatLng(0, 0),
1111
array('latitude' => 0, 'longitude' => 1),
1212
'1, 1',
13-
array(1, 0)
13+
'key' => array(1, 0)
1414
);
1515

1616
$polygon = new Polygon($points);
1717

1818
$this->assertEquals($points[0], $polygon[0]);
1919
$this->assertEquals(0, $polygon[1]->getLatitude());
2020
$this->assertEquals(1, $polygon[2]->getLatitude());
21-
$this->assertEquals(1, $polygon[3]->getLatitude());
21+
$this->assertEquals(1, $polygon['key']->getLatitude());
2222
}
2323

2424
public function testConstructorThrowsExceptionForInvalidLatLng()
@@ -224,6 +224,17 @@ public function containsDataProvider()
224224
array('lng' => 0.5, 'lat' => 0.5),
225225
false
226226
),
227+
228+
// Assoc polygon
229+
array(
230+
array(
231+
'polygon1' => array('lng' => 1, 'lat' => 1),
232+
'polygon2' => array('lng' => 3, 'lat' => 2),
233+
'polygon3' => array('lng' => 2, 'lat' => 3)
234+
),
235+
array('lng' => 1.5, 'lat' => 1.5),
236+
true
237+
),
227238
);
228239
}
229240

@@ -255,7 +266,7 @@ public function testToBoundsThrowsExceptionForEmptyPolygon()
255266
$polygon->toBounds();
256267
}
257268

258-
public function testArrayAccess()
269+
public function testArrayAccessNumeric()
259270
{
260271
$points = array(
261272
new LatLng(0, 0)
@@ -268,6 +279,19 @@ public function testArrayAccess()
268279
$this->assertEquals($points[0], $polygon[0]);
269280
}
270281

282+
public function testArrayAccessAssoc()
283+
{
284+
$points = array(
285+
'key' => new LatLng(0, 0)
286+
);
287+
288+
$polygon = new Polygon($points);
289+
290+
$this->assertTrue(isset($polygon['key']));
291+
$this->assertNotNull($polygon['key']);
292+
$this->assertEquals($points['key'], $polygon['key']);
293+
}
294+
271295
public function testOffsetGetThrowsExceptionForInvalidKey()
272296
{
273297
$this->setExpectedException('\InvalidArgumentException', 'Invalid offset 0.');

0 commit comments

Comments
 (0)