Skip to content

Commit 7bb2ab9

Browse files
committed
Restructure the calculator's decode()
1 parent 5beb644 commit 7bb2ab9

File tree

4 files changed

+18
-7
lines changed

4 files changed

+18
-7
lines changed

src/CodeCalculator/AbstractCodeCalculator.php

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,24 @@ public function encode(float $latitude, float $longitude, int $codeLength): stri
5353
abstract protected function generateRevOlcCode(float $latitude, float $longitude, int $codeLength): string;
5454

5555
/**
56-
* Assuming the given (string) Open Location Code is valid and stripped, decodes it into a CodeArea object encapsulating latitude/longitude bounding box.
56+
* Assuming the given Open Location Code is valid, decodes it into a CodeArea object encapsulating latitude/longitude bounding box.
57+
* @param string $code The stripped Open Location Code.
58+
* @return CodeArea A CodeArea object.
59+
*/
60+
public function decode(string $code): CodeArea
61+
{
62+
// Strip padding and separator characters out of the code.
63+
$clean = str_replace([OpenLocationCode::SEPARATOR, OpenLocationCode::PADDING_CHARACTER], "", $code);
64+
65+
return $this->generateCodeArea($clean);
66+
}
67+
68+
/**
69+
* Performs the necessary calculation to generate a CodeArea object from the given (stripped) Open Location Code.
5770
* @param string $strippedCode The stripped Open Location Code.
5871
* @return CodeArea A CodeArea object.
5972
*/
60-
abstract public function decode(string $strippedCode): CodeArea;
73+
abstract protected function generateCodeArea(string $strippedCode): CodeArea;
6174

6275
/**
6376
* Returns a static instance of the appropriate code calculator depending on whether the current PHP is 32-bit/64-bit.

src/CodeCalculator/CodeCalculatorFloat.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ protected function generateRevOlcCode(float $latitude, float $longitude, int $co
7171
return $revCode;
7272
}
7373

74-
public function decode(string $strippedCode): CodeArea
74+
protected function generateCodeArea(string $strippedCode): CodeArea
7575
{
7676
// Initialize the values.
7777
// We will assume these values are floats to ensure 32bit PHP compatibility.

src/CodeCalculator/CodeCalculatorInt.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ protected function generateRevOlcCode(float $latitude, float $longitude, int $co
7575
return $revCode;
7676
}
7777

78-
public function decode(string $strippedCode): CodeArea
78+
protected function generateCodeArea(string $strippedCode): CodeArea
7979
{
8080
// Initialize the values.
8181
$latVal = -OpenLocationCode::LATITUDE_MAX * self::LAT_INTEGER_MULTIPLIER;

src/OpenLocationCode.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -143,12 +143,10 @@ public function decode(): CodeArea
143143
if (!$this->isFull()) {
144144
throw new LogicException("Method decode() may only be called on valid full codes, but code was {$this->code}.");
145145
}
146-
// Strip padding and separator characters out of the code.
147-
$clean = str_replace([self::SEPARATOR, self::PADDING_CHARACTER], "", $this->code);
148146

149147
// Delegate to the correct Code Calculator to decode the OLC code for 32-bit/64-bit platforms
150148
$calculator = AbstractCodeCalculator::getDefaultCalculator();
151-
return $calculator->decode($clean);
149+
return $calculator->decode($this->code);
152150
}
153151

154152
public function __toString(): string

0 commit comments

Comments
 (0)