1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Communibase; |
6
|
|
|
|
7
|
|
|
use Communibase\Exception\InvalidIdException; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* @author Kingsquare ([email protected]) |
11
|
|
|
* @copyright Copyright (c) Kingsquare BV (http://www.kingsquare.nl) |
12
|
|
|
*/ |
13
|
|
|
class CommunibaseIdCollection implements \Countable, \IteratorAggregate, \JsonSerializable |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @var CommunibaseId[] |
17
|
|
|
*/ |
18
|
|
|
private $ids; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @throws InvalidIdException |
22
|
|
|
*/ |
23
|
|
|
private function __construct(array $strings) |
24
|
|
|
{ |
25
|
|
|
$this->ids = \array_map( |
26
|
|
|
static function (string $string) { |
27
|
|
|
return CommunibaseId::fromString($string); |
28
|
|
|
}, |
29
|
|
|
\array_filter(\array_unique($strings)) |
30
|
|
|
); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @throws InvalidIdException |
35
|
|
|
*/ |
36
|
|
|
public static function fromStrings(array $strings): CommunibaseIdCollection |
37
|
|
|
{ |
38
|
|
|
return new self($strings); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Filter out all invalid strings |
43
|
|
|
*/ |
44
|
|
|
public static function fromValidStrings(array $strings): CommunibaseIdCollection |
45
|
|
|
{ |
46
|
|
|
$collection = new self([]); |
47
|
|
|
$collection->ids = \array_reduce( |
48
|
|
|
$strings, |
49
|
|
|
static function (array $communibaseIds, $string) { |
50
|
|
|
try { |
51
|
|
|
$communibaseIds[] = CommunibaseId::fromString((string)$string); |
52
|
|
|
} catch (InvalidIdException $e) { |
53
|
|
|
// ignore invalid strings |
54
|
|
|
} |
55
|
|
|
return $communibaseIds; |
56
|
|
|
}, |
57
|
|
|
[] |
58
|
|
|
); |
59
|
|
|
return $collection; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public function contains(CommunibaseId $needleId): bool |
63
|
|
|
{ |
64
|
|
|
foreach ($this->ids as $id) { |
65
|
|
|
if ($id->equals($needleId)) { |
66
|
|
|
return true; |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
return false; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
public function count(): int |
73
|
|
|
{ |
74
|
|
|
return count($this->ids); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
public function isEmpty(): bool |
78
|
|
|
{ |
79
|
|
|
return empty($this->ids); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @return \ArrayIterator|\Traversable|CommunibaseId[] |
84
|
|
|
*/ |
85
|
|
|
public function getIterator() |
86
|
|
|
{ |
87
|
|
|
return new \ArrayIterator($this->ids); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @return array|string[] |
92
|
|
|
*/ |
93
|
|
|
public function toStrings(): array |
94
|
|
|
{ |
95
|
|
|
return \array_map('strval', $this->ids); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
public function toObjectQueryArray(): array |
99
|
|
|
{ |
100
|
|
|
return array_reduce( |
101
|
|
|
$this->ids, |
102
|
|
|
static function (array $carry, CommunibaseId $id) { |
103
|
|
|
$carry[] = ['$ObjectId' => $id->toString()]; |
104
|
|
|
return $carry; |
105
|
|
|
}, |
106
|
|
|
[] |
107
|
|
|
); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
public function jsonSerialize() |
111
|
|
|
{ |
112
|
|
|
return $this->toStrings(); |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|