Completed
Push — master ( 0885cd...ef8e35 )
by Antoine
02:22
created

CoordinateCollection::__construct()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 12
ccs 9
cts 9
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 7
nc 3
nop 2
crap 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 13
        }
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 38
        }
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 18
        }
96
97 31
        if ($coordinate->getEllipsoid() != $this->ellipsoid) {
98
            throw new InvalidArgumentException("Ellipsoids don't match");
99
        }
100 31
    }
101
}
102