Skip to content

Commit

Permalink
#7170 Added the long/lat validation methods.
Browse files Browse the repository at this point in the history
  • Loading branch information
Florian Krämer committed Aug 5, 2015
1 parent f766996 commit bfb8265
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 17 deletions.
64 changes: 47 additions & 17 deletions src/Validation/Validation.php
Expand Up @@ -1022,23 +1022,23 @@ 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 $value Geographic location as string
* @param array $options Options for the validation logic.
* @return bool
*/
/**
* 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 $value Geographic location as string
* @param array $options Options for the validation logic.
* @return bool
*/
public static function geoCoordinate($value, array $options = [])
{
$options += [
Expand All @@ -1061,6 +1061,36 @@ public static function geoCoordinate($value, array $options = [])
return (bool)preg_match($pattern, $value);
}

/**
* Convenience method for latitude validation.
*
* @link https://en.wikipedia.org/wiki/Latitude
* @see Validation::geoCoordinate()
* @param string $value Latitude as string
* @param array $options Options for the validation logic.
* @return bool
*/
public static function latitude($value, array $options = [])
{
$options['latLong'] = 'lat';
return self::geoCoordinate($value, $options);
}

/**
* Convenience method for longitude validation.
*
* @link https://en.wikipedia.org/wiki/Longitude
* @see Validation::geoCoordinate()
* @param string $value Latitude as string
* @param array $options Options for the validation logic.
* @return bool
*/
public static function longitude($value, array $options = [])
{
$options['latLong'] = 'long';
return self::geoCoordinate($value, $options);
}

/**
* 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
22 changes: 22 additions & 0 deletions tests/TestCase/Validation/ValidationTest.php
Expand Up @@ -2505,4 +2505,26 @@ public function testGeoCoordinate()
$this->assertTrue(Validation::geoCoordinate('51.165691', ['latLong' => 'lat']));
$this->assertTrue(Validation::geoCoordinate('10.451526', ['latLong' => 'long']));
}

/**
* Test the geoCoordinate method.
*
* @return void
*/
public function testLatitude()
{
$this->assertTrue(Validation::latitude('51.165691'));
$this->assertFalse(Validation::latitude('200.23552'));
}

/**
* Test the geoCoordinate method.
*
* @return void
*/
public function testLongitude()
{
$this->assertTrue(Validation::longitude('10.451526'));
$this->assertFalse(Validation::longitude('-190.52236'));
}
}

0 comments on commit bfb8265

Please sign in to comment.