Skip to content

Commit

Permalink
Adding geo-coordinate validation to the Validation class.
Browse files Browse the repository at this point in the history
  • Loading branch information
Florian Krämer committed Aug 4, 2015
1 parent c79a106 commit 9c8ffbc
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 1 deletion.
43 changes: 42 additions & 1 deletion src/Validation/Validation.php
Expand Up @@ -34,7 +34,9 @@ class Validation
* @var array
*/
protected static $_pattern = [
'hostname' => '(?:[_\p{L}0-9][-_\p{L}0-9]*\.)*(?:[\p{L}0-9][-\p{L}0-9]{0,62})\.(?:(?:[a-z]{2}\.)?[a-z]{2,})'
'hostname' => '(?:[_\p{L}0-9][-_\p{L}0-9]*\.)*(?:[\p{L}0-9][-\p{L}0-9]{0,62})\.(?:(?:[a-z]{2}\.)?[a-z]{2,})',
'latitude' => '[-+]?([1-8]?\d(\.\d+)?|90(\.0+)?)',
'longitude' => '[-+]?(180(\.0+)?|((1[0-7]\d)|([1-9]?\d))(\.\d+)?)',
];

/**
Expand Down Expand Up @@ -1020,6 +1022,45 @@ public static function uploadedFile($file, array $options = [])
return true;
}

/**
* Validates a geographic coordinate.
*
* Supported formats are right now:
*
* - <latitude>, <longitude> Example: -25.274398, 133.775136
*
* ### Options
*
* - `type` - A string of the coordinate format, right now only `longLat`.
* - `longLat` - By default `both`, can be `long` and `lat` as well to validate
* only a part of the coordinate.
*
* @param string Geographic location as string
* @param array $options Options for the validation logic.
* @return boolean
*/
public static function geoCoordinate($value, array $options = [])
{
$options += [
'longLat' => 'both',
'type' => 'longLat'
];
if ($options['type'] === 'longLat') {
if ($options['longLat'] === 'both') {
$pattern = '/^' . self::$_pattern['latitude'] . ',\s*' . self::$_pattern['longitude'] . '$/';
}
if ($options['longLat'] === 'long') {
$pattern = '/^' . self::$_pattern['longitude'] . '$/';
}
if ($options['longLat'] === 'lat') {
$pattern = '/^' . self::$_pattern['latitude'] . '$/';
}
} else {
throw new \RuntimeException(sprintf('Unsupported coordinate type "%s".', $options['type']));
}
return (bool)preg_match($pattern, $value);
}

/**
* Converts an array representing a date or datetime into a ISO string.
* The arrays are typically sent for validation from a form generated by
Expand Down
15 changes: 15 additions & 0 deletions tests/TestCase/Validation/ValidationTest.php
Expand Up @@ -2490,4 +2490,19 @@ public function testCompareWith()
$context = [];
$this->assertFalse(Validation::compareWith('a value', 'other', $context));
}

/**
* Test the geoCoordinate method.
*
* @return void
*/
public function testGeoCoordinate()
{
$this->assertTrue(Validation::geoCoordinate('51.165691, 10.451526'));
$this->assertTrue(Validation::geoCoordinate('-25.274398, 133.775136'));
$this->assertFalse(Validation::geoCoordinate('51.165691 10.451526'));
$this->assertFalse(Validation::geoCoordinate('-245.274398, -133.775136'));
$this->assertTrue(Validation::geoCoordinate('51.165691', ['longLat' => 'lat']));
$this->assertTrue(Validation::geoCoordinate('10.451526', ['longLat' => 'long']));
}
}

0 comments on commit 9c8ffbc

Please sign in to comment.