Issues (4)

src/CommunibaseIdCollection.php (1 issue)

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
 * @implements \IteratorAggregate<CommunibaseId>
14
 */
15
final class CommunibaseIdCollection implements \Countable, \IteratorAggregate, \JsonSerializable
16
{
17
    /**
18
     * @var CommunibaseId[]
19
     */
20
    private $ids;
21
22
    /**
23
     * @param string[] $strings
24
     * @throws InvalidIdException
25
     */
26
    private function __construct(array $strings)
27
    {
28
        $this->ids = array_map(
29
            static function (string $string) {
30
                return CommunibaseId::fromString($string);
31
            },
32
            array_unique(array_filter($strings))
33
        );
34
    }
35
36
    /**
37
     * @param string[] $strings
38
     * @throws InvalidIdException
39
     */
40
    public static function fromStrings(array $strings): CommunibaseIdCollection
41
    {
42
        return new self($strings);
43
    }
44
45
    /**
46
     * Filter out all invalid strings
47
     * @param string[] $strings
48
     */
49
    public static function fromValidStrings(array $strings): CommunibaseIdCollection
50
    {
51
        /** @noinspection PhpUnhandledExceptionInspection */
52
        $collection = new self([]);
53
        $collection->ids = array_reduce(
54
            $strings,
55
            static function (array $communibaseIds, $string) {
56
                try {
57
                    $communibaseIds[] = CommunibaseId::fromString((string)$string);
58
                } catch (InvalidIdException $e) {
59
                    // ignore invalid strings
60
                }
61
                return $communibaseIds;
62
            },
63
            []
64
        );
65
        return $collection;
66
    }
67
68
    public function contains(CommunibaseId $needleId): bool
69
    {
70
        foreach ($this->ids as $id) {
71
            if ($id->equals($needleId)) {
72
                return true;
73
            }
74
        }
75
        return false;
76
    }
77
78
    public function count(): int
79
    {
80
        return \count($this->ids);
81
    }
82
83
    public function isEmpty(): bool
84
    {
85
        return empty($this->ids);
86
    }
87
88
    /**
89
     * @return \ArrayIterator<int, CommunibaseId>
90
     */
91
    public function getIterator(): \ArrayIterator
92
    {
93
        return new \ArrayIterator($this->ids);
94
    }
95
96
    /**
97
     * @return string[]
98
     */
99
    public function toStrings(): array
100
    {
101
        return array_map('\strval', $this->ids);
102
    }
103
104
    /**
105
     * @return array{array{"$ObjectId":string}}
0 ignored issues
show
Documentation Bug introduced by
The doc comment array{array{"$ObjectId":string}} at position 2 could not be parsed: Expected ':' at position 2, but found 'array'.
Loading history...
106
     */
107
    public function toObjectQueryArray(): array
108
    {
109
        return array_reduce(
110
            $this->ids,
111
            static function (array $carry, CommunibaseId $id) {
112
                $carry[] = ['$ObjectId' => $id->toString()];
113
                return $carry;
114
            },
115
            []
116
        );
117
    }
118
119
    public function jsonSerialize()
120
    {
121
        return $this->toStrings();
122
    }
123
}
124