Skip to content

Commit c62302a

Browse files
Merge pull request #3 from Vectorial1024/olc_contains
Add alternative way of calling contains()
2 parents 6f75874 + 05081db commit c62302a

File tree

3 files changed

+22
-5
lines changed

3 files changed

+22
-5
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ class OpenLocationCode implememts Stringable
9494
public function recover(float $referenceLatitude, float $referenceLongitude): self;
9595

9696
public function contains(float $latitude, float $longitude): bool;
97+
// note: if you need to call contains() many times on the same $this, consider decoding $this first, and then call contains() on the resulting CodeArea instance
9798

9899
public static function isValidCode(string $code): bool;
99100
public function isValid(): bool;
@@ -121,6 +122,8 @@ class CodeArea
121122

122123
public function getCenterLatitude(): float;
123124
public function getCenterLongitude(): float;
125+
126+
public function contains(float $latitude, float $longitude): float;
124127
}
125128
```
126129

src/CodeArea.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,19 @@ public function getCenterLongitude(): float
3838
{
3939
return ($this->eastLongitude + $this->westLongitude) / 2;
4040
}
41+
42+
/**
43+
* Returns whether the bounding box specified by the code area (from a decoded Open Location Code) contains the provided point.
44+
*
45+
* @param float $latitude The provided latitude in degrees.
46+
* @param float $longitude The provided longitude in degrees.
47+
* @return bool True if the coordinates are contained by the code area.
48+
*/
49+
public function contains(float $latitude, float $longitude): bool
50+
{
51+
return $this->southLatitude <= $latitude
52+
&& $latitude < $this->northLatitude
53+
&& $this->westLongitude <= $longitude
54+
&& $longitude < $this->eastLongitude;
55+
}
4156
}

src/OpenLocationCode.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -349,17 +349,16 @@ public function recover(float $referenceLatitude, float $referenceLongitude): se
349349
/**
350350
* Returns whether the bounding box specified by the Open Location Code contains the provided point.
351351
*
352+
* @see CodeArea::contains() for the underlying implementation.
353+
*
352354
* @param float $latitude The provided latitude in degrees.
353355
* @param float $longitude The provided longitude in degrees.
354-
* @return bool True if tge coordinates are contained by the code.
356+
* @return bool True if the coordinates are contained by the code.
355357
*/
356358
public function contains(float $latitude, float $longitude): bool
357359
{
358360
$codeArea = $this->decode();
359-
return $codeArea->southLatitude <= $latitude
360-
&& $latitude < $codeArea->northLatitude
361-
&& $codeArea->westLongitude <= $longitude
362-
&& $longitude < $codeArea->eastLongitude;
361+
return $codeArea->contains($latitude, $longitude);
363362
}
364363

365364
// ---

0 commit comments

Comments
 (0)