Jeroen De Dauw has uploaded a new change for review.
https://gerrit.wikimedia.org/r/58926
Change subject: Added FloatCoordinateParser
......................................................................
Added FloatCoordinateParser
Change-Id: I569a5d563fb3c297af2041fceed7d8593f6935c7
---
M ValueParsers/ValueParsers.classes.php
M ValueParsers/ValueParsers.mw.php
A ValueParsers/includes/parsers/FloatCoordinateParser.php
M ValueParsers/tests/includes/parsers/DmsCoordinateParserTest.php
A ValueParsers/tests/includes/parsers/FloatCoordinateParserTest.php
5 files changed, 286 insertions(+), 2 deletions(-)
git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/DataValues
refs/changes/26/58926/1
diff --git a/ValueParsers/ValueParsers.classes.php
b/ValueParsers/ValueParsers.classes.php
index 088459b..a93708b 100644
--- a/ValueParsers/ValueParsers.classes.php
+++ b/ValueParsers/ValueParsers.classes.php
@@ -38,6 +38,7 @@
'ValueParsers\BoolParser' => 'includes/parsers/BoolParser.php',
'ValueParsers\DmsCoordinateParser' =>
'includes/parsers/DmsCoordinateParser.php',
+ 'ValueParsers\FloatCoordinateParser' =>
'includes/parsers/FloatCoordinateParser.php',
'ValueParsers\GeoCoordinateParser' =>
'includes/parsers/GeoCoordinateParser.php',
'ValueParsers\FloatParser' => 'includes/parsers/FloatParser.php',
'ValueParsers\IntParser' => 'includes/parsers/IntParser.php',
diff --git a/ValueParsers/ValueParsers.mw.php b/ValueParsers/ValueParsers.mw.php
index 5f3538e..3fdf2da 100644
--- a/ValueParsers/ValueParsers.mw.php
+++ b/ValueParsers/ValueParsers.mw.php
@@ -70,6 +70,7 @@
'includes/parsers/BoolParser',
'includes/parsers/DmsCoordinateParser',
+ 'includes/parsers/FloatCoordinateParser',
'includes/parsers/GeoCoordinateParser',
'includes/parsers/FloatParser',
'includes/parsers/IntParser',
diff --git a/ValueParsers/includes/parsers/FloatCoordinateParser.php
b/ValueParsers/includes/parsers/FloatCoordinateParser.php
new file mode 100644
index 0000000..d6041eb
--- /dev/null
+++ b/ValueParsers/includes/parsers/FloatCoordinateParser.php
@@ -0,0 +1,186 @@
+<?php
+
+namespace ValueParsers;
+
+use DataValues\GeoCoordinateValue;
+use LogicException;
+
+/**
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ * http://www.gnu.org/copyleft/gpl.html
+ *
+ * @since 0.1
+ *
+ * @file
+ * @ingroup ValueParsers
+ *
+ * @licence GNU GPL v2+
+ * @author Jeroen De Dauw < [email protected] >
+ */
+class FloatCoordinateParser extends StringValueParser {
+
+ /**
+ * The symbols representing the different directions for usage in
directional notation.
+ * @since 0.1
+ */
+ const OPT_NORTH_SYMBOL = 'north';
+ const OPT_EAST_SYMBOL = 'east';
+ const OPT_SOUTH_SYMBOL = 'south';
+ const OPT_WEST_SYMBOL = 'west';
+
+ /**
+ * The symbol to use as separator between latitude and longitude.
+ * @since 0.1
+ */
+ const OPT_SEPARATOR_SYMBOL = 'separator';
+
+ /**
+ * @since 0.1
+ *
+ * @param ParserOptions|null $options
+ */
+ public function __construct( ParserOptions $options = null ) {
+ parent::__construct( $options );
+
+ $this->defaultOption( self::OPT_NORTH_SYMBOL, 'N' );
+ $this->defaultOption( self::OPT_EAST_SYMBOL, 'E' );
+ $this->defaultOption( self::OPT_SOUTH_SYMBOL, 'S' );
+ $this->defaultOption( self::OPT_WEST_SYMBOL, 'W' );
+
+ $this->defaultOption( self::OPT_SEPARATOR_SYMBOL, ',' );
+ }
+
+ /**
+ * @see StringValueParser::stringParse
+ *
+ * @since 0.1
+ *
+ * @param string $value
+ *
+ * @return GeoCoordinateValue
+ * @throws ParseException
+ * @throws LogicException
+ */
+ protected function stringParse( $value ) {
+ $value = $this->removeInvalidChars( $value );
+
+ if ( !$this->areFloatCoordinates( $value ) ) {
+ throw new ParseException( 'Not a geographical
coordinate in float format' );
+ }
+
+ $coordinates = explode( $this->getOption(
self::OPT_SEPARATOR_SYMBOL ), $value );
+
+ if ( count( $coordinates ) !== 2 ) {
+ throw new LogicException( 'A coordinates string with an
incorrect segment count has made it through validation' );
+ }
+
+ list( $latitude, $longitude ) = $coordinates;
+
+ $latitude = $this->getParsedCoordinate( $latitude );
+ $longitude = $this->getParsedCoordinate( $longitude );
+
+ $coordinate = new GeoCoordinateValue( $latitude, $longitude );
+
+ return $coordinate;
+ }
+
+ /**
+ * Parsers a single coordinate (either latitude or longitude) and
returns it as a float.
+ *
+ * @since 0.1
+ *
+ * @param string $coordinate
+ *
+ * @return float
+ */
+ protected function getParsedCoordinate( $coordinate ) {
+ return (float)$this->resolveDirection( $coordinate );
+ }
+
+ /**
+ * Turns directional notation (N/E/S/W) of a single coordinate into
non-directional notation (+/-).
+ * This method assumes there are no preceding or tailing spaces.
+ *
+ * @since 0.1
+ *
+ * @param string $coordinate
+ *
+ * @return string
+ */
+ protected function resolveDirection( $coordinate ) {
+ // Get the last char, which could be a direction indicator
+ $lastChar = strtoupper( substr( $coordinate, -1 ) );
+
+ $n = $this->getOption( self::OPT_NORTH_SYMBOL );
+ $e = $this->getOption( self::OPT_EAST_SYMBOL );
+ $s = $this->getOption( self::OPT_SOUTH_SYMBOL );
+ $w = $this->getOption( self::OPT_WEST_SYMBOL );
+
+ // If there is a direction indicator, remove it, and prepend a
minus sign for south and west directions.
+ // If there is no direction indicator, the coordinate is
already non-directional and no work is required.
+ if ( in_array( $lastChar, array( $n, $e, $s, $w ) ) ) {
+ $coordinate = substr( $coordinate, 0, -1 );
+
+ if ( in_array( $lastChar, array( $s, $w ) ) ) {
+ $coordinate = '-' . $coordinate;
+ }
+ }
+
+ return $coordinate;
+ }
+
+ /**
+ * Returns a string with whitespace, control characters and characters
with ASCII values above 126 removed.
+ *
+ * @since 0.1
+ *
+ * @param string $string
+ *
+ * @return string
+ */
+ protected function removeInvalidChars( $string ) {
+ $filtered = array();
+
+ foreach ( str_split( $string ) as $character ) {
+ $asciiValue = ord( $character );
+
+ if ( ( $asciiValue > 32 && $asciiValue < 127 ) ||
$asciiValue == 194 || $asciiValue == 176 ) {
+ $filtered[] = $character;
+ }
+ }
+
+ return implode( '', $filtered );
+ }
+
+ /**
+ * returns whether the coordinates are in float representation.
+ * TODO: nicify
+ *
+ * @since 0.1
+ *
+ * @param string $coordinates
+ *
+ * @return boolean
+ */
+ protected function areFloatCoordinates( $coordinates ) {
+ $sep = $this->getOption( self::OPT_SEPARATOR_SYMBOL );
+
+ $match = preg_match( '/^(-)?\d{1,3}(\.\d{1,20})?' . $sep .
'(-)?\d{1,3}(\.\d{1,20})?$/i', $coordinates ) // Non-directional
+ || preg_match( '/^\d{1,3}(\.\d{1,20})?(N|S)' . $sep .
'\d{1,3}(\.\d{1,20})?(E|W)$/i', $coordinates ); // Directional;
+
+ return $match;
+ }
+
+}
diff --git a/ValueParsers/tests/includes/parsers/DmsCoordinateParserTest.php
b/ValueParsers/tests/includes/parsers/DmsCoordinateParserTest.php
index e485793..3b3ea25 100644
--- a/ValueParsers/tests/includes/parsers/DmsCoordinateParserTest.php
+++ b/ValueParsers/tests/includes/parsers/DmsCoordinateParserTest.php
@@ -6,7 +6,7 @@
use ValueParsers\DmsCoordinateParser;
/**
- * Unit tests for the ValueParsers\DmsGeoCoordinateValue class.
+ * Unit tests for the ValueParsers\DmsCoordinateValue class.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@@ -35,7 +35,7 @@
* @licence GNU GPL v2+
* @author Jeroen De Dauw < [email protected] >
*/
-class DmsGeoCoordinateParserTest extends StringValueParserTest {
+class DmsCoordinateParserTest extends StringValueParserTest {
/**
* @see ValueParserTestBase::validInputProvider
diff --git a/ValueParsers/tests/includes/parsers/FloatCoordinateParserTest.php
b/ValueParsers/tests/includes/parsers/FloatCoordinateParserTest.php
new file mode 100644
index 0000000..7d29318
--- /dev/null
+++ b/ValueParsers/tests/includes/parsers/FloatCoordinateParserTest.php
@@ -0,0 +1,96 @@
+<?php
+
+namespace ValueParsers\Test;
+
+use DataValues\GeoCoordinateValue;
+use ValueParsers\FloatCoordinateParser;
+
+/**
+ * Unit tests for the ValueParsers\FloatCoordinateValue class.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ * http://www.gnu.org/copyleft/gpl.html
+ *
+ * @file
+ * @since 0.1
+ *
+ * @ingroup ValueParsersTest
+ *
+ * @group ValueParsers
+ * @group DataValueExtensions
+ * @group GeoCoordinateParserTest
+ *
+ * @licence GNU GPL v2+
+ * @author Jeroen De Dauw < [email protected] >
+ */
+class FloatCoordinateParserTest extends StringValueParserTest {
+
+ /**
+ * @see ValueParserTestBase::validInputProvider
+ *
+ * @since 0.1
+ *
+ * @return array
+ */
+ public function validInputProvider() {
+ $argLists = array();
+
+ // TODO: test with different parser options
+
+ $valid = array(
+ '55.7557860 N, 37.6176330 W' => array( 55.7557860,
-37.6176330 ),
+ '55.7557860, -37.6176330' => array( 55.7557860,
-37.6176330 ),
+ '55 S, 37.6176330 W' => array( -55, -37.6176330 ),
+ '-55, -37.6176330' => array( -55, -37.6176330 ),
+ '5.5S,37W ' => array( -5.5, -37 ),
+ '-5.5,-37 ' => array( -5.5, -37 ),
+ '4,2' => array( 4, 2 ),
+ );
+
+ foreach ( $valid as $value => $expected ) {
+ $expected = new GeoCoordinateValue( $expected[0],
$expected[1] );
+ $argLists[] = array( (string)$value, $expected );
+ }
+
+ return $argLists;
+ }
+
+ public function invalidInputProvider() {
+ $argLists = parent::invalidInputProvider();
+
+ $invalid = array(
+ '~=[,,_,,]:3',
+ 'ohi there',
+ );
+
+ foreach ( $invalid as $value ) {
+ $argLists[] = array( $value );
+ }
+
+ return $argLists;
+ }
+
+ /**
+ * @see ValueParserTestBase::getParserClass
+ *
+ * @since 0.1
+ *
+ * @return string
+ */
+ protected function getParserClass() {
+ return 'ValueParsers\FloatCoordinateParser';
+ }
+
+}
--
To view, visit https://gerrit.wikimedia.org/r/58926
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: newchange
Gerrit-Change-Id: I569a5d563fb3c297af2041fceed7d8593f6935c7
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/DataValues
Gerrit-Branch: master
Gerrit-Owner: Jeroen De Dauw <[email protected]>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits