|
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\Collection; |
|
16
|
|
|
use Geocoder\Exception\CollectionIsEmpty; |
|
17
|
|
|
use Geocoder\Exception\OutOfBounds; |
|
18
|
|
|
use Geocoder\Location; |
|
19
|
|
|
|
|
20
|
|
|
final class AddressCollection implements Collection |
|
21
|
|
|
{ |
|
22
|
|
|
/** |
|
23
|
|
|
* @var Location[] |
|
24
|
|
|
*/ |
|
25
|
|
|
private $locations; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @param Location[] $locations |
|
29
|
|
|
*/ |
|
30
|
5 |
|
public function __construct(array $locations = []) |
|
31
|
|
|
{ |
|
32
|
5 |
|
$this->locations = array_values($locations); |
|
33
|
5 |
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* {@inheritdoc} |
|
37
|
|
|
*/ |
|
38
|
|
|
public function getIterator() |
|
39
|
|
|
{ |
|
40
|
|
|
return new \ArrayIterator($this->all()); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* {@inheritdoc} |
|
45
|
|
|
*/ |
|
46
|
|
|
public function count() |
|
47
|
|
|
{ |
|
48
|
|
|
return count($this->locations); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* {@inheritdoc} |
|
53
|
|
|
*/ |
|
54
|
3 |
|
public function first(): Location |
|
55
|
|
|
{ |
|
56
|
3 |
|
if (empty($this->locations)) { |
|
57
|
1 |
|
throw new CollectionIsEmpty(); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
2 |
|
return reset($this->locations); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* {@inheritdoc} |
|
65
|
|
|
*/ |
|
66
|
|
|
public function isEmpty(): bool |
|
67
|
|
|
{ |
|
68
|
|
|
return empty($this->locations); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* @return Location[] |
|
73
|
|
|
*/ |
|
74
|
|
|
public function slice(int $offset, int $length = null) |
|
75
|
|
|
{ |
|
76
|
|
|
return array_slice($this->locations, $offset, $length); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* @return bool |
|
81
|
|
|
*/ |
|
82
|
|
|
public function has(int $index): bool |
|
83
|
|
|
{ |
|
84
|
|
|
return isset($this->locations[$index]); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* {@inheritdoc} |
|
89
|
|
|
*/ |
|
90
|
|
|
public function get(int $index): Location |
|
91
|
|
|
{ |
|
92
|
|
|
if (!isset($this->locations[$index])) { |
|
93
|
|
|
throw new OutOfBounds(sprintf('The index "%s" does not exist in this collection.', $index)); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
return $this->locations[$index]; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* {@inheritdoc} |
|
101
|
|
|
*/ |
|
102
|
|
|
public function all(): array |
|
103
|
|
|
{ |
|
104
|
|
|
return $this->locations; |
|
105
|
|
|
} |
|
106
|
|
|
} |
|
107
|
|
|
|