Passed
Push — GlobeCoordinateParser ( aed06a...a4aa24 )
by Jeroen De
02:14
created

LatLongPrecisionParser::parse()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 16
ccs 0
cts 8
cp 0
rs 9.7333
c 0
b 0
f 0
cc 3
nc 3
nop 1
crap 12
1
<?php
2
3
namespace DataValues\Geo\PackagePrivate;
4
5
use DataValues\Geo\Parsers\DdCoordinateParser;
6
use DataValues\Geo\Parsers\DmCoordinateParser;
7
use DataValues\Geo\Parsers\DmsCoordinateParser;
8
use DataValues\Geo\Parsers\FloatCoordinateParser;
9
use ValueParsers\ParseException;
10
use ValueParsers\ParserOptions;
11
12
class LatLongPrecisionParser {
13
14
	private $options;
15
	private $parsers;
16
17
	public function __construct( ParserOptions $options = null ) {
18
		$this->options = $options;
19
	}
20
21
	public function parse( string $coordinate ): LatLongPrecision {
22
		foreach ( $this->getParsers() as $parser ) {
23
			try {
24
				$latLongPrecision = $parser->parse( $coordinate );
25
			} catch ( ParseException $parseException ) {
26
				continue;
27
			}
28
29
			return $latLongPrecision;
30
		}
31
32
		throw new ParseException(
33
			'The format of the coordinate could not be determined.',
34
			$coordinate
35
		);
36
	}
37
38
	/**
39
	 * @return PrecisionParser[]
40
	 */
41
	private function getParsers(): iterable {
42
		if ( $this->parsers === null ) {
43
			$this->parsers = new \CachingIterator( $this->getNewParsers(), \CachingIterator::FULL_CACHE );
44
		}
45
46
		return $this->parsers;
47
	}
48
49
	private function getNewParsers(): \Generator {
50
		yield new PrecisionParser( new FloatCoordinateParser( $this->options ), new FloatPrecisionDetector() );
51
		yield new PrecisionParser( new DmsCoordinateParser( $this->options ), new DmsPrecisionDetector() );
52
		yield new PrecisionParser( new DmCoordinateParser( $this->options ), new DmPrecisionDetector() );
53
		yield new PrecisionParser( new DdCoordinateParser( $this->options ), new FloatPrecisionDetector() );
54
	}
55
56
}
57