Jeroen De Dauw has uploaded a new change for review. https://gerrit.wikimedia.org/r/79778
Change subject: Added LatLongValue ...................................................................... Added LatLongValue Change-Id: Ifa0c008d1c60708ed7885369b5ce49295e3da765 --- M DataValues/DataValues.classes.php A DataValues/includes/values/LatLongValue.php A DataValues/tests/phpunit/includes/values/LatLongValueTest.php 3 files changed, 320 insertions(+), 0 deletions(-) git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/DataValues refs/changes/78/79778/1 diff --git a/DataValues/DataValues.classes.php b/DataValues/DataValues.classes.php index d88aa23..2df21eb 100644 --- a/DataValues/DataValues.classes.php +++ b/DataValues/DataValues.classes.php @@ -15,6 +15,7 @@ 'DataValues\BooleanValue' => 'includes/values/BooleanValue.php', 'DataValues\GeoCoordinateValue' => 'includes/values/GeoCoordinateValue.php', 'DataValues\IriValue' => 'includes/values/IriValue.php', + 'DataValues\LatLongValue' => 'includes/values/LatLongValue.php', 'DataValues\MonolingualTextValue' => 'includes/values/MonolingualTextValue.php', 'DataValues\MultilingualTextValue' => 'includes/values/MultilingualTextValue.php', 'DataValues\NumberValue' => 'includes/values/NumberValue.php', diff --git a/DataValues/includes/values/LatLongValue.php b/DataValues/includes/values/LatLongValue.php new file mode 100644 index 0000000..9da75dc --- /dev/null +++ b/DataValues/includes/values/LatLongValue.php @@ -0,0 +1,208 @@ +<?php + +namespace DataValues; + +use InvalidArgumentException; +use OutOfRangeException; + +/** + * Object representing a geographic point. + * + * Latitude is specified in degrees within the range [-90, 90]. + * Longitude is specified in degrees within the range [-180, 180]. + * + * @since 0.1 + * + * @file + * @ingroup DataValue + * + * @licence GNU GPL v2+ + * @author Jeroen De Dauw < jeroended...@gmail.com > + */ +class LatLongValue extends DataValueObject { + + /** + * The locations latitude. + * + * @since 0.1 + * + * @var float + */ + protected $latitude; + + /** + * The locations longitude. + * + * @since 0.1 + * + * @var float + */ + protected $longitude; + + /** + * @since 0.1 + * + * @param float|int $latitude + * @param float|int $longitude + * + * @throws InvalidArgumentException + */ + public function __construct( $latitude, $longitude ) { + if ( is_int( $latitude ) ) { + $latitude = (float)$latitude; + } + + if ( is_int( $longitude ) ) { + $longitude = (float)$longitude; + } + + $this->assertIsLatitude( $latitude ); + $this->assertIsLongitude( $longitude ); + + $this->latitude = $latitude; + $this->longitude = $longitude; + } + + protected function assertIsLatitude( $latitude ) { + if ( !is_float( $latitude ) ) { + throw new InvalidArgumentException( 'Can only construct LatLongValue with a numeric latitude' ); + } + + if ( $latitude < -90 || $latitude > 90 ) { + throw new OutOfRangeException( 'Latitude needs to be between -90 and 90' ); + } + } + + protected function assertIsLongitude( $longitude ) { + if ( !is_float( $longitude ) ) { + throw new InvalidArgumentException( 'Can only construct LatLongValue with a numeric longitude' ); + } + + if ( $longitude < -180 || $longitude > 180 ) { + throw new OutOfRangeException( 'Longitude needs to be between -180 and 180' ); + } + } + + /** + * @see Serializable::serialize + * + * @since 0.1 + * + * @return string + */ + public function serialize() { + $data = array( + $this->latitude, + $this->longitude + ); + + return implode( '|', $data ); + } + + /** + * @see Serializable::unserialize + * + * @since 0.1 + * + * @param string $value + * + * @return LatLongValue + * @throws InvalidArgumentException + */ + public function unserialize( $value ) { + $data = explode( '|', $value, 2 ); + + if ( count( $data ) < 2 ) { + throw new InvalidArgumentException( 'Invalid serialization provided in ' . __METHOD__ ); + } + + $this->__construct( + (float)$data[0], + (float)$data[1] + ); + } + + /** + * @see DataValue::getType + * + * @since 0.1 + * + * @return string + */ + public static function getType() { + return 'geocoordinate'; + } + + /** + * @see DataValue::getSortKey + * + * @since 0.1 + * + * @return string|float|int + */ + public function getSortKey() { + return $this->latitude; + } + + /** + * @see DataValue::getValue + * + * @since 0.1 + * + * @return GeoCoordinateValue + */ + public function getValue() { + return $this; + } + + /** + * Returns the latitude. + * + * @since 0.1 + * + * @return float + */ + public function getLatitude() { + return $this->latitude; + } + + /** + * Returns the longitude. + * + * @since 0.1 + * + * @return float + */ + public function getLongitude() { + return $this->longitude; + } + + /** + * @see DataValue::getArrayValue + * + * @since 0.1 + * + * @return mixed + */ + public function getArrayValue() { + return array( + 'latitude' => $this->latitude, + 'longitude' => $this->longitude + ); + } + + /** + * Constructs a new instance of the DataValue from the provided data. + * This can round-trip with @see getArrayValue + * + * @since 0.1 + * + * @param array $data + * + * @return LatLongValue + */ + public static function newFromArray( array $data ) { + return new static( $data['latitude'], $data['longitude'] ); + } + +} diff --git a/DataValues/tests/phpunit/includes/values/LatLongValueTest.php b/DataValues/tests/phpunit/includes/values/LatLongValueTest.php new file mode 100644 index 0000000..aeda056 --- /dev/null +++ b/DataValues/tests/phpunit/includes/values/LatLongValueTest.php @@ -0,0 +1,111 @@ +<?php + +namespace DataValues\Test; + +use DataValues\GeoCoordinateValue; +use DataValues\LatLongValue; + +/** + * @covers DataValues\LatLongValue + * + * @file + * @since 0.1 + * + * @ingroup DataValue + * + * @group DataValue + * @group DataValueExtensions + * + * @licence GNU GPL v2+ + * @author Jeroen De Dauw < jeroended...@gmail.com > + */ +class LatLongValueTest extends DataValueTest { + + /** + * @see DataValueTest::getClass + * + * @since 0.1 + * + * @return string + */ + public function getClass() { + return 'DataValues\LatLongValue'; + } + + /** + * @see DataValueTest::constructorProvider + * + * @since 0.1 + * + * @return array + */ + public function constructorProvider() { + $argLists = array(); + + $argLists[] = array( false ); + $argLists[] = array( false, 42 ); + $argLists[] = array( false, array() ); + $argLists[] = array( false, false ); + $argLists[] = array( false, true ); + $argLists[] = array( false, null ); + $argLists[] = array( false, 'foo' ); + $argLists[] = array( false, 42 ); + + $argLists[] = array( false, 'en', 42 ); + $argLists[] = array( false, 'en', 4.2 ); + $argLists[] = array( false, 42, false ); + $argLists[] = array( false, 42, array() ); + $argLists[] = array( false, 42, null ); + $argLists[] = array( false, 42, 'foo' ); + $argLists[] = array( false, 4.2, 'foo' ); + + $argLists[] = array( false, '4.2', 4.2 ); + $argLists[] = array( false, '4.2', '4.2' ); + $argLists[] = array( false, 4.2, '4.2' ); + $argLists[] = array( false, '42', 42 ); + $argLists[] = array( false, 42, '42' ); + $argLists[] = array( false, '0', 0 ); + + $argLists[] = array( false, -91, 0 ); + $argLists[] = array( false, -999, 1 ); + $argLists[] = array( false, 90.001, 2 ); + $argLists[] = array( false, 3, 181 ); + $argLists[] = array( false, 4, -1337 ); + + $argLists[] = array( true, 4.2, 4.2 ); + $argLists[] = array( true, 4.2, 42 ); + $argLists[] = array( true, 42, 4.2 ); + $argLists[] = array( true, 42, 42 ); + $argLists[] = array( true, -4.2, -4.2 ); + $argLists[] = array( true, 4.2, -42 ); + $argLists[] = array( true, -42, 4.2 ); + $argLists[] = array( true, 0, 0 ); + + return $argLists; + } + + /** + * @dataProvider instanceProvider + * @param LatLongValue $geoCoord + * @param array $arguments + */ + public function testGetLatitude( LatLongValue $geoCoord, array $arguments ) { + $actual = $geoCoord->getLatitude(); + + $this->assertInternalType( 'float', $actual ); + $this->assertEquals( (float)$arguments[0], $actual ); + } + + /** + * @dataProvider instanceProvider + * @param LatLongValue $geoCoord + * @param array $arguments + */ + public function testGetLongitude( LatLongValue $geoCoord, array $arguments ) { + $actual = $geoCoord->getLongitude(); + + $this->assertInternalType( 'float', $actual ); + $this->assertEquals( (float)$arguments[1], $actual ); + } + +} -- To view, visit https://gerrit.wikimedia.org/r/79778 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: newchange Gerrit-Change-Id: Ifa0c008d1c60708ed7885369b5ce49295e3da765 Gerrit-PatchSet: 1 Gerrit-Project: mediawiki/extensions/DataValues Gerrit-Branch: master Gerrit-Owner: Jeroen De Dauw <jeroended...@gmail.com> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits