mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-11-22 20:07:47 +00:00
cs: Fix code style (safe rules only).
This commit is contained in:
@@ -1,25 +1,79 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Chill is a software for social workers
|
||||
*
|
||||
* For the full copyright and license information, please view
|
||||
* the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Chill\MainBundle\Doctrine\Model;
|
||||
|
||||
use \JsonSerializable;
|
||||
use Exception;
|
||||
use JsonSerializable;
|
||||
use function json_encode;
|
||||
|
||||
class Point implements JsonSerializable {
|
||||
private ?float $lat;
|
||||
private ?float $lon;
|
||||
class Point implements JsonSerializable
|
||||
{
|
||||
public static string $SRID = '4326';
|
||||
|
||||
private ?float $lat;
|
||||
|
||||
private ?float $lon;
|
||||
|
||||
private function __construct(?float $lon, ?float $lat)
|
||||
{
|
||||
$this->lat = $lat;
|
||||
$this->lon = $lon;
|
||||
}
|
||||
|
||||
public function toGeoJson(): string
|
||||
public static function fromArrayGeoJson(array $array): self
|
||||
{
|
||||
$array = $this->toArrayGeoJson();
|
||||
if ('Point' === $array['type'] && isset($array['coordinates'])) {
|
||||
return self::fromLonLat($array['coordinates'][0], $array['coordinates'][1]);
|
||||
}
|
||||
|
||||
return \json_encode($array);
|
||||
throw new Exception('Unable to build a point from input data.');
|
||||
}
|
||||
|
||||
public static function fromGeoJson(string $geojson): self
|
||||
{
|
||||
$a = json_decode($geojson);
|
||||
|
||||
if (null === $a) {
|
||||
throw PointException::badJsonString($geojson);
|
||||
}
|
||||
|
||||
if (null === $a->type || null === $a->coordinates) {
|
||||
throw PointException::badJsonString($geojson);
|
||||
}
|
||||
|
||||
if ('Point' !== $a->type) {
|
||||
throw PointException::badGeoType();
|
||||
}
|
||||
|
||||
[$lon, $lat] = $a->coordinates;
|
||||
|
||||
return Point::fromLonLat($lon, $lat);
|
||||
}
|
||||
|
||||
public static function fromLonLat(float $lon, float $lat): self
|
||||
{
|
||||
if ((-180 < $lon && 180 > $lon) && (-90 < $lat && 90 > $lat)) {
|
||||
return new Point($lon, $lat);
|
||||
}
|
||||
|
||||
throw PointException::badCoordinates($lon, $lat);
|
||||
}
|
||||
|
||||
public function getLat(): float
|
||||
{
|
||||
return $this->lat;
|
||||
}
|
||||
|
||||
public function getLon(): float
|
||||
{
|
||||
return $this->lon;
|
||||
}
|
||||
|
||||
public function jsonSerialize(): array
|
||||
@@ -35,59 +89,15 @@ class Point implements JsonSerializable {
|
||||
];
|
||||
}
|
||||
|
||||
public function toGeoJson(): string
|
||||
{
|
||||
$array = $this->toArrayGeoJson();
|
||||
|
||||
return json_encode($array);
|
||||
}
|
||||
|
||||
public function toWKT(): string
|
||||
{
|
||||
return sprintf("SRID=%s;POINT(%s %s)", self::$SRID, $this->lon, $this->lat);
|
||||
}
|
||||
|
||||
public static function fromGeoJson(string $geojson): self
|
||||
{
|
||||
$a = json_decode($geojson);
|
||||
|
||||
if (null === $a) {
|
||||
throw PointException::badJsonString($geojson);
|
||||
}
|
||||
|
||||
if (null === $a->type || null === $a->coordinates) {
|
||||
throw PointException::badJsonString($geojson);
|
||||
}
|
||||
|
||||
if ($a->type !== 'Point'){
|
||||
throw PointException::badGeoType();
|
||||
}
|
||||
|
||||
[$lon, $lat] = $a->coordinates;
|
||||
|
||||
return Point::fromLonLat($lon, $lat);
|
||||
}
|
||||
|
||||
public static function fromLonLat(float $lon, float $lat): self
|
||||
{
|
||||
if (($lon > -180 && $lon < 180) && ($lat > -90 && $lat < 90)) {
|
||||
return new Point($lon, $lat);
|
||||
}
|
||||
|
||||
throw PointException::badCoordinates($lon, $lat);
|
||||
}
|
||||
|
||||
public static function fromArrayGeoJson(array $array): self
|
||||
{
|
||||
if ($array['type'] === 'Point' && isset($array['coordinates'])) {
|
||||
return self::fromLonLat($array['coordinates'][0], $array['coordinates'][1]);
|
||||
}
|
||||
|
||||
throw new \Exception('Unable to build a point from input data.');
|
||||
}
|
||||
|
||||
public function getLat(): float
|
||||
{
|
||||
return $this->lat;
|
||||
}
|
||||
|
||||
public function getLon(): float
|
||||
{
|
||||
return $this->lon;
|
||||
return sprintf('SRID=%s;POINT(%s %s)', self::$SRID, $this->lon, $this->lat);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user