Last active
June 4, 2020 06:16
-
-
Save nyamsprod/365b6e4f435c1c844099aeb7b2f3fa18 to your computer and use it in GitHub Desktop.
named-constructors to the rescue
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
declare(strict_types=1); | |
namespace Example; | |
final class Geolocation | |
{ | |
private float $latitude; | |
private float $longitude; | |
public function __construct(float $latitude, float $longitude) | |
{ | |
$this->latitude = $latitude; | |
$this->longitude = $longitude; | |
} | |
public function latitude(): float | |
{ | |
return $this->latitude; | |
} | |
public function longitude(): float | |
{ | |
return $this->latitude; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$latitude = Geolocation::fromDMM("40° 26.767′ N 79° 58.933′ W")->latitude(); | |
$geoloc2 = Geolocation::fromCoordinates(38.1256, -25.1235); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
declare(strict_types=1); | |
namespace Example; | |
use InvalidArgumentException; | |
final class Geolocation | |
{ | |
private const REGEXP_DDM = 'a-regexp-to-parse-a-ddm-string'; | |
private float $latitude; | |
private float $longitude; | |
public static function fromDDM(string $coordinates): self | |
{ | |
if (!preg_match(self::REGEXP_DDM, $coordinates, $match)) { | |
throw new InvalidArgumentException('The coordinate are invalid'); | |
} | |
//obviously this part will be more complicated... | |
$latitude = $match[1]; | |
$longitude = $match[2]; | |
return self::fromCoordinates($latitude, $longitude); | |
} | |
public static function fromCoordinates($latitude, $longitude): self | |
{ | |
//$latitude and $longitude filtering happens here | |
return new self($latitude, $longitude); | |
} | |
private function __construct(float $latitude, float $longitude) | |
{ | |
$this->latitude = $latitude; | |
$this->longitude = $longitude; | |
} | |
public function latitude(): float | |
{ | |
return $this->latitude; | |
} | |
public function longitude(): float | |
{ | |
return $this->latitude; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
declare(strict_types=1); | |
namespace Example; | |
use InvalidArgumentException; | |
final class Geolocation | |
{ | |
private const REGEXP_DDM = 'a-regexp-to-parse-a-ddm-string'; | |
public static function fromDDM(string $coordinates): self | |
{ | |
if (!preg_match(self::REGEXP_DDM, $coordinates, $match)) { | |
throw new InvalidArgumentException('The coordinate do not represent a valid DDM string'); | |
} | |
//obviously this part will be more complicated... | |
$latitude = $match[1]; | |
$longitude = $match[2]; | |
return new self($latitude, $longitude); | |
} | |
//... rest of the code stay unchanged | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
declare(strict_types=1); | |
namespace Example; | |
final class Geolocation | |
{ | |
private float $latitude; | |
private float $longitude; | |
public function __construct(float $latitude, float $longitude) | |
{ | |
$this->latitude = $this->filterLatitude($latitude); | |
$this->longitude = $this->filterLongitude($longitude); | |
} | |
private function filterLatitude(float $latitude): float | |
{ | |
//filtering logic | |
return $latitude; | |
} | |
private function filterLongitude(float $longitude): float | |
{ | |
//filtering logic | |
return $longitude; | |
} | |
//... rest of the code stay unchanged | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$geoloc1 = Geolocation::fromTenTen("MED 8FV N9K5"); | |
$geoloc2 = Geolocation::fromGeoHash("u1513y"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$geoloc1 = Geolocation::make("MED 8FV N9K5"); | |
$geoloc2 = Geolocation::make("u1513y"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$content = (string) $request->getBody(); | |
$geoloc = Geolocation::fromJsonCoordinates($content); | |
//and the Json String should be clearly specify at the name constructor definition site. | |
// string should be "[5.142358,-4.12458]" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$geoloc = Geolocation::fromRequest($request); | |
//where $request is a Psr\Http\Message\RequestInterface; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment