From d0f67d894279e5712e948c8c64adf11fbf9fb94d Mon Sep 17 00:00:00 2001 From: Bilal Amarni Date: Mon, 4 Jan 2016 12:03:34 +0100 Subject: [PATCH] allow normalization of geo objects with getters --- src/LatLng.php | 3 +++ tests/Fixtures/ThirdPartyLatLng.php | 25 +++++++++++++++++++++++++ tests/LngLatTest.php | 12 ++++++++++++ 3 files changed, 40 insertions(+) create mode 100644 tests/Fixtures/ThirdPartyLatLng.php diff --git a/src/LatLng.php b/src/LatLng.php index cdad2c7..5beed18 100644 --- a/src/LatLng.php +++ b/src/LatLng.php @@ -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]; diff --git a/tests/Fixtures/ThirdPartyLatLng.php b/tests/Fixtures/ThirdPartyLatLng.php new file mode 100644 index 0000000..125f632 --- /dev/null +++ b/tests/Fixtures/ThirdPartyLatLng.php @@ -0,0 +1,25 @@ +lat = $lat; + $this->lng = $lng; + } + + public function getLatitude() + { + return $this->lat; + } + + public function getLongitude() + { + return $this->lng; + } +} diff --git a/tests/LngLatTest.php b/tests/LngLatTest.php index c065a73..93328dc 100644 --- a/tests/LngLatTest.php +++ b/tests/LngLatTest.php @@ -2,6 +2,8 @@ namespace Geokit; +use Geokit\Fixtures\ThirdPartyLatLng; + class LngLatTest extends \PHPUnit_Framework_TestCase { public function testConstructorShouldAcceptStringsAsArguments() @@ -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 */