|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace MongoDB\Laravel\Query; |
| 6 | + |
| 7 | +use Illuminate\Support\Collection as LaravelCollection; |
| 8 | +use Illuminate\Support\LazyCollection; |
| 9 | +use InvalidArgumentException; |
| 10 | +use Iterator; |
| 11 | +use MongoDB\Builder\BuilderEncoder; |
| 12 | +use MongoDB\Builder\Stage\FluentFactoryTrait; |
| 13 | +use MongoDB\Collection as MongoDBCollection; |
| 14 | +use MongoDB\Driver\CursorInterface; |
| 15 | +use MongoDB\Laravel\Collection as LaravelMongoDBCollection; |
| 16 | + |
| 17 | +use function array_replace; |
| 18 | +use function collect; |
| 19 | +use function sprintf; |
| 20 | +use function str_starts_with; |
| 21 | + |
| 22 | +class AggregationBuilder |
| 23 | +{ |
| 24 | + use FluentFactoryTrait; |
| 25 | + |
| 26 | + public function __construct( |
| 27 | + private MongoDBCollection|LaravelMongoDBCollection $collection, |
| 28 | + private readonly array $options = [], |
| 29 | + ) { |
| 30 | + } |
| 31 | + |
| 32 | + /** |
| 33 | + * Add a stage without using the builder. Necessary if the stage is built |
| 34 | + * outside the builder, or it is not yet supported by the library. |
| 35 | + */ |
| 36 | + public function addRawStage(string $operator, mixed $value): static |
| 37 | + { |
| 38 | + if (! str_starts_with($operator, '$')) { |
| 39 | + throw new InvalidArgumentException(sprintf('The stage name "%s" is invalid. It must start with a "$" sign.', $operator)); |
| 40 | + } |
| 41 | + |
| 42 | + $this->pipeline[] = [$operator => $value]; |
| 43 | + |
| 44 | + return $this; |
| 45 | + } |
| 46 | + |
| 47 | + /** |
| 48 | + * Execute the aggregation pipeline and return the results. |
| 49 | + */ |
| 50 | + public function get(array $options = []): LaravelCollection|LazyCollection |
| 51 | + { |
| 52 | + $cursor = $this->execute($options); |
| 53 | + |
| 54 | + return collect($cursor->toArray()); |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * Execute the aggregation pipeline and return the results in a lazy collection. |
| 59 | + */ |
| 60 | + public function cursor($options = []): LazyCollection |
| 61 | + { |
| 62 | + $cursor = $this->execute($options); |
| 63 | + |
| 64 | + return LazyCollection::make(function () use ($cursor) { |
| 65 | + foreach ($cursor as $item) { |
| 66 | + yield $item; |
| 67 | + } |
| 68 | + }); |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * Execute the aggregation pipeline and return the first result. |
| 73 | + */ |
| 74 | + public function first(array $options = []): mixed |
| 75 | + { |
| 76 | + return (clone $this) |
| 77 | + ->limit(1) |
| 78 | + ->get($options) |
| 79 | + ->first(); |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * Execute the aggregation pipeline and return MongoDB cursor. |
| 84 | + */ |
| 85 | + private function execute(array $options): CursorInterface&Iterator |
| 86 | + { |
| 87 | + $encoder = new BuilderEncoder(); |
| 88 | + $pipeline = $encoder->encode($this->getPipeline()); |
| 89 | + |
| 90 | + $options = array_replace( |
| 91 | + ['typeMap' => ['root' => 'array', 'document' => 'array']], |
| 92 | + $this->options, |
| 93 | + $options, |
| 94 | + ); |
| 95 | + |
| 96 | + return $this->collection->aggregate($pipeline, $options); |
| 97 | + } |
| 98 | +} |
0 commit comments