32 lines
1.1 KiB
PHP
32 lines
1.1 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Domain\Models\ValueObjects;
|
|
|
|
use Webmozart\Assert\Assert;
|
|
|
|
final readonly class Coordinates
|
|
{
|
|
public function __construct(
|
|
public float $latitude,
|
|
public float $longitude,
|
|
) {
|
|
Assert::greaterThanEq($this->latitude, -90.0, 'Latitude must be between -90 and 90 degrees.');
|
|
Assert::lessThanEq($this->latitude, 90.0, 'Latitude must be between -90 and 90 degrees.');
|
|
Assert::greaterThanEq($this->longitude, -180.0, 'Longitude must be between -180 and 180 degrees.');
|
|
Assert::lessThanEq($this->longitude, 180.0, 'Longitude must be between -180 and 180 degrees.');
|
|
}
|
|
|
|
public static function tryFrom(?string $latitude, ?string $longitude): ?self
|
|
{
|
|
if ($latitude === null || $longitude === null || trim($latitude) === '' || trim($longitude) === '') {
|
|
return null;
|
|
}
|
|
|
|
Assert::numeric($latitude, 'Latitude must be numeric.');
|
|
Assert::numeric($longitude, 'Longitude must be numeric.');
|
|
|
|
return new self((float) $latitude, (float) $longitude);
|
|
}
|
|
}
|