Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 1 addition & 4 deletions src/Formatters/GlobeCoordinateFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,7 @@
*/
class GlobeCoordinateFormatter implements ValueFormatter {

/**
* @var LatLongFormatter
*/
private $formatter;
private LatLongFormatter $formatter;

public function __construct( ?FormatterOptions $options = null ) {
$this->formatter = new LatLongFormatter( $options );
Expand Down
17 changes: 6 additions & 11 deletions src/Formatters/LatLongFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ private function getPrecisionFromOptions(): float {
* @since 0.5
*
* @param LatLongValue $value
* @param float|int $precision The desired precision, given as fractional degrees.
* @param ?float $precision The desired precision, given as fractional degrees.
*
* @return string Plain text
* @throws InvalidArgumentException
Expand All @@ -171,15 +171,13 @@ public function formatLatLongValue( LatLongValue $value, ?float $precision ): st
$precision = self::DEFAULT_PRECISION;
}

$formatted = implode(
return implode(
$this->options->getOption( self::OPT_SEPARATOR_SYMBOL ) . $this->getSpacing( self::OPT_SPACE_LATLONG ),
[
$this->formatLatitude( $value->getLatitude(), $precision ),
$this->formatLongitude( $value->getLongitude(), $precision )
]
);

return $formatted;
}

/**
Expand All @@ -188,10 +186,7 @@ public function formatLatLongValue( LatLongValue $value, ?float $precision ): st
* @return string
*/
private function getSpacing( string $spacingLevel ): string {
if ( in_array( $spacingLevel, $this->options->getOption( self::OPT_SPACING_LEVEL ) ) ) {
return ' ';
}
return '';
return in_array( $spacingLevel, $this->options->getOption( self::OPT_SPACING_LEVEL ) ) ? ' ' : '';
}

private function formatLatitude( float $latitude, float $precision ): string {
Expand Down Expand Up @@ -347,9 +342,9 @@ private function getInDecimalMinuteFormat( float $floatDegrees, float $precision
}

/**
* @param float|int $unitsPerDegree The number of target units per degree
* @param float $unitsPerDegree The number of target units per degree
* (60 for minutes, 3600 for seconds)
* @param float|int $degreePrecision
* @param float $degreePrecision
*
* @return int The number of digits to show after the decimal point
* (resp. before, if the result is negative).
Expand All @@ -366,7 +361,7 @@ private function getSignificantDigits( float $unitsPerDegree, float $degreePreci
*/
private function formatNumber( float $number, int $digits = 0 ): string {
// TODO: use NumberLocalizer
return sprintf( '%.' . ( $digits > 0 ? $digits : 0 ) . 'F', $number );
return sprintf( '%.' . ( max( $digits, 0 ) ) . 'F', $number );
}

private function defaultOption( string $option, mixed $default ): void {
Expand Down
12 changes: 4 additions & 8 deletions src/GlobeMath.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,10 @@ public function normalizeGlobeCoordinate( GlobeCoordinateValue $value ): GlobeCo
* @return LatLongValue
*/
public function normalizeGlobeLatLong( LatLongValue $value, ?string $globe = null ): LatLongValue {
switch ( $this->normalizeGlobe( $globe ) ) {
case GlobeCoordinateValue::GLOBE_EARTH:
case self::GLOBE_MOON:
$minimumLongitude = -180;
break;
default:
$minimumLongitude = 0;
}
$minimumLongitude = match ( $this->normalizeGlobe( $globe ) ) {
GlobeCoordinateValue::GLOBE_EARTH, self::GLOBE_MOON => -180,
default => 0,
};

return $this->normalizeLatLong( $value, $minimumLongitude );
}
Expand Down
7 changes: 2 additions & 5 deletions src/PackagePrivate/LatLongPrecisionParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,16 @@
* @api
*/
class LatLongPrecisionParser {

private ?ParserOptions $options;
private ?array $parsers = null;

public function __construct( ?ParserOptions $options = null ) {
$this->options = $options;
public function __construct( private ?ParserOptions $options = null ) {
}

public function parse( string $coordinate ): PreciseLatLong {
foreach ( $this->getParsers() as $parser ) {
try {
$latLongPrecision = $parser->parse( $coordinate );
} catch ( ParseException $parseException ) {
} catch ( ParseException ) {
continue;
}

Expand Down
8 changes: 1 addition & 7 deletions src/PackagePrivate/PreciseLatLong.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,7 @@
* @api
*/
class PreciseLatLong {

private LatLongValue $latLong;
private Precision $precision;

public function __construct( LatLongValue $latLong, Precision $precision ) {
$this->latLong = $latLong;
$this->precision = $precision;
public function __construct( private readonly LatLongValue $latLong, private readonly Precision $precision ) {
}

public function getLatLong(): LatLongValue {
Expand Down
4 changes: 3 additions & 1 deletion src/PackagePrivate/Precision.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

namespace DataValues\Geo\PackagePrivate;

use InvalidArgumentException;

/**
* @api
*/
Expand All @@ -13,7 +15,7 @@ class Precision {

public function __construct( float $precisionInDegrees ) {
if ( $precisionInDegrees < -360 || $precisionInDegrees > 360 ) {
throw new \InvalidArgumentException( '$precision needs to be between -360 and 360' );
throw new InvalidArgumentException( '$precision needs to be between -360 and 360' );
}

$this->precision = $precisionInDegrees;
Expand Down
8 changes: 1 addition & 7 deletions src/PackagePrivate/PrecisionParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,7 @@
* @api
*/
class PrecisionParser {

private ValueParser $latLongParser;
private PrecisionDetector $precisionDetector;

public function __construct( ValueParser $latLongParser, PrecisionDetector $precisionDetector ) {
$this->latLongParser = $latLongParser;
$this->precisionDetector = $precisionDetector;
public function __construct( private readonly ValueParser $latLongParser, private readonly PrecisionDetector $precisionDetector ) {
}

public function parse( string $coordinate ): PreciseLatLong {
Expand Down
28 changes: 10 additions & 18 deletions src/Parsers/DdCoordinateParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,6 @@ public function __construct( ?ParserOptions $options = null ) {

/**
* @see LatLongParserBase::getParsedCoordinate
*
* @param string $coordinateSegment
*
* @return float
*/
protected function getParsedCoordinate( string $coordinateSegment ): float {
$coordinateSegment = $this->resolveDirection( $coordinateSegment );
Expand All @@ -73,14 +69,14 @@ protected function areValidCoordinates( array $normalizedCoordinateSegments ): b
$match = false;

foreach ( $normalizedCoordinateSegments as $i => $segment ) {
$direction = '('
. $this->getOption( self::OPT_NORTH_SYMBOL ) . '|'
. $this->getOption( self::OPT_SOUTH_SYMBOL ) . ')';

if ( $i === 1 ) {
$direction = '('
. $this->getOption( self::OPT_EAST_SYMBOL ) . '|'
. $this->getOption( self::OPT_WEST_SYMBOL ) . ')';
} else {
$direction = '('
. $this->getOption( self::OPT_NORTH_SYMBOL ) . '|'
. $this->getOption( self::OPT_SOUTH_SYMBOL ) . ')';
}

$match = preg_match(
Expand All @@ -90,10 +86,12 @@ protected function areValidCoordinates( array $normalizedCoordinateSegments ): b

if ( $directional ) {
// Directionality is only set after parsing latitude: When the latitude is
// is directional, the longitude needs to be as well. Therefore we break here since
// directional, the longitude needs to be as well. Therefore, we break here since
// checking for directionality is the only check needed for longitude.
break;
} elseif ( $match ) {
}

if ( $match ) {
// Latitude is directional, no need to check for non-directionality.
$directional = true;
continue;
Expand Down Expand Up @@ -139,9 +137,7 @@ protected function getNormalizedNotation( string $coordinates ): string {
$this->getOption( self::OPT_DEGREE_SYMBOL ), $coordinates
);

$coordinates = $this->removeInvalidChars( $coordinates );

return $coordinates;
return $this->removeInvalidChars( $coordinates );
}

/**
Expand All @@ -160,10 +156,6 @@ protected function removeInvalidChars( string $string ): string {

/**
* Converts a coordinate segment to float representation.
*
* @param string $coordinateSegment
*
* @return float
*/
protected function parseCoordinate( string $coordinateSegment ): float {
return (float)str_replace(
Expand All @@ -186,7 +178,7 @@ protected function splitString( string $normalizedCoordinateString ): array {
$normalizedCoordinateSegments = explode( $separator, $normalizedCoordinateString );

if ( count( $normalizedCoordinateSegments ) !== 2 ) {
// Separator not present within the string, trying to figure out the segments by
// Separator is not present within the string, trying to figure out the segments by
// splitting after the first direction character or degree symbol:
$delimiters = $this->defaultDelimiters;

Expand Down
16 changes: 4 additions & 12 deletions src/Parsers/DmCoordinateParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,21 +109,13 @@ protected function areValidCoordinates( array $normalizedCoordinateSegments ): b

/**
* @see DdCoordinateParser::getNormalizedNotation
*
* @param string $coordinates
*
* @return string
*/
protected function getNormalizedNotation( string $coordinates ): string {
$minute = $this->getOption( self::OPT_MINUTE_SYMBOL );

$coordinates = str_replace( [ '&#8242;', '&prime;', '´', '′' ], $minute, $coordinates );
$coordinates = str_replace( [ '&#8242;', '&prime;', '´', '′' ], $this->getOption( self::OPT_MINUTE_SYMBOL ), $coordinates );

$coordinates = parent::getNormalizedNotation( $coordinates );

$coordinates = $this->removeInvalidChars( $coordinates );

return $coordinates;
return $this->removeInvalidChars(
parent::getNormalizedNotation( $coordinates )
);
}

/**
Expand Down
14 changes: 5 additions & 9 deletions src/Parsers/DmsCoordinateParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,14 +68,14 @@ protected function areValidCoordinates( array $normalizedCoordinateSegments ): b
$directional = false;

foreach ( $normalizedCoordinateSegments as $i => $segment ) {
$direction = '('
. $this->getOption( self::OPT_NORTH_SYMBOL ) . '|'
. $this->getOption( self::OPT_SOUTH_SYMBOL ) . ')';

if ( $i === 1 ) {
$direction = '('
. $this->getOption( self::OPT_EAST_SYMBOL ) . '|'
. $this->getOption( self::OPT_WEST_SYMBOL ) . ')';
} else {
$direction = '('
. $this->getOption( self::OPT_NORTH_SYMBOL ) . '|'
. $this->getOption( self::OPT_SOUTH_SYMBOL ) . ')';
}

$match = preg_match(
Expand Down Expand Up @@ -130,11 +130,7 @@ protected function getNormalizedNotation( string $coordinates ): string {
);
$coordinates = str_replace( [ '&acute;', '&#180;' ], $second, $coordinates );

$coordinates = parent::getNormalizedNotation( $coordinates );

$coordinates = $this->removeInvalidChars( $coordinates );

return $coordinates;
return $this->removeInvalidChars( parent::getNormalizedNotation( $coordinates ) );
}

/**
Expand Down
12 changes: 6 additions & 6 deletions src/Parsers/FloatCoordinateParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,14 @@ protected function areValidCoordinates( array $normalizedCoordinateSegments ): b
foreach ( $normalizedCoordinateSegments as $i => $segment ) {
$segment = str_replace( ' ', '', $segment );

$direction = '('
. $this->getOption( self::OPT_NORTH_SYMBOL ) . '|'
. $this->getOption( self::OPT_SOUTH_SYMBOL ) . ')';

if ( $i === 1 ) {
$direction = '('
. $this->getOption( self::OPT_EAST_SYMBOL ) . '|'
. $this->getOption( self::OPT_WEST_SYMBOL ) . ')';
} else {
$direction = '('
. $this->getOption( self::OPT_NORTH_SYMBOL ) . '|'
. $this->getOption( self::OPT_SOUTH_SYMBOL ) . ')';
}

$match = preg_match(
Expand Down Expand Up @@ -89,8 +89,8 @@ protected function splitString( string $normalizedCoordinateString ): array {
$normalizedCoordinateSegments = explode( $separator, $normalizedCoordinateString );

if ( count( $normalizedCoordinateSegments ) !== 2 ) {
// Separator not present within the string, trying to figure out the segments by
// splitting at the the first SPACE after the first direction character or digit:
// Separator is not present within the string, trying to figure out the segments by
// splitting at the first SPACE after the first direction character or digit:
$numberRegEx = '-?\d{1,3}(\.\d{1,20})?';

$ns = '('
Expand Down
3 changes: 2 additions & 1 deletion src/Parsers/GlobeCoordinateParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use DataValues\Geo\PackagePrivate\LatLongPrecisionParser;
use DataValues\Geo\PackagePrivate\Precision;
use DataValues\Geo\Values\GlobeCoordinateValue;
use Exception;
use ValueParsers\ParseException;
use ValueParsers\ParserOptions;
use ValueParsers\ValueParser;
Expand Down Expand Up @@ -56,7 +57,7 @@ public function parse( $value ): GlobeCoordinateValue {

try {
$latLongPrecision = $parser->parse( $value );
} catch ( \Exception $ex ) {
} catch ( Exception ) {
throw new ParseException(
'The format of the coordinate could not be determined.',
$value,
Expand Down
9 changes: 3 additions & 6 deletions src/Parsers/LatLongParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,11 @@ class LatLongParser implements ValueParser {
public const OPT_SECOND_SYMBOL = 'second';

/**
* The symbol to use as separator between latitude and longitude.
* The symbol to use as a separator between latitude and longitude.
*/
public const OPT_SEPARATOR_SYMBOL = 'separator';

/**
* @var ParserOptions
*/
private $options;
private ParserOptions $options;

public function __construct( ?ParserOptions $options = null ) {
$this->options = ( $options ?: new ParserOptions() )->withDefaultOption( ValueParser::OPT_LANG, 'en' );
Expand All @@ -83,7 +80,7 @@ public function parse( $value ): LatLongValue {
foreach ( $this->getParsers() as $parser ) {
try {
return $parser->parse( $value );
} catch ( ParseException $ex ) {
} catch ( ParseException ) {
continue;
}
}
Expand Down
Loading