Skip to content

Commit

Permalink
Fix calculate neighbor when lastChar is empty (#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
skthon authored Jul 25, 2020
1 parent 8075535 commit ee1de51
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 7 deletions.
6 changes: 1 addition & 5 deletions src/Geohash.php
Original file line number Diff line number Diff line change
Expand Up @@ -234,10 +234,6 @@ private function calculateNeighbor($hash, $direction) {
if (strpos($this->borderChars[$evenOrOdd][$direction], $lastChar) !== false) {
$baseHash = $this->calculateNeighbor($baseHash, $direction);
}
if (isset($baseHash[0])) {
return $baseHash . $this->neighborChars[$evenOrOdd][$direction][strpos($this->base32Mapping, $lastChar)];
} else {
return '';
}
return $baseHash . $this->neighborChars[$evenOrOdd][$direction][strpos($this->base32Mapping, $lastChar)];
}
}
52 changes: 50 additions & 2 deletions tests/GeohashTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

use PHPUnit\Framework\TestCase;
use Sk\Geohash\Geohash;

class GeohashTest extends TestCase
{
/**
Expand Down Expand Up @@ -75,4 +75,52 @@ public function testGetNeighbors() {
'NorthWest' => 'dhx4b7r',
], $neighbors);
}
}

public function testGetNeighborsGeohash12() {
$geohash = new Geohash();
$hash = '7j2r6k4z6xtv';
$neighbors = $geohash->getNeighbors($hash);
$this->assertEquals([
'North' => '7j2r6k4z6xty',
'East'=> '7j2r6k4z6xwj',
'South' => '7j2r6k4z6xtu',
'West' => '7j2r6k4z6xtt',
'NorthEast' => '7j2r6k4z6xwn',
'SouthEast' => '7j2r6k4z6xwh',
'SouthWest' => '7j2r6k4z6xts',
'NorthWest' => '7j2r6k4z6xtw',
], $neighbors);
}

public function testGetNeighborsGeohash_7j2() {
$geohash = new Geohash();
$hash = '7j2';
$neighbors = $geohash->getNeighbors($hash);
$this->assertEquals([
'North' => '7j8',
'East'=> '7j3',
'South' => '7j0',
'West' => '6vr',
'NorthEast' => '7j9',
'SouthEast' => '7j1',
'SouthWest' => '6vp',
'NorthWest' => '6vx',
], $neighbors);
}

public function testGetNeighborsGeohash_g00() {
$geohash = new Geohash();
$hash = 'g00';
$neighbors = $geohash->getNeighbors($hash);
$this->assertEquals([
'North' => 'g02',
'East'=> 'g01',
'South' => 'epb',
'West' => 'fbp',
'NorthEast' => 'g03',
'SouthEast' => 'epc',
'SouthWest' => 'dzz',
'NorthWest' => 'fbr',
], $neighbors);
}
}

0 comments on commit ee1de51

Please sign in to comment.