mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-09-06 06:44:59 +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);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
@@ -1,27 +1,33 @@
|
||||
<?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 \Exception;
|
||||
use Exception;
|
||||
|
||||
/**
|
||||
* Description of PointException
|
||||
*
|
||||
* Description of PointException.
|
||||
*/
|
||||
class PointException extends Exception {
|
||||
|
||||
class PointException extends Exception
|
||||
{
|
||||
public static function badCoordinates($lon, $lat): self
|
||||
{
|
||||
return new self("Input coordinates are not valid in the used coordinate system (longitude = $lon , latitude = $lat)");
|
||||
}
|
||||
|
||||
public static function badJsonString($str): self
|
||||
{
|
||||
return new self("The JSON string is not valid: $str");
|
||||
return new self("Input coordinates are not valid in the used coordinate system (longitude = {$lon} , latitude = {$lat})");
|
||||
}
|
||||
|
||||
public static function badGeoType(): self
|
||||
{
|
||||
return new self("The geoJSON object type is not valid");
|
||||
return new self('The geoJSON object type is not valid');
|
||||
}
|
||||
|
||||
public static function badJsonString($str): self
|
||||
{
|
||||
return new self("The JSON string is not valid: {$str}");
|
||||
}
|
||||
}
|
||||
|
@@ -1,12 +1,20 @@
|
||||
<?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 Chill\MainBundle\Entity\User;
|
||||
use DateTimeInterface;
|
||||
|
||||
interface TrackCreationInterface
|
||||
{
|
||||
public function setCreatedBy(User $user): self;
|
||||
public function setCreatedAt(DateTimeInterface $datetime): self;
|
||||
|
||||
public function setCreatedAt(\DateTimeInterface $datetime): self;
|
||||
public function setCreatedBy(User $user): self;
|
||||
}
|
||||
|
@@ -1,12 +1,20 @@
|
||||
<?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 Chill\MainBundle\Entity\User;
|
||||
use DateTimeInterface;
|
||||
|
||||
interface TrackUpdateInterface
|
||||
{
|
||||
public function setUpdatedBy(User $user): self;
|
||||
public function setUpdatedAt(DateTimeInterface $datetime): self;
|
||||
|
||||
public function setUpdatedAt(\DateTimeInterface $datetime): self;
|
||||
public function setUpdatedBy(User $user): self;
|
||||
}
|
||||
|
Reference in New Issue
Block a user