forked from geocoder-php/php-common
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathAddressCollection.php
More file actions
86 lines (73 loc) · 1.6 KB
/
Copy pathAddressCollection.php
File metadata and controls
86 lines (73 loc) · 1.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
<?php
namespace Geocoder\Model;
use Geocoder\Exception\CollectionIsEmpty;
final class AddressCollection implements \IteratorAggregate, \Countable
{
/**
* @var Address[]
*/
private $addresses;
/**
* @param Address[] $addresses
*/
public function __construct(array $addresses = [])
{
$this->addresses = array_values($addresses);
}
/**
* {@inheritDoc}
*/
public function getIterator()
{
return new \ArrayIterator($this->all());
}
/**
* {@inheritDoc}
*/
public function count()
{
return count($this->addresses);
}
/**
* @return Address
*/
public function first()
{
if (empty($this->addresses)) {
throw new CollectionIsEmpty('The AddressCollection instance is empty.');
}
return reset($this->addresses);
}
/**
* @return Address[]
*/
public function slice($offset, $length = null)
{
return array_slice($this->addresses, $offset, $length);
}
/**
* @return bool
*/
public function has($index)
{
return isset($this->addresses[$index]);
}
/**
* @return Address
* @throws \OutOfBoundsException
*/
public function get($index)
{
if (!isset($this->addresses[$index])) {
throw new \OutOfBoundsException(sprintf('The index "%s" does not exist in this collection.', $index));
}
return $this->addresses[$index];
}
/**
* @return Address[]
*/
public function all()
{
return $this->addresses;
}
}