diff --git a/composer.json b/composer.json index 494e5f6..798e513 100644 --- a/composer.json +++ b/composer.json @@ -12,7 +12,7 @@ ], "license": "GPL-3.0-or-later", "require": { - "php": "^7||^8" + "php": "^7.1||^8" }, "require-dev": { "phpunit/phpunit": "^7||^8||^9" diff --git a/src/OpenLocationCode.php b/src/OpenLocationCode.php index 9a7cb0d..da9cf8a 100644 --- a/src/OpenLocationCode.php +++ b/src/OpenLocationCode.php @@ -1,8 +1,8 @@ code = strtoupper($code); }elseif((func_num_args()==2 || func_num_args()==3) && is_numeric($latitudeOrCode) && is_numeric($longitude) && is_int($codeLength)){ - $latitude = $latitudeOrCode; + $latitude = (double) $latitudeOrCode; + $longitude = (double) $longitude; + $codeLength = $codeLength ?? self::PAIR_CODE_LENGTH; $codeLength = min($codeLength,self::MAX_DIGIT_COUNT); if($codeLengthself::PAIR_CODE_LENGTH){ for($i=0;$icode)){ - throw new Exception('Method decode() could only be called on valid full codes, code was '.$this->code.'.'); + throw new RuntimeException('Method decode() could only be called on valid full codes, code was '.$this->code.'.'); } $clean = str_replace(self::PADDING_CHARACTER,'',str_replace(self::SEPARATOR,'',$this->code)); - $latVal = -self::LATITUDE_MAX * self::LAT_INTEGER_MULTIPLIER; - $lngVal = -self::LONGITUDE_MAX * self::LNG_INTEGER_MULTIPLIER; + $latVal = self::floatToInt(-self::LATITUDE_MAX * self::LAT_INTEGER_MULTIPLIER); + $lngVal = self::floatToInt(-self::LONGITUDE_MAX * self::LNG_INTEGER_MULTIPLIER); - $latPlaceVal = self::LAT_MSP_VALUE; - $lngPlaceVal = self::LNG_MSP_VALUE; + $latPlaceVal = self::floatToInt(self::LAT_MSP_VALUE); + $lngPlaceVal = self::floatToInt(self::LNG_MSP_VALUE); for($i=0;$idecode(); - $range = max(abs($referenceLatitude - $codeArea->getCenterLatitude()),$referenceLongitude-$codeArea->getCenterLongitude()); + var_dump($codeArea); + $range = max(abs($referenceLatitude - $codeArea->getCenterLatitude()),$referenceLongitude - $codeArea->getCenterLongitude()); for($i=4;$i>=1;$i--){ if($range<(self::computeLatitudePrecision($i*2)*0.3)){ @@ -255,7 +258,7 @@ public function recover($referenceLatitude,$referenceLongitude){ $referenceLongitude = self::normalizeLongitude($referenceLongitude); $digitsToRecover = self::SEPARATOR_POSITION - self::indexOf($this->code,self::SEPARATOR); - $prefixPrecision = pow(self::ENCODING_BASE,2-($digitsToRecover/2)); + $prefixPrecision = self::ENCODING_BASE ** (2-($digitsToRecover/2)); $recoveredPrefix = substr((new self($referenceLatitude,$referenceLongitude))->getCode(),0,$digitsToRecover); $recovered = new self($recoveredPrefix.$this->code); @@ -285,7 +288,6 @@ public function recover($referenceLatitude,$referenceLongitude){ * @param double $latitude * @param double $longitude * @return bool - * @throws Exception */ public function contains($latitude,$longitude){ $codeArea = $this->decode(); @@ -293,8 +295,6 @@ public function contains($latitude,$longitude){ return $codeArea->getSouthLatitude()<=$latitude && $latitude<$codeArea->getNorthLatitude() && $codeArea->getWestLongitude()<=$longitude && $longitude<$codeArea->getEastLongitude(); } - //TODO equals - public function __toString(){ return $this->getCode(); } @@ -405,7 +405,7 @@ private static function normalizeLongitude($longitude){ return $longitude; } $CIRCLE_DEG = 2 * self::LONGITUDE_MAX; - return ($longitude % $CIRCLE_DEG + $CIRCLE_DEG + self::LONGITUDE_MAX) % $CIRCLE_DEG - self::LONGITUDE_MAX; + return fmod((fmod($longitude,$CIRCLE_DEG) + $CIRCLE_DEG + self::LONGITUDE_MAX),$CIRCLE_DEG) - self::LONGITUDE_MAX; } /** @@ -427,4 +427,8 @@ private static function indexOf($haystack,$needle,$offset=0){ return $pos; } + private static function floatToInt($float){ + return (intval(($float+0).'')); + } + } \ No newline at end of file diff --git a/tests/ShorteningTest.php b/tests/ShorteningTest.php index 7e5c14b..f58e059 100644 --- a/tests/ShorteningTest.php +++ b/tests/ShorteningTest.php @@ -26,6 +26,7 @@ public function testShortening(){ if('B'!==$testData->testType && 'S'!==$testData->testType){ continue; } + var_dump($testData); $olc = new OpenLocationCode($testData->code); $shortened = $olc->shorten($testData->referenceLatitude,$testData->referenceLongitude); $this->assertEquals($testData->shortCode,$shortened->getCode(),'Wrong shortening of code '.$testData->code); diff --git a/tests/ValidityTest_TestData.php b/tests/ValidityTest_TestData.php index a1ca18d..d359907 100644 --- a/tests/ValidityTest_TestData.php +++ b/tests/ValidityTest_TestData.php @@ -19,7 +19,6 @@ public function __construct($line){ $this->isValid = filter_var($parts[1],FILTER_VALIDATE_BOOLEAN); $this->isShort = filter_var($parts[2],FILTER_VALIDATE_BOOLEAN); $this->isFull = filter_var($parts[3],FILTER_VALIDATE_BOOLEAN); -// var_dump($this); } } \ No newline at end of file