Skip to content

Commit

Permalink
Fix and improve polygon with assoc point array
Browse files Browse the repository at this point in the history
  • Loading branch information
jsor committed Jul 5, 2016
1 parent 53a6df0 commit 931be95
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 5 deletions.
4 changes: 2 additions & 2 deletions src/Polygon.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public function close()
$points = $this->points;

if (!$this->isClosed()) {
$points[] = clone $this->points[0];
$points[] = clone reset($this->points);
}

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

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

$x0 = $p->getLongitude();
$y0 = $p->getLatitude();
Expand Down
30 changes: 27 additions & 3 deletions tests/PolygonTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ public function testConstructorShouldAcceptArrayOfLatLngs()
new LatLng(0, 0),
array('latitude' => 0, 'longitude' => 1),
'1, 1',
array(1, 0)
'key' => array(1, 0)
);

$polygon = new Polygon($points);

$this->assertEquals($points[0], $polygon[0]);
$this->assertEquals(0, $polygon[1]->getLatitude());
$this->assertEquals(1, $polygon[2]->getLatitude());
$this->assertEquals(1, $polygon[3]->getLatitude());
$this->assertEquals(1, $polygon['key']->getLatitude());
}

public function testConstructorThrowsExceptionForInvalidLatLng()
Expand Down Expand Up @@ -224,6 +224,17 @@ public function containsDataProvider()
array('lng' => 0.5, 'lat' => 0.5),
false
),

// Assoc polygon
array(
array(
'polygon1' => array('lng' => 1, 'lat' => 1),
'polygon2' => array('lng' => 3, 'lat' => 2),
'polygon3' => array('lng' => 2, 'lat' => 3)
),
array('lng' => 1.5, 'lat' => 1.5),
true
),
);
}

Expand Down Expand Up @@ -255,7 +266,7 @@ public function testToBoundsThrowsExceptionForEmptyPolygon()
$polygon->toBounds();
}

public function testArrayAccess()
public function testArrayAccessNumeric()
{
$points = array(
new LatLng(0, 0)
Expand All @@ -268,6 +279,19 @@ public function testArrayAccess()
$this->assertEquals($points[0], $polygon[0]);
}

public function testArrayAccessAssoc()
{
$points = array(
'key' => new LatLng(0, 0)
);

$polygon = new Polygon($points);

$this->assertTrue(isset($polygon['key']));
$this->assertNotNull($polygon['key']);
$this->assertEquals($points['key'], $polygon['key']);
}

public function testOffsetGetThrowsExceptionForInvalidKey()
{
$this->setExpectedException('\InvalidArgumentException', 'Invalid offset 0.');
Expand Down

0 comments on commit 931be95

Please sign in to comment.