-
Notifications
You must be signed in to change notification settings - Fork 265
/
Copy pathPipeline.php
60 lines (50 loc) · 1.61 KB
/
Pipeline.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
57
58
59
60
<?php
declare(strict_types=1);
namespace MongoDB\Builder;
use ArrayIterator;
use IteratorAggregate;
use MongoDB\Builder\Type\StageInterface;
use MongoDB\Exception\InvalidArgumentException;
use stdClass;
use function array_is_list;
use function array_merge;
use function is_array;
/**
* An aggregation pipeline consists of one or more stages that process documents.
*
* @see https://www.mongodb.com/docs/manual/core/aggregation-pipeline/
*
* @psalm-immutable
* @psalm-type stage = StageInterface|array<string,mixed>|stdClass
* @implements IteratorAggregate<stage>
*/
final class Pipeline implements IteratorAggregate
{
private readonly array $stages;
/**
* @psalm-param stage|list<stage> ...$stagesOrPipelines
*
* @no-named-arguments
*/
public function __construct(StageInterface|Pipeline|array|stdClass ...$stagesOrPipelines)
{
if (! array_is_list($stagesOrPipelines)) {
throw new InvalidArgumentException('Named arguments are not supported for pipelines');
}
$stages = [];
foreach ($stagesOrPipelines as $stageOrPipeline) {
if (is_array($stageOrPipeline) && array_is_list($stageOrPipeline)) {
$stages = array_merge($stages, $stageOrPipeline);
} elseif ($stageOrPipeline instanceof Pipeline) {
$stages = array_merge($stages, $stageOrPipeline->stages);
} else {
$stages[] = $stageOrPipeline;
}
}
$this->stages = $stages;
}
public function getIterator(): ArrayIterator
{
return new ArrayIterator($this->stages);
}
}