Completed
Push — master ( ef8e35...28395c )
by Antoine
05:19
created

CoordinateCollection   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 82
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 3
dl 0
loc 82
ccs 27
cts 27
cp 1
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A set() 0 5 1
A add() 0 7 1
A getEllipsoid() 0 4 1
A __construct() 0 12 3
A checkCoordinatesArray() 0 6 2
A checkEllipsoid() 0 10 3
1
<?php
2
3
/*
4
 * This file is part of the Geotools library.
5
 *
6
 * (c) Antoine Corcy <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace League\Geotools\Coordinate;
13
14
use League\Geotools\ArrayCollection;
15
use League\Geotools\Exception\InvalidArgumentException;
16
17
/**
18
 * @author Gabriel Bull <[email protected]>
19
 */
20
class CoordinateCollection extends ArrayCollection
21
{
22
    /**
23
     * @var Ellipsoid
24
     */
25
    private $ellipsoid;
26
27
    /**
28
     * CoordinateCollection constructor.
29
     *
30
     * @param CoordinateInterface[] $coordinates
31
     * @param Ellipsoid             $ellipsoid
32
     */
33 38
    public function __construct(array $coordinates = array(), Ellipsoid $ellipsoid = null)
34
    {
35 38
        if ($ellipsoid) {
36 4
            $this->ellipsoid = $ellipsoid;
37 38
        } elseif (count($coordinates) > 0) {
38 13
            $this->ellipsoid = reset($coordinates)->getEllipsoid();
39
        }
40
41 38
        $this->checkCoordinatesArray($coordinates);
42
43 38
        parent::__construct($coordinates);
44 38
    }
45
46
    /**
47
     * @param string     $key
48
     * @param CoordinateInterface $value
49
     */
50 17
    public function set($key, $value)
51
    {
52 17
        $this->checkEllipsoid($value);
53 17
        $this->elements[$key] = $value;
54 17
    }
55
56
    /**
57
     * @param  CoordinateInterface $value
58
     * @return bool
59
     */
60 2
    public function add($value)
61
    {
62 2
        $this->checkEllipsoid($value);
63 2
        $this->elements[] = $value;
64
65 2
        return true;
66
    }
67
68
    /**
69
     * @return Ellipsoid
70
     */
71 17
    public function getEllipsoid()
72
    {
73 17
        return $this->ellipsoid;
74
    }
75
76
    /**
77
     * @param array CoordinateInterface[] $coordinates
78
     */
79 38
    private function checkCoordinatesArray(array $coordinates)
80
    {
81 38
        foreach ($coordinates as $coordinate) {
82 15
            $this->checkEllipsoid($coordinate);
83
        }
84 38
    }
85
86
    /**
87
     * @param CoordinateInterface $coordinate
88
     *
89
     * @throws InvalidArgumentException
90
     */
91 31
    private function checkEllipsoid(CoordinateInterface $coordinate)
92
    {
93 31
        if ($this->ellipsoid === null) {
94 18
            $this->ellipsoid = $coordinate->getEllipsoid();
95
        }
96
97 31
        if ($coordinate->getEllipsoid() != $this->ellipsoid) {
98
            throw new InvalidArgumentException("Ellipsoids don't match");
99
        }
100 31
    }
101
}
102