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

LatLongPrecisionParser   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 9

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 9
dl 0
loc 45
ccs 0
cts 21
cp 0
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A parse() 0 16 3
A getParsers() 0 7 2
A getNewParsers() 0 6 1
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