mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-06-07 18:44:08 +00:00
add type hinting in Point and PointException
This commit is contained in:
parent
0b2f29f1e8
commit
c089960707
@ -9,25 +9,29 @@ use \JsonSerializable;
|
||||
*
|
||||
*/
|
||||
class Point implements JsonSerializable {
|
||||
private $lat;
|
||||
private $lon;
|
||||
public static $SRID = '4326';
|
||||
private float $lat;
|
||||
private float $lon;
|
||||
public static string $SRID = '4326';
|
||||
|
||||
private function __construct($lon, $lat) {
|
||||
private function __construct($lon, $lat)
|
||||
{
|
||||
$this->lat = $lat;
|
||||
$this->lon = $lon;
|
||||
}
|
||||
|
||||
public function toGeoJson() {
|
||||
public function toGeoJson(): string
|
||||
{
|
||||
$array = $this->toArrayGeoJson();
|
||||
return \json_encode($array);
|
||||
}
|
||||
|
||||
public function jsonSerialize() {
|
||||
public function jsonSerialize(): array
|
||||
{
|
||||
return $this->toArrayGeoJson();
|
||||
}
|
||||
|
||||
public function toArrayGeoJson() {
|
||||
public function toArrayGeoJson(): array
|
||||
{
|
||||
return array("type" => "Point",
|
||||
"coordinates" => array ($this->lon, $this->lat));
|
||||
}
|
||||
@ -36,7 +40,8 @@ class Point implements JsonSerializable {
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function toWKT() {
|
||||
public function toWKT(): string
|
||||
{
|
||||
return 'SRID='.self::$SRID.';POINT('.$this->lon.' '.$this->lat.')';
|
||||
}
|
||||
|
||||
@ -45,7 +50,7 @@ class Point implements JsonSerializable {
|
||||
* @param type $geojson
|
||||
* @return Point
|
||||
*/
|
||||
public static function fromGeoJson($geojson)
|
||||
public static function fromGeoJson($geojson): Point
|
||||
{
|
||||
$a = json_decode($geojson);
|
||||
//check if the geojson string is correct
|
||||
@ -63,7 +68,7 @@ class Point implements JsonSerializable {
|
||||
|
||||
}
|
||||
|
||||
public static function fromLonLat($lon, $lat)
|
||||
public static function fromLonLat($lon, $lat): Point
|
||||
{
|
||||
if (($lon > -180 && $lon < 180) && ($lat > -90 && $lat < 90))
|
||||
{
|
||||
@ -73,7 +78,7 @@ class Point implements JsonSerializable {
|
||||
}
|
||||
}
|
||||
|
||||
public static function fromArrayGeoJson($array)
|
||||
public static function fromArrayGeoJson($array): Point
|
||||
{
|
||||
if ($array['type'] == 'Point' &&
|
||||
isset($array['coordinates']))
|
||||
@ -82,12 +87,12 @@ class Point implements JsonSerializable {
|
||||
}
|
||||
}
|
||||
|
||||
public function getLat()
|
||||
public function getLat(): float
|
||||
{
|
||||
return $this->lat;
|
||||
}
|
||||
|
||||
public function getLon()
|
||||
public function getLon(): float
|
||||
{
|
||||
return $this->lon;
|
||||
}
|
||||
|
@ -10,18 +10,17 @@ use \Exception;
|
||||
*/
|
||||
class PointException extends Exception {
|
||||
|
||||
public static function badCoordinates($lon, $lat)
|
||||
public static function badCoordinates($lon, $lat): self
|
||||
{
|
||||
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)
|
||||
public static function badJsonString($str): self
|
||||
{
|
||||
return new self("la chaine JSON n'est pas valide : $str");
|
||||
|
||||
}
|
||||
|
||||
public static function badGeoType()
|
||||
public static function badGeoType(): self
|
||||
{
|
||||
return new self("le type de l'objet GeoJSON est invalide");
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user