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

@@ -4,6 +4,7 @@ namespace Chill\MainBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Context\ExecutionContextInterface;
use Chill\MainBundle\Doctrine\Model\Point;
/**
* Address
@@ -125,7 +126,7 @@ class Address
*
* @var Point|null
*
* @ORM\Column(type="Point", nullable=true)
* @ORM\Column(type="point", nullable=true)
*/
private $point;
@@ -474,5 +475,17 @@ class Address
return $this;
}
public function getPoint(): ?Point
{
return $this->point;
}
public function setPoint(?Point $point): self
{
$this->point = $point;
return $this;
}
}

View File

@@ -1,96 +0,0 @@
<?php
namespace Chill\MainBundle\Entity;
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;
}
}

View File

@@ -1,28 +0,0 @@
<?php
namespace Chill\MainBundle\Entity;
use \Exception;
/**
* Description of PointException
*
*/
class PointException extends Exception {
public static function badCoordinates($lon, $lat)
{
return new self("les coordonnées fournies sont invalides dans le système de projection utilisé (longitude = $lon , latitude = $lat)");
}
public static function badJsonString($str)
{
return new self("la chaine JSON n'est pas valide : $str");
}
public static function badGeoType()
{
return new self("le type de l'objet GeoJSON est invalide");
}
}