Skip to content

Commit 6409730

Browse files
committed
Fix code to pass more tests
1 parent d56565f commit 6409730

File tree

4 files changed

+39
-35
lines changed

4 files changed

+39
-35
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
],
1313
"license": "GPL-3.0-or-later",
1414
"require": {
15-
"php": "^7||^8"
15+
"php": "^7.1||^8"
1616
},
1717
"require-dev": {
1818
"phpunit/phpunit": "^7||^8||^9"

src/OpenLocationCode.php

Lines changed: 37 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<?php
22
namespace YOCLIB\OpenLocationCode;
33

4-
use Exception;
54
use InvalidArgumentException;
5+
use RuntimeException;
66

77
class OpenLocationCode{
88

@@ -46,16 +46,19 @@ class OpenLocationCode{
4646
* @param double|string $latitudeOrCode
4747
* @param ?double $longitude
4848
* @param ?int $codeLength
49+
* @throws InvalidArgumentException
4950
*/
5051
public function __construct($latitudeOrCode,$longitude=null,$codeLength=self::CODE_PRECISION_NORMAL){
5152
if(func_num_args()===1 && is_string($latitudeOrCode)){
52-
$code = $latitudeOrCode;
53+
$code = (string) $latitudeOrCode;
5354
if(!self::isValidCode(strtoupper($code))){
5455
throw new InvalidArgumentException('The provided code \''.$code.'\' is not a valid Open Location Code.');
5556
}
5657
$this->code = strtoupper($code);
5758
}elseif((func_num_args()==2 || func_num_args()==3) && is_numeric($latitudeOrCode) && is_numeric($longitude) && is_int($codeLength)){
58-
$latitude = $latitudeOrCode;
59+
$latitude = (double) $latitudeOrCode;
60+
$longitude = (double) $longitude;
61+
$codeLength = $codeLength ?? self::PAIR_CODE_LENGTH;
5962

6063
$codeLength = min($codeLength,self::MAX_DIGIT_COUNT);
6164
if($codeLength<self::PAIR_CODE_LENGTH && $codeLength%2==1 || $codeLength<4){
@@ -70,28 +73,28 @@ public function __construct($latitudeOrCode,$longitude=null,$codeLength=self::CO
7073

7174
$revCode = '';
7275

73-
$latVal = (int) round(($latitude+self::LATITUDE_MAX)*self::LAT_INTEGER_MULTIPLIER*1e6)/1e6;
74-
$lngVal = (int) round(($longitude+self::LONGITUDE_MAX)*self::LNG_INTEGER_MULTIPLIER*1e6)/1e6;
76+
$latVal = self::floatToInt(round(($latitude+self::LATITUDE_MAX)*self::LAT_INTEGER_MULTIPLIER,6));
77+
$lngVal = self::floatToInt(round(($longitude+self::LONGITUDE_MAX)*self::LNG_INTEGER_MULTIPLIER,6));
7578

7679
if($codeLength>self::PAIR_CODE_LENGTH){
7780
for($i=0;$i<self::GRID_CODE_LENGTH;$i++){
7881
$latDigit = $latVal%self::GRID_ROWS;
7982
$lngDigit = $lngVal%self::GRID_COLUMNS;
80-
$ndx = (int) ($latDigit*self::GRID_COLUMNS+$lngDigit);
83+
$ndx = self::floatToInt($latDigit*self::GRID_COLUMNS+$lngDigit);
8184
$revCode .= substr(self::CODE_ALPHABET,$ndx,1);
82-
$latVal /= self::GRID_ROWS;
83-
$lngVal /= self::GRID_COLUMNS;
85+
$latVal = self::floatToInt($latVal/self::GRID_ROWS);
86+
$lngVal = self::floatToInt($lngVal/self::GRID_COLUMNS);
8487
}
8588
}else{
86-
$latVal = (int) ($latVal/pow(self::GRID_ROWS,self::GRID_CODE_LENGTH));
87-
$lngVal = (int) ($lngVal/pow(self::GRID_COLUMNS,self::GRID_CODE_LENGTH));
89+
$latVal = self::floatToInt($latVal/(self::GRID_ROWS ** self::GRID_CODE_LENGTH));
90+
$lngVal = self::floatToInt($lngVal/(self::GRID_COLUMNS ** self::GRID_CODE_LENGTH));
8891
}
8992

9093
for($i=0;$i<self::PAIR_CODE_LENGTH/2;$i++){
9194
$revCode .= substr(self::CODE_ALPHABET,$lngVal%self::ENCODING_BASE,1);
9295
$revCode .= substr(self::CODE_ALPHABET,$latVal%self::ENCODING_BASE,1);
93-
$latVal /= self::ENCODING_BASE;
94-
$lngVal /= self::ENCODING_BASE;
96+
$latVal = self::floatToInt($latVal/self::ENCODING_BASE);
97+
$lngVal = self::floatToInt($lngVal/self::ENCODING_BASE);
9598
if($i===0){
9699
$revCode .= self::SEPARATOR;
97100
}
@@ -130,33 +133,32 @@ public static function encode($latitude,$longitude,$codeLength=self::CODE_PRECIS
130133

131134
/**
132135
* @return CodeArea
133-
* @throws Exception
134136
*/
135137
public function decode(){
136138
if(!self::isFullCode($this->code)){
137-
throw new Exception('Method decode() could only be called on valid full codes, code was '.$this->code.'.');
139+
throw new RuntimeException('Method decode() could only be called on valid full codes, code was '.$this->code.'.');
138140
}
139141
$clean = str_replace(self::PADDING_CHARACTER,'',str_replace(self::SEPARATOR,'',$this->code));
140142

141-
$latVal = -self::LATITUDE_MAX * self::LAT_INTEGER_MULTIPLIER;
142-
$lngVal = -self::LONGITUDE_MAX * self::LNG_INTEGER_MULTIPLIER;
143+
$latVal = self::floatToInt(-self::LATITUDE_MAX * self::LAT_INTEGER_MULTIPLIER);
144+
$lngVal = self::floatToInt(-self::LONGITUDE_MAX * self::LNG_INTEGER_MULTIPLIER);
143145

144-
$latPlaceVal = self::LAT_MSP_VALUE;
145-
$lngPlaceVal = self::LNG_MSP_VALUE;
146+
$latPlaceVal = self::floatToInt(self::LAT_MSP_VALUE);
147+
$lngPlaceVal = self::floatToInt(self::LNG_MSP_VALUE);
146148
for($i=0;$i<min(strlen($clean),self::PAIR_CODE_LENGTH);$i+=2){
147-
$latPlaceVal /= self::ENCODING_BASE;
148-
$lngPlaceVal /= self::ENCODING_BASE;
149-
$latVal += self::indexOf(self::CODE_ALPHABET,substr($clean,$i,1)) * $latPlaceVal;
150-
$lngVal += self::indexOf(self::CODE_ALPHABET,substr($clean,$i+1,1)) * $lngPlaceVal;
149+
$latPlaceVal = self::floatToInt($latPlaceVal/self::ENCODING_BASE);
150+
$lngPlaceVal = self::floatToInt($lngPlaceVal/self::ENCODING_BASE);
151+
$latVal += self::floatToInt(self::indexOf(self::CODE_ALPHABET,substr($clean,$i,1)) * $latPlaceVal);
152+
$lngVal += self::floatToInt(self::indexOf(self::CODE_ALPHABET,substr($clean,$i+1,1)) * $lngPlaceVal);
151153
}
152154
for($i=self::PAIR_CODE_LENGTH;$i<min(strlen($clean),self::MAX_DIGIT_COUNT);$i++){
153-
$latPlaceVal /= self::GRID_ROWS;
154-
$lngPlaceVal /= self::GRID_COLUMNS;
155+
$latPlaceVal = self::floatToInt($latPlaceVal/self::GRID_ROWS);
156+
$lngPlaceVal = self::floatToInt($lngPlaceVal/self::GRID_COLUMNS);
155157
$digit = self::indexOf(self::CODE_ALPHABET,substr($clean,$i,1));
156-
$row = $digit/self::GRID_COLUMNS;
158+
$row = self::floatToInt($digit/self::GRID_COLUMNS);
157159
$col = $digit%self::GRID_COLUMNS;
158-
$latVal += $row*$latPlaceVal;
159-
$lngVal += $col*$lngPlaceVal;
160+
$latVal += self::floatToInt($row*$latPlaceVal);
161+
$lngVal += self::floatToInt($col*$lngPlaceVal);
160162
}
161163
$latitudeLo = $latVal/self::LAT_INTEGER_MULTIPLIER;
162164
$longitudeLo = $lngVal/self::LNG_INTEGER_MULTIPLIER;
@@ -232,7 +234,8 @@ public function shorten($referenceLatitude,$referenceLongitude){
232234
}
233235

234236
$codeArea = $this->decode();
235-
$range = max(abs($referenceLatitude - $codeArea->getCenterLatitude()),$referenceLongitude-$codeArea->getCenterLongitude());
237+
var_dump($codeArea);
238+
$range = max(abs($referenceLatitude - $codeArea->getCenterLatitude()),$referenceLongitude - $codeArea->getCenterLongitude());
236239

237240
for($i=4;$i>=1;$i--){
238241
if($range<(self::computeLatitudePrecision($i*2)*0.3)){
@@ -255,7 +258,7 @@ public function recover($referenceLatitude,$referenceLongitude){
255258
$referenceLongitude = self::normalizeLongitude($referenceLongitude);
256259

257260
$digitsToRecover = self::SEPARATOR_POSITION - self::indexOf($this->code,self::SEPARATOR);
258-
$prefixPrecision = pow(self::ENCODING_BASE,2-($digitsToRecover/2));
261+
$prefixPrecision = self::ENCODING_BASE ** (2-($digitsToRecover/2));
259262

260263
$recoveredPrefix = substr((new self($referenceLatitude,$referenceLongitude))->getCode(),0,$digitsToRecover);
261264
$recovered = new self($recoveredPrefix.$this->code);
@@ -285,16 +288,13 @@ public function recover($referenceLatitude,$referenceLongitude){
285288
* @param double $latitude
286289
* @param double $longitude
287290
* @return bool
288-
* @throws Exception
289291
*/
290292
public function contains($latitude,$longitude){
291293
$codeArea = $this->decode();
292294

293295
return $codeArea->getSouthLatitude()<=$latitude && $latitude<$codeArea->getNorthLatitude() && $codeArea->getWestLongitude()<=$longitude && $longitude<$codeArea->getEastLongitude();
294296
}
295297

296-
//TODO equals
297-
298298
public function __toString(){
299299
return $this->getCode();
300300
}
@@ -405,7 +405,7 @@ private static function normalizeLongitude($longitude){
405405
return $longitude;
406406
}
407407
$CIRCLE_DEG = 2 * self::LONGITUDE_MAX;
408-
return ($longitude % $CIRCLE_DEG + $CIRCLE_DEG + self::LONGITUDE_MAX) % $CIRCLE_DEG - self::LONGITUDE_MAX;
408+
return fmod((fmod($longitude,$CIRCLE_DEG) + $CIRCLE_DEG + self::LONGITUDE_MAX),$CIRCLE_DEG) - self::LONGITUDE_MAX;
409409
}
410410

411411
/**
@@ -427,4 +427,8 @@ private static function indexOf($haystack,$needle,$offset=0){
427427
return $pos;
428428
}
429429

430+
private static function floatToInt($float){
431+
return (intval(($float+0).''));
432+
}
433+
430434
}

tests/ShorteningTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ public function testShortening(){
2626
if('B'!==$testData->testType && 'S'!==$testData->testType){
2727
continue;
2828
}
29+
var_dump($testData);
2930
$olc = new OpenLocationCode($testData->code);
3031
$shortened = $olc->shorten($testData->referenceLatitude,$testData->referenceLongitude);
3132
$this->assertEquals($testData->shortCode,$shortened->getCode(),'Wrong shortening of code '.$testData->code);

tests/ValidityTest_TestData.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ public function __construct($line){
1919
$this->isValid = filter_var($parts[1],FILTER_VALIDATE_BOOLEAN);
2020
$this->isShort = filter_var($parts[2],FILTER_VALIDATE_BOOLEAN);
2121
$this->isFull = filter_var($parts[3],FILTER_VALIDATE_BOOLEAN);
22-
// var_dump($this);
2322
}
2423

2524
}

0 commit comments

Comments
 (0)