add point type + Address: add Point field : fix dependency injection of the Point type

This commit is contained in:
nobohan
2021-04-20 10:55:45 +02:00
parent 7d1a1c4004
commit 9a4f50472a
5 changed files with 24 additions and 7 deletions

View File

@@ -0,0 +1,96 @@
<?php
namespace Chill\MainBundle\Doctrine\Model;
use \JsonSerializable;
/**
* Description of Point
*
*/
class Point implements JsonSerializable {
private $lat;
private $lon;
public static $SRID = '4326';
private function __construct($lon, $lat) {
$this->lat = $lat;
$this->lon = $lon;
}
public function toGeoJson() {
$array = $this->toArrayGeoJson();
return \json_encode($array);
}
public function jsonSerialize() {
return $this->toArrayGeoJson();
}
public function toArrayGeoJson() {
return array("type" => "Point",
"coordinates" => array ($this->lon, $this->lat));
}
/**
*
* @return string
*/
public function toWKT() {
return 'SRID='.self::$SRID.';POINT('.$this->lon.' '.$this->lat.')';
}
/**
*
* @param type $geojson
* @return Point
*/
public static function fromGeoJson($geojson)
{
$a = json_decode($geojson);
//check if the geojson string is correct
if ($a == null or !isset($a->type) or !isset($a->coordinates)){
throw PointException::badJsonString($geojson);
}
if ($a->type != "Point"){
throw PointException::badGeoType();
} else {
$lat = $a->coordinates[1];
$lon = $a->coordinates[0];
return Point::fromLonLat($lon, $lat);
}
}
public static function fromLonLat($lon, $lat)
{
if (($lon > -180 && $lon < 180) && ($lat > -90 && $lat < 90))
{
return new Point($lon, $lat);
} else {
throw PointException::badCoordinates($lon, $lat);
}
}
public static function fromArrayGeoJson($array)
{
if ($array['type'] == 'Point' &&
isset($array['coordinates']))
{
return self::fromLonLat($array['coordinates'][0], $array['coordinates'][1]);
}
}
public function getLat()
{
return $this->lat;
}
public function getLon()
{
return $this->lon;
}
}