-
-
Notifications
You must be signed in to change notification settings - Fork 68
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add validation rules for geographical coordinates
- Loading branch information
1 parent
31b2e6d
commit 33a545e
Showing
9 changed files
with
209 additions
and
0 deletions.
There are no files selected for viewing
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
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
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 . "$/"; | ||
} | ||
} |
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
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+)?)$/"; | ||
} | ||
} |
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
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+)?)\)?$/"; | ||
} | ||
} |
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
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
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
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'], | ||
]; | ||
} | ||
} |
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
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'], | ||
]; | ||
} | ||
} |
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
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'], | ||
]; | ||
} | ||
} |