Skip to content

Commit

Permalink
Add validation rules for geographical coordinates
Browse files Browse the repository at this point in the history
  • Loading branch information
olivervogel committed May 26, 2024
1 parent 31b2e6d commit 33a545e
Show file tree
Hide file tree
Showing 9 changed files with 209 additions and 0 deletions.
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,24 @@ The given value must be formatted in [Kebab case](https://en.wikipedia.org/wiki/

public Intervention\Validation\Rules\Kebabcase::__construct()

### Latitude

Checks for a valid geographic [Latitude](https://en.wikipedia.org/wiki/Latitude).

public Intervention\Validation\Rules\Latitude::__construct()

### Longitude

Checks for a valid geographic [Longitude](https://en.wikipedia.org/wiki/Longitude).

public Intervention\Validation\Rules\Longitude::__construct()

### LatLng

Checks for a valid geographic comma separated pair of a [Latitude](https://en.wikipedia.org/wiki/Latitude) and a [Longitude](https://en.wikipedia.org/wiki/Longitude).

public Intervention\Validation\Rules\LatLng::__construct()

### Lower case string

The given value must be all lower case letters.
Expand Down
18 changes: 18 additions & 0 deletions src/Rules/LatLng.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

declare(strict_types=1);

namespace Intervention\Validation\Rules;

use Intervention\Validation\AbstractRegexRule;

class LatLng extends AbstractRegexRule
{
protected function pattern(): string
{
$lat = substr((new Latitude())->pattern(), 2, -2);
$lng = substr((new Longitude())->pattern(), 2, -2);

return "/^" . $lat . ", ?" . $lng . "$/";
}
}
15 changes: 15 additions & 0 deletions src/Rules/Latitude.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

namespace Intervention\Validation\Rules;

use Intervention\Validation\AbstractRegexRule;

class Latitude extends AbstractRegexRule
{
protected function pattern(): string
{
return "/^[+-]?(90(\.0+)?|[1-8]?\d(\.\d+)?)$/";
}
}
15 changes: 15 additions & 0 deletions src/Rules/Longitude.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

namespace Intervention\Validation\Rules;

use Intervention\Validation\AbstractRegexRule;

class Longitude extends AbstractRegexRule
{
protected function pattern(): string
{
return "/^[+-]?(180(\.0+)?|1[0-7]\d(\.\d+)?|\d{1,2}(\.\d+)?)\)?$/";
}
}
3 changes: 3 additions & 0 deletions src/lang/de/validation.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,7 @@
'mimetype' => 'Der Wert :attribute einhält keinen gültigen Internet Media Type (MIME-Type).',
'austrianinsurancenumber' => 'Der Wert :attribute enthält keine gültige österreichische Versicherungsnummer',
'grid' => 'Der Wert :attribute enthält keinen gültige Global Release Identifier (GRid).',
'latitude' => 'Der Wert :attribute enthält keine gültige geographische Breite.',
'longitude' => 'Der Wert :attribute enthält keine gültige geographische Länge.',
'latlng' => 'Der Wert :attribute enthält keine gültige geographischen Koordinaten.',
];
3 changes: 3 additions & 0 deletions src/lang/en/validation.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,7 @@
'mimetype' => 'The value :attribute does not contain a valid Internet Media Type (MIME-Type).',
'austrianinsurancenumber' => 'The value :attribute does not contain a valid austrian insurance number.',
'grid' => 'The value :attribute must be a valid Global Release Identifier (GRid).',
'latitude' => 'The value :attribute must be a valid geographical latitude.',
'longitude' => 'The value :attribute must be a valid geographical longitude.',
'latlng' => 'The value :attribute must be valid geographical coordinates.',
];
59 changes: 59 additions & 0 deletions tests/Rules/LatLngTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

declare(strict_types=1);

namespace Intervention\Validation\Tests\Rules;

use PHPUnit\Framework\Attributes\DataProvider;
use Intervention\Validation\Rules\LatLng;
use PHPUnit\Framework\TestCase;

final class LatLngTest extends TestCase
{
#[DataProvider('dataProvider')]
public function testValidation($result, $value): void
{
$valid = (new LatLng())->isValid($value);
$this->assertEquals($result, $valid);
}

public static function dataProvider(): array
{
return [
[true, '0, 0'],
[true, '-80, 90'],
[true, '0, +90'],
[true, '+50, -180'],
[true, '90, 180'],
[true, '-19.123, +180'],
[true, '0.11111, 12.123'],
[true, '+89.00000, +12.123'],
[true, '0.00000, -12.123'],
[true, '+89.00000, -90'],
[true, '89.99999, +90'],
[true, '90, 150'],
[true, '-90, 34.112311'],
[true, '0,0'],
[true, '-80,90'],
[true, '0,+90'],
[true, '+50,-180'],
[true, '90,180'],
[true, '-19.123,+180'],
[true, '0.11111,12.123'],
[true, '+89.00000,+12.123'],
[true, '0.00000,-12.123'],
[true, '+89.00000,-90'],
[true, '89.99999,+90'],
[true, '90,150'],
[true, '-90,34.112311'],
[false, '91, 0'],
[false, '-91, 9'],
[false, '90.000001, 0'],
[false, '-90.000001, 0'],
[false, '0, -200'],
[false, '0, -180.12'],
[false, '0, 180.12'],
[false, '0, +180.12'],
];
}
}
41 changes: 41 additions & 0 deletions tests/Rules/LatitudeTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

declare(strict_types=1);

namespace Intervention\Validation\Tests\Rules;

use PHPUnit\Framework\Attributes\DataProvider;
use Intervention\Validation\Rules\Latitude;
use PHPUnit\Framework\TestCase;

final class LatitudeTest extends TestCase
{
#[DataProvider('dataProvider')]
public function testValidation($result, $value): void
{
$valid = (new Latitude())->isValid($value);
$this->assertEquals($result, $valid);
}

public static function dataProvider(): array
{
return [
[true, '-80'],
[true, '0'],
[true, '+50'],
[true, '90'],
[true, '-19.123'],
[true, '0.11111'],
[true, '+89.00000'],
[true, '0.00000'],
[true, '+89.00000'],
[true, '89.99999'],
[true, '90'],
[true, '-90'],
[false, '91'],
[false, '-91'],
[false, '90.000001'],
[false, '-90.000001'],
];
}
}
37 changes: 37 additions & 0 deletions tests/Rules/LongitudeTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

declare(strict_types=1);

namespace Intervention\Validation\Tests\Rules;

use PHPUnit\Framework\Attributes\DataProvider;
use Intervention\Validation\Rules\Longitude;
use PHPUnit\Framework\TestCase;

final class LongitudeTest extends TestCase
{
#[DataProvider('dataProvider')]
public function testValidation($result, $value): void
{
$valid = (new Longitude())->isValid($value);
$this->assertEquals($result, $valid);
}

public static function dataProvider(): array
{
return [
[true, '0'],
[true, '+90'],
[true, '-90'],
[true, '90'],
[true, '+90.0000001'],
[true, '-90.0000001'],
[true, '90.00000001'],
[true, '+180'],
[true, '-180'],
[true, '180'],
[false, '180.0001'],
[false, '-180.0001'],
];
}
}

0 comments on commit 33a545e

Please sign in to comment.