|
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
|
|
|
public function contains(CommunibaseId $needleId): bool |
|
42
|
|
|
{ |
|
43
|
|
|
foreach ($this->ids as $id) { |
|
44
|
|
|
if ($id->equals($needleId)) { |
|
45
|
|
|
return true; |
|
46
|
|
|
} |
|
47
|
|
|
} |
|
48
|
|
|
return false; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
public function count(): int |
|
52
|
|
|
{ |
|
53
|
|
|
return count($this->ids); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
public function isEmpty(): bool |
|
57
|
|
|
{ |
|
58
|
|
|
return empty($this->ids); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* @return \ArrayIterator|\Traversable|CommunibaseId[] |
|
63
|
|
|
*/ |
|
64
|
|
|
public function getIterator() |
|
65
|
|
|
{ |
|
66
|
|
|
return new \ArrayIterator($this->ids); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* @return array|string[] |
|
71
|
|
|
*/ |
|
72
|
|
|
public function toStrings(): array |
|
73
|
|
|
{ |
|
74
|
|
|
return \array_map('strval', $this->ids); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
public function toObjectQueryArray(): array |
|
78
|
|
|
{ |
|
79
|
|
|
return array_reduce( |
|
80
|
|
|
$this->ids, |
|
81
|
|
|
static function (array $carry, CommunibaseId $id) { |
|
82
|
|
|
$carry[] = ['$ObjectId' => $id->toString()]; |
|
83
|
|
|
return $carry; |
|
84
|
|
|
}, |
|
85
|
|
|
[] |
|
86
|
|
|
); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
public function jsonSerialize() |
|
90
|
|
|
{ |
|
91
|
|
|
return $this->toStrings(); |
|
92
|
|
|
} |
|
93
|
|
|
} |
|
94
|
|
|
|