MAPG-3 move utils classes into Util namespace
This commit is contained in:
1 parent
a61d2951d7
commit
d5c0e59d29
2 files changed
+2
-2
No files matched your search
@@ -0,0 +1,76 @@
|
||||
<?php namespace MapGuesser\Util\Geo;
|
||||
|
||||
class Bounds
|
||||
{
|
||||
private float $southLat;
|
||||
private float $westLng;
|
||||
|
||||
private float $northLat;
|
||||
private float $eastLng;
|
||||
|
||||
private bool $initialized = false;
|
||||
|
||||
public function __construct(Position $position = null)
|
||||
{
|
||||
if ($position === null) {
|
||||
return;
|
||||
}
|
||||
|
||||
$this->initialize($position);
|
||||
}
|
||||
|
||||
public function extend(Position $position): void
|
||||
{
|
||||
if (!$this->initialized) {
|
||||
$this->initialize($position);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
$lat = $position->getLat();
|
||||
$lng = $position->getLng();
|
||||
|
||||
if ($lat < $this->southLat) {
|
||||
$this->southLat = $lat;
|
||||
}
|
||||
|
||||
if ($lng < $this->westLng) {
|
||||
$this->westLng = $lng;
|
||||
}
|
||||
|
||||
if ($lat > $this->northLat) {
|
||||
$this->northLat = $lat;
|
||||
}
|
||||
|
||||
if ($lng > $this->eastLng) {
|
||||
$this->eastLng = $lng;
|
||||
}
|
||||
}
|
||||
|
||||
public function toJson(): string
|
||||
{
|
||||
if (!$this->initialized) {
|
||||
throw new \Exception("Bounds are not initialized!");
|
||||
}
|
||||
|
||||
return json_encode([
|
||||
'south' => $this->southLat,
|
||||
'west' => $this->westLng,
|
||||
'north' => $this->northLat,
|
||||
'east' => $this->eastLng,
|
||||
]);
|
||||
}
|
||||
|
||||
private function initialize(Position $position)
|
||||
{
|
||||
$lat = $position->getLat();
|
||||
$lng = $position->getLng();
|
||||
|
||||
$this->northLat = $lat;
|
||||
$this->westLng = $lng;
|
||||
$this->southLat = $lat;
|
||||
$this->eastLng = $lng;
|
||||
|
||||
$this->initialized = true;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
<?php namespace MapGuesser\Util\Geo;
|
||||
|
||||
class Position
|
||||
{
|
||||
private float $lat;
|
||||
private float $lng;
|
||||
|
||||
public function __construct(float $lat, float $lng)
|
||||
{
|
||||
$this->lat = $lat;
|
||||
$this->lng = $lng;
|
||||
}
|
||||
|
||||
public function getLat(): float
|
||||
{
|
||||
return $this->lat;
|
||||
}
|
||||
|
||||
public function getLng(): float
|
||||
{
|
||||
return $this->lng;
|
||||
}
|
||||
|
||||
public function toJson(): string
|
||||
{
|
||||
return json_encode([
|
||||
'lat' => $this->lat,
|
||||
'lng' => $this->lng,
|
||||
]);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user