add type hinting in Point and PointException

This commit is contained in:
nobohan 2021-04-22 09:32:46 +02:00
parent 0b2f29f1e8
commit c089960707
2 changed files with 21 additions and 17 deletions

View File

@ -9,25 +9,29 @@ use \JsonSerializable;
* *
*/ */
class Point implements JsonSerializable { class Point implements JsonSerializable {
private $lat; private float $lat;
private $lon; private float $lon;
public static $SRID = '4326'; public static string $SRID = '4326';
private function __construct($lon, $lat) { private function __construct($lon, $lat)
{
$this->lat = $lat; $this->lat = $lat;
$this->lon = $lon; $this->lon = $lon;
} }
public function toGeoJson() { public function toGeoJson(): string
{
$array = $this->toArrayGeoJson(); $array = $this->toArrayGeoJson();
return \json_encode($array); return \json_encode($array);
} }
public function jsonSerialize() { public function jsonSerialize(): array
{
return $this->toArrayGeoJson(); return $this->toArrayGeoJson();
} }
public function toArrayGeoJson() { public function toArrayGeoJson(): array
{
return array("type" => "Point", return array("type" => "Point",
"coordinates" => array ($this->lon, $this->lat)); "coordinates" => array ($this->lon, $this->lat));
} }
@ -36,7 +40,8 @@ class Point implements JsonSerializable {
* *
* @return string * @return string
*/ */
public function toWKT() { public function toWKT(): string
{
return 'SRID='.self::$SRID.';POINT('.$this->lon.' '.$this->lat.')'; return 'SRID='.self::$SRID.';POINT('.$this->lon.' '.$this->lat.')';
} }
@ -45,7 +50,7 @@ class Point implements JsonSerializable {
* @param type $geojson * @param type $geojson
* @return Point * @return Point
*/ */
public static function fromGeoJson($geojson) public static function fromGeoJson($geojson): Point
{ {
$a = json_decode($geojson); $a = json_decode($geojson);
//check if the geojson string is correct //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)) 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' && if ($array['type'] == 'Point' &&
isset($array['coordinates'])) isset($array['coordinates']))
@ -82,12 +87,12 @@ class Point implements JsonSerializable {
} }
} }
public function getLat() public function getLat(): float
{ {
return $this->lat; return $this->lat;
} }
public function getLon() public function getLon(): float
{ {
return $this->lon; return $this->lon;
} }

View File

@ -10,18 +10,17 @@ use \Exception;
*/ */
class PointException extends 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)"); 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"); 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"); return new self("le type de l'objet GeoJSON est invalide");
} }