-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathIpPool.php
56 lines (48 loc) · 958 Bytes
/
IpPool.php
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
<?php
declare(strict_types=1);
namespace Flow;
/**
* @template T
*/
class IpPool
{
/**
* @var array<Ip<T>>
*/
private array $ips = [];
/**
* @param Ip<T> $ip
*
* @return callable A function that removes the added IP from the pool when called
*/
public function addIp(Ip $ip): callable
{
$this->ips[] = $ip;
return function () use ($ip) {
$this->ips = array_filter($this->ips, static function ($iteratorIp) use ($ip) {
return $iteratorIp !== $ip;
});
};
}
/**
* @return array<Ip<T>>
*/
public function getIps(): array
{
return $this->ips;
}
/**
* @return null|Ip<T>
*/
public function shiftIp(): ?Ip
{
return array_shift($this->ips);
}
/**
* @return null|Ip<T>
*/
public function popIp(): ?Ip
{
return array_pop($this->ips);
}
}