Skip to content

Commit

Permalink
Merge pull request #2 from bamarni/normalize-geo-object
Browse files Browse the repository at this point in the history
allow normalization of geo objects with getters
  • Loading branch information
jsor committed Jan 4, 2016
2 parents e12bf0c + d0f67d8 commit 7592071
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/LatLng.php
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,9 @@ public static function normalize($input)
if (is_string($input) && preg_match('/(\-?\d+\.?\d*)[, ] ?(\-?\d+\.?\d*)$/', $input, $match)) {
$lat = $match[1];
$lng = $match[2];
} elseif (is_object($input) && method_exists($input, 'getLatitude') && method_exists($input, 'getLongitude')) {
$lat = $input->getLatitude();
$lng = $input->getLongitude();
} elseif (is_array($input) || $input instanceof \ArrayAccess) {
if (Utils::isNumericInputArray($input)) {
$lat = $input[0];
Expand Down
25 changes: 25 additions & 0 deletions tests/Fixtures/ThirdPartyLatLng.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace Geokit\Fixtures;

class ThirdPartyLatLng
{
private $lat;
private $lng;

public function __construct($lat, $lng)
{
$this->lat = $lat;
$this->lng = $lng;
}

public function getLatitude()
{
return $this->lat;
}

public function getLongitude()
{
return $this->lng;
}
}
12 changes: 12 additions & 0 deletions tests/LngLatTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Geokit;

use Geokit\Fixtures\ThirdPartyLatLng;

class LngLatTest extends \PHPUnit_Framework_TestCase
{
public function testConstructorShouldAcceptStringsAsArguments()
Expand Down Expand Up @@ -179,6 +181,16 @@ public function testNormalizeShouldAcceptIndexedArrayArgument()
$this->assertSame(1.1234, $LatLng->getLongitude());
}

public function testNormalizeShouldAcceptObjectWithLatLngGetters()
{
$thirdPartyLatLng = new ThirdPartyLatLng(2.5678, 1.1234);

$LatLng = LatLng::normalize($thirdPartyLatLng);

$this->assertSame(2.5678, $LatLng->getLatitude());
$this->assertSame(1.1234, $LatLng->getLongitude());
}

/**
* @dataProvider normalizeLatDataProvider
*/
Expand Down

0 comments on commit 7592071

Please sign in to comment.