Passed
Push — GlobeCoordinateParser ( 7d8c3a )
by Jeroen De
02:26
created

LatLongPrecisionParser::getParsers()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

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