vendor/doctrine/mongodb-odm/lib/Doctrine/ODM/MongoDB/Aggregation/Stage.php line 41

  1. <?php
  2. declare(strict_types=1);
  3. namespace Doctrine\ODM\MongoDB\Aggregation;
  4. use Doctrine\ODM\MongoDB\Iterator\Iterator;
  5. use GeoJson\Geometry\Point;
  6. use function trigger_deprecation;
  7. /**
  8.  * Fluent interface for building aggregation pipelines.
  9.  *
  10.  * @internal
  11.  */
  12. abstract class Stage
  13. {
  14.     /** @var Builder */
  15.     protected $builder;
  16.     public function __construct(Builder $builder)
  17.     {
  18.         $this->builder $builder;
  19.     }
  20.     /**
  21.      * Assembles the aggregation stage
  22.      *
  23.      * @return array<string, mixed>
  24.      */
  25.     abstract public function getExpression(): array;
  26.     /**
  27.      * Executes the aggregation pipeline
  28.      *
  29.      * @deprecated This method was deprecated in doctrine/mongodb-odm 2.2. Please use getAggregation() instead.
  30.      *
  31.      * @param array<string, mixed> $options
  32.      */
  33.     public function execute(array $options = []): Iterator
  34.     {
  35.         trigger_deprecation(
  36.             'doctrine/mongodb-odm',
  37.             '2.2',
  38.             'Using "%s" is deprecated, use "%s::getAggregation()" instead.',
  39.             __METHOD__,
  40.             self::class,
  41.         );
  42.         return $this->builder->execute($options);
  43.     }
  44.     /**
  45.      * Returns an aggregation object for the current pipeline
  46.      *
  47.      * @param array<string, mixed> $options
  48.      */
  49.     public function getAggregation(array $options = []): Aggregation
  50.     {
  51.         return $this->builder->getAggregation($options);
  52.     }
  53.     /**
  54.      * Adds new fields to documents. $addFields outputs documents that contain
  55.      * all existing fields from the input documents and newly added fields.
  56.      *
  57.      * The $addFields stage is equivalent to a $project stage that explicitly
  58.      * specifies all existing fields in the input documents and adds the new
  59.      * fields.
  60.      *
  61.      * @see https://docs.mongodb.com/manual/reference/operator/aggregation/addFields/
  62.      */
  63.     public function addFields(): Stage\AddFields
  64.     {
  65.         return $this->builder->addFields();
  66.     }
  67.     /**
  68.      * Categorizes incoming documents into groups, called buckets, based on a
  69.      * specified expression and bucket boundaries.
  70.      *
  71.      * Each bucket is represented as a document in the output. The document for
  72.      * each bucket contains an _id field, whose value specifies the inclusive
  73.      * lower bound of the bucket and a count field that contains the number of
  74.      * documents in the bucket. The count field is included by default when the
  75.      * output is not specified.
  76.      *
  77.      * @see https://docs.mongodb.com/manual/reference/operator/aggregation/bucket/
  78.      */
  79.     public function bucket(): Stage\Bucket
  80.     {
  81.         return $this->builder->bucket();
  82.     }
  83.     /**
  84.      * Categorizes incoming documents into a specific number of groups, called
  85.      * buckets, based on a specified expression.
  86.      *
  87.      * Bucket boundaries are automatically determined in an attempt to evenly
  88.      * distribute the documents into the specified number of buckets. Each
  89.      * bucket is represented as a document in the output. The document for each
  90.      * bucket contains an _id field, whose value specifies the inclusive lower
  91.      * bound and the exclusive upper bound for the bucket, and a count field
  92.      * that contains the number of documents in the bucket. The count field is
  93.      * included by default when the output is not specified.
  94.      *
  95.      * @see https://docs.mongodb.com/manual/reference/operator/aggregation/bucketAuto/
  96.      */
  97.     public function bucketAuto(): Stage\BucketAuto
  98.     {
  99.         return $this->builder->bucketAuto();
  100.     }
  101.     /**
  102.      * Returns statistics regarding a collection or view.
  103.      *
  104.      * $collStats must be the first stage in an aggregation pipeline, or else
  105.      * the pipeline returns an error.
  106.      *
  107.      * @see https://docs.mongodb.com/manual/reference/operator/aggregation/geoNear/
  108.      */
  109.     public function collStats(): Stage\CollStats
  110.     {
  111.         return $this->builder->collStats();
  112.     }
  113.     /**
  114.      * Returns a document that contains a count of the number of documents input
  115.      * to the stage.
  116.      *
  117.      * @see https://docs.mongodb.com/manual/reference/operator/aggregation/count/
  118.      */
  119.     public function count(string $fieldName): Stage\Count
  120.     {
  121.         return $this->builder->count($fieldName);
  122.     }
  123.     /**
  124.      * Processes multiple aggregation pipelines within a single stage on the
  125.      * same set of input documents.
  126.      *
  127.      * Each sub-pipeline has its own field in the output document where its
  128.      * results are stored as an array of documents.
  129.      */
  130.     public function facet(): Stage\Facet
  131.     {
  132.         return $this->builder->facet();
  133.     }
  134.     /**
  135.      * Outputs documents in order of nearest to farthest from a specified point.
  136.      * You can only use this as the first stage of a pipeline.
  137.      *
  138.      * @see https://docs.mongodb.com/manual/reference/operator/aggregation/geoNear/
  139.      *
  140.      * @param float|array<string, mixed>|Point $x
  141.      * @param float                            $y
  142.      */
  143.     public function geoNear($x$y null): Stage\GeoNear
  144.     {
  145.         return $this->builder->geoNear($x$y);
  146.     }
  147.     /**
  148.      * Returns the assembled aggregation pipeline
  149.      *
  150.      * @return array<array<string, mixed>>
  151.      */
  152.     public function getPipeline(): array
  153.     {
  154.         return $this->builder->getPipeline();
  155.     }
  156.     /**
  157.      * Performs a recursive search on a collection, with options for restricting
  158.      * the search by recursion depth and query filter.
  159.      *
  160.      * @see https://docs.mongodb.com/manual/reference/operator/aggregation/graphLookup/
  161.      *
  162.      * @param string $from Target collection for the $graphLookup operation to
  163.      * search, recursively matching the connectFromField to the connectToField.
  164.      */
  165.     public function graphLookup(string $from): Stage\GraphLookup
  166.     {
  167.         return $this->builder->graphLookup($from);
  168.     }
  169.     /**
  170.      * Groups documents by some specified expression and outputs to the next
  171.      * stage a document for each distinct grouping.
  172.      *
  173.      * @see https://docs.mongodb.com/manual/reference/operator/aggregation/group/
  174.      */
  175.     public function group(): Stage\Group
  176.     {
  177.         return $this->builder->group();
  178.     }
  179.     /**
  180.      * Returns statistics regarding the use of each index for the collection.
  181.      *
  182.      * @see https://docs.mongodb.com/manual/reference/operator/aggregation/indexStats/
  183.      */
  184.     public function indexStats(): Stage\IndexStats
  185.     {
  186.         return $this->builder->indexStats();
  187.     }
  188.     /**
  189.      * Limits the number of documents passed to the next stage in the pipeline.
  190.      *
  191.      * @see https://docs.mongodb.com/manual/reference/operator/aggregation/limit/
  192.      *
  193.      * @return Stage\Limit
  194.      */
  195.     public function limit(int $limit)
  196.     {
  197.         return $this->builder->limit($limit);
  198.     }
  199.     /**
  200.      * Performs a left outer join to an unsharded collection in the same
  201.      * database to filter in documents from the “joined” collection for
  202.      * processing.
  203.      *
  204.      * @see https://docs.mongodb.com/manual/reference/operator/aggregation/lookup/
  205.      */
  206.     public function lookup(string $from): Stage\Lookup
  207.     {
  208.         return $this->builder->lookup($from);
  209.     }
  210.     /**
  211.      * Filters the documents to pass only the documents that match the specified
  212.      * condition(s) to the next pipeline stage.
  213.      *
  214.      * @see https://docs.mongodb.com/manual/reference/operator/aggregation/match/
  215.      */
  216.     public function match(): Stage\MatchStage
  217.     {
  218.         return $this->builder->match();
  219.     }
  220.     /**
  221.      * Takes the documents returned by the aggregation pipeline and writes them
  222.      * to a specified collection. This must be the last stage in the pipeline.
  223.      *
  224.      * @see https://docs.mongodb.com/manual/reference/operator/aggregation/out/
  225.      */
  226.     public function out(string $collection): Stage\Out
  227.     {
  228.         return $this->builder->out($collection);
  229.     }
  230.     /**
  231.      * Passes along the documents with only the specified fields to the next
  232.      * stage in the pipeline. The specified fields can be existing fields from
  233.      * the input documents or newly computed fields.
  234.      *
  235.      * @see https://docs.mongodb.com/manual/reference/operator/aggregation/project/
  236.      */
  237.     public function project(): Stage\Project
  238.     {
  239.         return $this->builder->project();
  240.     }
  241.     /**
  242.      * Restricts the contents of the documents based on information stored in
  243.      * the documents themselves.
  244.      *
  245.      * @see https://docs.mongodb.com/manual/reference/operator/aggregation/redact/
  246.      */
  247.     public function redact(): Stage\Redact
  248.     {
  249.         return $this->builder->redact();
  250.     }
  251.     /**
  252.      * Promotes a specified document to the top level and replaces all other
  253.      * fields.
  254.      *
  255.      * The operation replaces all existing fields in the input document,
  256.      * including the _id field. You can promote an existing embedded document to
  257.      * the top level, or create a new document for promotion.
  258.      *
  259.      * @param string|mixed[]|null $expression Optional. A replacement expression that
  260.      * resolves to a document.
  261.      */
  262.     public function replaceRoot($expression null): Stage\ReplaceRoot
  263.     {
  264.         return $this->builder->replaceRoot($expression);
  265.     }
  266.     /**
  267.      * Controls if resulting iterator should be wrapped with CachingIterator.
  268.      */
  269.     public function rewindable(bool $rewindable true): self
  270.     {
  271.         $this->builder->rewindable($rewindable);
  272.         return $this;
  273.     }
  274.     /**
  275.      * Randomly selects the specified number of documents from its input.
  276.      *
  277.      * @see https://docs.mongodb.com/manual/reference/operator/aggregation/sample/
  278.      */
  279.     public function sample(int $size): Stage\Sample
  280.     {
  281.         return $this->builder->sample($size);
  282.     }
  283.     /**
  284.      * Skips over the specified number of documents that pass into the stage and
  285.      * passes the remaining documents to the next stage in the pipeline.
  286.      *
  287.      * @see https://docs.mongodb.com/manual/reference/operator/aggregation/skip/
  288.      */
  289.     public function skip(int $skip): Stage\Skip
  290.     {
  291.         return $this->builder->skip($skip);
  292.     }
  293.     /**
  294.      * Groups incoming documents based on the value of a specified expression,
  295.      * then computes the count of documents in each distinct group.
  296.      *
  297.      * @see https://docs.mongodb.com/manual/reference/operator/aggregation/sortByCount/
  298.      */
  299.     public function sortByCount(string $expression): Stage\SortByCount
  300.     {
  301.         return $this->builder->sortByCount($expression);
  302.     }
  303.     /**
  304.      * Sorts all input documents and returns them to the pipeline in sorted order.
  305.      *
  306.      * If sorting by multiple fields, the first argument should be an array of
  307.      * field name (key) and order (value) pairs.
  308.      *
  309.      * @see https://docs.mongodb.com/manual/reference/operator/aggregation/sort/
  310.      *
  311.      * @param array<string, int|string>|string $fieldName Field name or array of field/order pairs
  312.      * @param int|string                       $order     Field order (if one field is specified)
  313.      */
  314.     public function sort($fieldName$order null): Stage\Sort
  315.     {
  316.         return $this->builder->sort($fieldName$order);
  317.     }
  318.     /**
  319.      * Deconstructs an array field from the input documents to output a document
  320.      * for each element. Each output document is the input document with the
  321.      * value of the array field replaced by the element.
  322.      *
  323.      * @see https://docs.mongodb.com/manual/reference/operator/aggregation/unwind/
  324.      */
  325.     public function unwind(string $fieldName): Stage\Unwind
  326.     {
  327.         return $this->builder->unwind($fieldName);
  328.     }
  329.     /**
  330.      * Allows adding an arbitrary stage to the pipeline
  331.      *
  332.      * @return Stage The method returns the stage given as an argument
  333.      */
  334.     public function addStage(Stage $stage): Stage
  335.     {
  336.         return $this->builder->addStage($stage);
  337.     }
  338. }