From 7d1a1c4004b7d4a6a6489be959a89ae3be91ee5c Mon Sep 17 00:00:00 2001 From: nobohan Date: Mon, 19 Apr 2021 12:12:55 +0200 Subject: [PATCH] #32 add point type + Address: add Point field + add null on nullable fields --- .../ChillMainExtension.php | 3 +- .../Doctrine/Type/PointType.php | 72 ++++++++++++++ src/Bundle/ChillMainBundle/Entity/Address.php | 23 +++-- src/Bundle/ChillMainBundle/Entity/Point.php | 96 +++++++++++++++++++ .../ChillMainBundle/Entity/PointException.php | 28 ++++++ 5 files changed, 214 insertions(+), 8 deletions(-) create mode 100644 src/Bundle/ChillMainBundle/Doctrine/Type/PointType.php create mode 100644 src/Bundle/ChillMainBundle/Entity/Point.php create mode 100644 src/Bundle/ChillMainBundle/Entity/PointException.php diff --git a/src/Bundle/ChillMainBundle/DependencyInjection/ChillMainExtension.php b/src/Bundle/ChillMainBundle/DependencyInjection/ChillMainExtension.php index 234e1203d..ee9608d82 100644 --- a/src/Bundle/ChillMainBundle/DependencyInjection/ChillMainExtension.php +++ b/src/Bundle/ChillMainBundle/DependencyInjection/ChillMainExtension.php @@ -188,7 +188,8 @@ class ChillMainExtension extends Extension implements PrependExtensionInterface, $container->prependExtensionConfig('doctrine', array( 'dbal' => [ 'types' => [ - 'dateinterval' => \Chill\MainBundle\Doctrine\Type\NativeDateIntervalType::class + 'dateinterval' => \Chill\MainBundle\Doctrine\Type\NativeDateIntervalType::class, + 'json' => \Chill\MainBundle\Doctrine\Type\PointType::class ] ] )); diff --git a/src/Bundle/ChillMainBundle/Doctrine/Type/PointType.php b/src/Bundle/ChillMainBundle/Doctrine/Type/PointType.php new file mode 100644 index 000000000..ba84177ac --- /dev/null +++ b/src/Bundle/ChillMainBundle/Doctrine/Type/PointType.php @@ -0,0 +1,72 @@ +toWKT(); + } + + public function canRequireSQLConversion() + { + return true; + } + + public function convertToPHPValueSQL($sqlExpr, $platform) + { + return 'ST_AsGeoJSON('.$sqlExpr.') '; + } + + public function convertToDatabaseValueSQL($sqlExpr, AbstractPlatform $platform) + { + return $sqlExpr; + } + + + + + +} + diff --git a/src/Bundle/ChillMainBundle/Entity/Address.php b/src/Bundle/ChillMainBundle/Entity/Address.php index 1286ba2e0..6592c6a3d 100644 --- a/src/Bundle/ChillMainBundle/Entity/Address.php +++ b/src/Bundle/ChillMainBundle/Entity/Address.php @@ -45,49 +45,49 @@ class Address private $postcode; /** - * @var string + * @var string|null * * @ORM\Column(type="string", length=16, nullable=true) */ private $floor; /** - * @var string + * @var string|null * * @ORM\Column(type="string", length=16, nullable=true) */ private $corridor; /** - * @var string + * @var string|null * * @ORM\Column(type="string", length=16, nullable=true) */ private $steps; /** - * @var string + * @var string|null * * @ORM\Column(type="string", length=255, nullable=true) */ private $buildingName; /** - * @var string + * @var string|null * * @ORM\Column(type="string", length=16, nullable=true) */ private $flat; /** - * @var string + * @var string|null * * @ORM\Column(type="string", length=255, nullable=true) */ private $distribution; /** - * @var string + * @var string|null * * @ORM\Column(type="string", length=255, nullable=true) */ @@ -120,6 +120,15 @@ class Address */ private $isNoAddress = false; + /** + * A geospatial field storing the coordinates of the Address + * + * @var Point|null + * + * @ORM\Column(type="Point", nullable=true) + */ + private $point; + /** * A list of metadata, added by customizable fields * diff --git a/src/Bundle/ChillMainBundle/Entity/Point.php b/src/Bundle/ChillMainBundle/Entity/Point.php new file mode 100644 index 000000000..528834e42 --- /dev/null +++ b/src/Bundle/ChillMainBundle/Entity/Point.php @@ -0,0 +1,96 @@ +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; + } +} + + diff --git a/src/Bundle/ChillMainBundle/Entity/PointException.php b/src/Bundle/ChillMainBundle/Entity/PointException.php new file mode 100644 index 000000000..ac024eebf --- /dev/null +++ b/src/Bundle/ChillMainBundle/Entity/PointException.php @@ -0,0 +1,28 @@ +