Skip to content

Commit

Permalink
Merge 08b631b into 7cdd921
Browse files Browse the repository at this point in the history
  • Loading branch information
JeroenDeDauw committed Oct 20, 2018
2 parents 7cdd921 + 08b631b commit ee492ab
Show file tree
Hide file tree
Showing 6 changed files with 356 additions and 395 deletions.
1 change: 1 addition & 0 deletions README.md
Expand Up @@ -107,6 +107,7 @@ employees for the [Wikidata project](https://wikidata.org/).

* Added "PHP strict types" to all files
* `LatLongValue` no longer extends `DataValueObject`
* `GlobeCoordinateValue` no longer extends `DataValueObject`

### 4.0.1 (2018-08-10)

Expand Down
86 changes: 47 additions & 39 deletions src/Values/GlobeCoordinateValue.php
Expand Up @@ -4,28 +4,24 @@

namespace DataValues\Geo\Values;

use DataValues\DataValueObject;
use DataValues\DataValue;
use DataValues\IllegalValueException;
use InvalidArgumentException;

/**
* Class representing a geographical coordinate value.
* Represents a latitude-longitude pair with a certain precision on a certain globe.
*
* @since 0.1
*
* @license GPL-2.0-or-later
* @author Jeroen De Dauw < jeroendedauw@gmail.com >
* @author Thiemo Kreuz
*/
class GlobeCoordinateValue extends DataValueObject {
class GlobeCoordinateValue implements DataValue {

/**
* @var LatLongValue
*/
private $latLong;

/**
* The precision of the coordinate in degrees, e.g. 0.01.
*
* @var float|null
*/
private $precision;
Expand Down Expand Up @@ -83,7 +79,7 @@ public function serialize(): string {
*
* @param string $value
*
* @throws IllegalValueException
* @throws InvalidArgumentException
*/
public function unserialize( $value ) {
list( $latitude, $longitude, $altitude, $precision, $globe ) = json_decode( $value );
Expand All @@ -92,17 +88,13 @@ public function unserialize( $value ) {

/**
* @see DataValue::getType
*
* @return string
*/
public static function getType(): string {
return 'globecoordinate';
}

/**
* @see DataValue::getSortKey
*
* @return float
*/
public function getSortKey(): float {
return $this->getLatitude();
Expand All @@ -118,8 +110,6 @@ public function getLongitude(): float {

/**
* @see DataValue::getValue
*
* @return self
*/
public function getValue(): self {
return $this;
Expand All @@ -131,17 +121,13 @@ public function getLatLong(): LatLongValue {

/**
* Returns the precision of the coordinate in degrees, e.g. 0.01.
*
* @return float|int|null
*/
public function getPrecision(): ?float {
return $this->precision;
}

/**
* Returns the IRI of the globe on which the location resides.
*
* @return string
*/
public function getGlobe(): string {
return $this->globe;
Expand All @@ -151,20 +137,18 @@ public function getGlobe(): string {
* @see Hashable::getHash
*
* @since 2.0
*
* @return string
*/
public function getHash(): string {
return md5( $this->latLong->getLatitude() . '|'
return md5(
$this->latLong->getLatitude() . '|'
. $this->latLong->getLongitude() . '|'
. $this->precision . '|'
. $this->globe );
. $this->globe
);
}

/**
* @see DataValue::getArrayValue
*
* @return array
*/
public function getArrayValue(): array {
return [
Expand All @@ -181,23 +165,47 @@ public function getArrayValue(): array {
}

/**
* Constructs a new instance from the provided data. Required for @see DataValueDeserializer.
* This is expected to round-trip with @see getArrayValue.
*
* @deprecated since 2.0.1. Static DataValue::newFromArray constructors like this are
* underspecified (not in the DataValue interface), and misleadingly named (should be named
* newFromArrayValue). Instead, use DataValue builder callbacks in @see DataValueDeserializer.
*
* @param mixed $data Warning! Even if this is expected to be a value as returned by
* @see getArrayValue, callers of this specific newFromArray implementation can not guarantee
* this. This is not even guaranteed to be an array!
* @see \Comparable::equals
*/
public function equals( $target ): bool {
return $target instanceof self
&& $this->latLong->equals( $target->latLong )
&& $this->precision === $target->precision
&& $this->globe === $target->globe;
}

public function getCopy(): self {
return new self(
$this->latLong,
$this->precision,
$this->globe
);
}

public function toArray(): array {
return [
'value' => $this->getArrayValue(),
'type' => $this->getType(),
];
}

/**
* Constructs a new instance from the provided array. Round-trips with @see getArrayValue.
*
* @throws IllegalValueException if $data is not in the expected format. Subclasses of
* InvalidArgumentException are expected and properly handled by @see DataValueDeserializer.
* @return self
* @throws InvalidArgumentException
*/
public static function newFromArray( $data ): self {
self::requireArrayFields( $data, [ 'latitude', 'longitude' ] );
if ( !is_array( $data ) ) {
throw new IllegalValueException( 'array expected' );
}

if ( !array_key_exists( 'latitude', $data ) ) {
throw new IllegalValueException( 'latitude field required' );
}

if ( !array_key_exists( 'longitude', $data ) ) {
throw new IllegalValueException( 'longitude field required' );
}

return new static(
new LatLongValue(
Expand Down
13 changes: 3 additions & 10 deletions src/Values/LatLongValue.php
Expand Up @@ -9,7 +9,7 @@
use InvalidArgumentException;

/**
* Object representing a geographic point.
* Represents a geographical point.
*
* Latitude is specified in degrees within the range [-360, 360].
* Longitude is specified in degrees within the range [-360, 360].
Expand Down Expand Up @@ -151,18 +151,11 @@ public function getHash(): string {

/**
* @see \Comparable::equals
*
* @param mixed $target
*
* @return bool
*/
public function equals( $target ): bool {
if ( $this === $target ) {
return true;
}

return $target instanceof self
&& serialize( $this ) === serialize( $target );
&& $this->latitude === $target->latitude
&& $this->longitude === $target->longitude;
}

public function getCopy(): self {
Expand Down

0 comments on commit ee492ab

Please sign in to comment.