Skip to content

Commit

Permalink
Validate coverage
Browse files Browse the repository at this point in the history
Creates a Validator class to help validate various elements. Currently
just a static class that better validates WKT geometry types

fixes #314 and #288
  • Loading branch information
Wayne Graham authored and Eric Rochester committed Jul 16, 2014
1 parent cccdb8d commit 81bf1eb
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 4 deletions.
10 changes: 6 additions & 4 deletions helpers/Coverage.php
Expand Up @@ -19,11 +19,13 @@ function nl_extractWkt($coverage) {
$wkt = null;

// Get coverage format.
$format = geoPHP::detectFormat($coverage);
if (Validator::isValidWkt($coverage)) {
$format = geoPHP::detectFormat($coverage);

// Convert / reduce to WKT.
if (in_array($format, array('wkt', 'kml'))) {
$wkt = geoPHP::load($coverage)->out('wkt');
// Convert / reduce to WKT.
if (in_array($format, array('wkt', 'kml'))) {
$wkt = geoPHP::load($coverage)->out('wkt');
}
}

return $wkt;
Expand Down
29 changes: 29 additions & 0 deletions helpers/Validators.php
@@ -0,0 +1,29 @@
<?php

/**
* Helper classes for validations
*
* @category Utilities
* @package Omeka
* @subpackage Neatline
* @author Scholars' Lab <scholarslab@virginia.edu>
* @copyright 2014 Rector and Board of Visitors, University of Virginia
* @license http://www.apache.org/licenses/LICENSE-2.0.html Apache 2
*/
class Validator
{
/**
* Tests if a given string contains valid WKT
*
* @param string $coverage Coverage string to test
*
* @return boolean if the string is found
*/
static function isValidWkt($coverage)
{
return (bool) preg_match(
'#(point|linestring|polygon|multipoint|multilinestring|multipolygon|geometrycollection)#',
strtolower($coverage)
);
}
}
1 change: 1 addition & 0 deletions plugin.php
Expand Up @@ -43,6 +43,7 @@
require_once NL_DIR.'/helpers/Schemas.php';
require_once NL_DIR.'/helpers/Strings.php';
require_once NL_DIR.'/helpers/Styles.php';
require_once NL_DIR.'/helpers/Validators.php';
require_once NL_DIR.'/helpers/Views.php';

// Vendor:
Expand Down
24 changes: 24 additions & 0 deletions tests/phpunit/tests/unit/Helpers/CoverageTest.php
@@ -0,0 +1,24 @@
<?php

/**
* @package omeka
* @subpackage neatline
* @copyright 2014 Rector and Board of Visitors, University of Virginia
* @license http://www.apache.org/licenses/LICENSE-2.0.html
*/

class HelpersTest_Coverage extends Neatline_Case_Default
{
/**
* `nl_explode` should split on ','.
*/
public function testExtractValueWKT()
{
$this->assertNotNull(nl_extractWkt('POINT (13 42)'));
}

public function testIgnoreNonWKT()
{
$this->assertNull(nl_extractWkt('Paris'));
}
}

0 comments on commit 81bf1eb

Please sign in to comment.