-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNodeList.php
More file actions
45 lines (37 loc) · 935 Bytes
/
NodeList.php
File metadata and controls
45 lines (37 loc) · 935 Bytes
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
<?php
namespace Megawilddaddy\Linode;
class NodeList implements \ArrayAccess, \IteratorAggregate
{
/**
* @var Node[]
*/
private $nodes = [];
public function offsetExists($offset)
{
return array_key_exists($offset, $this->nodes);
}
public function offsetGet($offset)
{
return $this->nodes[$offset];
}
public function offsetSet($offset, $value)
{
$offset = $offset === null ? count($this->nodes) : $offset;
$this->nodes[$offset] = $value;
}
public function offsetUnset($offset)
{
unset($this->nodes[$offset]);
}
public function getIterator()
{
return $this->nodes;
}
public function getNodeByIp($ip): ?Node
{
$nodes = array_filter($this->nodes, function (Node $node) use ($ip) {
return in_array($ip, $node->getIps());
});
return reset($nodes) ?? null;
}
}