-
Type: New Feature
-
Resolution: Duplicate
-
Priority: Unknown
-
None
-
Affects Version/s: None
-
Component/s: Builder
-
None
Feature discussed with a user. The idea is to make it possible to pass to Collection::aggregate an object that implements both Pipeline and Decoder. The decoder is then automatically used to process the pipeline results.
public function aggregate(Pipeline $pipeline, array $options = []) { if (!isset($options['codec']) && $pipeline instanceof \MongoDB\Codec\Decoder) { $options['codec'] = $pipeline; } $this->collection->aggregate($pipeline, $options); }
The use-case is to create classes that describe both how the data is queried and how it is transformed.
class PipelineAndDecode implements Decoder, PipelineInterface { use DecodeIfSupported; public function __construct(string $name) {} public function getPipeline(): Pipeline { return new Pipeline( Stage::match(author: $this->name), Stage::project(_id: 0, title: 1, author: 1, body: 1), Stage::limit(10), ); } public function canDecode($value): bool { return $value instanceof stdClass; } public function decode($value) { return new Article( $value->title, $value->author, $value->body, ); } } class Repository { public function __construct(private Collection $collection) {} public function aggregate(Pipeline $pipeline, array $options = []) { if (!isset($options['codec']) && $pipeline instanceof \MongoDB\Codec\Decoder) { $options['codec'] = $pipeline; } $this->collection->aggregate($pipeline, $options); } }
- duplicates
-
PHPLIB-1419 Integrate pipeline builder with aggregate() and watch() methods
- Closed