Completed
Push — master ( d2848a...af8d92 )
by Tobias
02:58
created

AdminLevelCollection::checkLevel()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 3.1406

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 3
cts 4
cp 0.75
rs 10
c 0
b 0
f 0
cc 3
nc 2
nop 1
crap 3.1406
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Geocoder package.
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 *
10
 * @license    MIT License
11
 */
12
13
namespace Geocoder\Model;
14
15
use Geocoder\Exception\CollectionIsEmpty;
16
use Geocoder\Exception\InvalidArgument;
17
use Geocoder\Exception\OutOfBounds;
18
19
/**
20
 * @author Giorgio Premi <[email protected]>
21
 */
22
final class AdminLevelCollection implements \IteratorAggregate, \Countable
23
{
24
    const MAX_LEVEL_DEPTH = 5;
25
26
    /**
27
     * @var AdminLevel[]
28
     */
29
    private $adminLevels;
30
31
    /**
32
     * @param AdminLevel[] $adminLevels
33
     */
34 37
    public function __construct(array $adminLevels = [])
35
    {
36 37
        $this->adminLevels = [];
37
38 37
        foreach ($adminLevels as $adminLevel) {
39 6
            $level = $adminLevel->getLevel();
40
41 6
            $this->checkLevel($level);
42
43 6
            if ($this->has($level)) {
44
                throw new InvalidArgument(sprintf('Administrative level %d is defined twice', $level));
45
            }
46
47 6
            $this->adminLevels[$level] = $adminLevel;
48
        }
49
50 37
        ksort($this->adminLevels, SORT_NUMERIC);
51 37
    }
52
53
    /**
54
     * {@inheritdoc}
55
     */
56 31
    public function getIterator()
57
    {
58 31
        return new \ArrayIterator($this->all());
59
    }
60
61
    /**
62
     * {@inheritdoc}
63
     */
64
    public function count()
65
    {
66
        return count($this->adminLevels);
67
    }
68
69
    /**
70
     * @return AdminLevel
71
     *
72
     * @throws CollectionIsEmpty
73
     */
74
    public function first(): AdminLevel
75
    {
76
        if (empty($this->adminLevels)) {
77
            throw new CollectionIsEmpty();
78
        }
79
80
        return reset($this->adminLevels);
81
    }
82
83
    /**
84
     * @param int      $offset
85
     * @param int|null $length
86
     *
87
     * @return AdminLevel[]
88
     */
89
    public function slice(int $offset, int $length = null): array
90
    {
91
        return array_slice($this->adminLevels, $offset, $length, true);
92
    }
93
94
    /**
95
     * @return bool
96
     */
97 6
    public function has(int $level): bool
98
    {
99 6
        return isset($this->adminLevels[$level]);
100
    }
101
102
    /**
103
     * @return AdminLevel
104
     *
105
     * @throws \OutOfBoundsException
106
     * @throws InvalidArgument
107
     */
108
    public function get(int $level): AdminLevel
109
    {
110
        $this->checkLevel($level);
111
112
        if (!isset($this->adminLevels[$level])) {
113
            throw new InvalidArgument(sprintf('Administrative level %d is not set for this address', $level));
114
        }
115
116
        return  $this->adminLevels[$level];
117
    }
118
119
    /**
120
     * @return AdminLevel[]
121
     */
122 31
    public function all(): array
123
    {
124 31
        return $this->adminLevels;
125
    }
126
127
    /**
128
     * @param int $level
129
     *
130
     * @throws \OutOfBoundsException
131
     */
132 6
    private function checkLevel(int $level)
133
    {
134 6
        if ($level <= 0 || $level > self::MAX_LEVEL_DEPTH) {
135
            throw new OutOfBounds(sprintf('Administrative level should be an integer in [1,%d], %d given', self::MAX_LEVEL_DEPTH, $level));
136
        }
137 6
    }
138
}
139