getAggregation example

private function parseFilterAggregation(
        FilterAggregation $aggregation,
        QueryBuilder $query,
        EntityDefinition $definition,
        Context $context
    ): void {
        if (!empty($aggregation->getFilter())) {
            $this->criteriaQueryBuilder->addFilter($definitionnew MultiFilter(MultiFilter::CONNECTION_AND, $aggregation->getFilter())$query$context);
        }

        $aggregationStruct = $aggregation->getAggregation();
        \assert($aggregationStruct !== null, 'FilterAggregation always have an aggregation');

        $this->extendQuery($aggregationStruct$query$definition$context);
    }

    private function parseDateHistogramAggregation(
        DateHistogramAggregation $aggregation,
        QueryBuilder $query,
        EntityDefinition $definition,
        Context $context
    ): void {
        


    public function hydrate(EntityDefinition $definition, Criteria $criteria, Context $context, array $result): AggregationResultCollection
    {
        if (!isset($result['aggregations'])) {
            return new AggregationResultCollection();
        }

        $aggregations = new AggregationResultCollection();

        foreach ($result['aggregations'] as $name => $aggResult) {
            $aggregation = $criteria->getAggregation($name);

            if (!$aggregation) {
                continue;
            }

            $hydration = $this->hydrateAggregation($aggregation$aggResult$context);
            if ($hydration) {
                $aggregations->add(
                    $hydration
                );
            }
        }

        if ($aggregation instanceof FilterAggregation) {
            $filters = [];
            foreach ($aggregation->getFilter() as $filter) {
                $filters[] = QueryStringParser::toArray($filter);
            }

            return [
                'name' => $aggregation->getName(),
                'type' => 'filter',
                'filter' => $filters,
                'aggregation' => $this->aggregationToArray($aggregation->getAggregation()),
            ];
        }
        if ($aggregation instanceof DateHistogramAggregation) {
            $data = [
                'name' => $aggregation->getName(),
                'type' => 'histogram',
                'interval' => $aggregation->getInterval(),
                'format' => $aggregation->getFormat(),
                'field' => $aggregation->getField(),
                'timeZone' => $aggregation->getTimeZone(),
            ];

            

            ],
            '_class' => FilterAggregation::class,
        ]json_decode(json_encode($aggregation->jsonSerialize(), \JSON_THROW_ON_ERROR), true));
    }

    public function testClone(): void
    {
        $aggregation = new FilterAggregation('foo', new TermsAggregation('foo', 'name')[new EqualsFilter('name', 'test')]);
        $clone = clone $aggregation;
        static::assertEquals($aggregation->getName()$clone->getName());
        static::assertEquals($aggregation->getAggregation()$clone->getAggregation());
        static::assertEquals($aggregation->getFilter()$clone->getFilter());
        static::assertEquals($aggregation->jsonSerialize()$clone->jsonSerialize());
    }
}
$filter instanceof EqualsAnyFilter => $this->parseEqualsAnyFilter($filter$definition$context),
            $filter instanceof ContainsFilter => $this->parseContainsFilter($filter$definition$context),
            $filter instanceof PrefixFilter => $this->parsePrefixFilter($filter$definition$context),
            $filter instanceof SuffixFilter => $this->parseSuffixFilter($filter$definition$context),
            $filter instanceof RangeFilter => $this->parseRangeFilter($filter$definition$context),
            default => throw new \RuntimeException(sprintf('Unsupported filter %s', $filter::class)),
        };
    }

    protected function parseFilterAggregation(FilterAggregation $aggregation, EntityDefinition $definition, Context $context): AbstractAggregation
    {
        if ($aggregation->getAggregation() === null) {
            throw new \RuntimeException(sprintf('Filter aggregation %s contains no nested aggregation.', $aggregation->getName()));
        }

        $nested = $this->parseAggregation($aggregation->getAggregation()$definition$context);
        if ($nested === null) {
            throw new \RuntimeException(sprintf('Nested filter aggregation %s can not be parsed.', $aggregation->getName()));
        }

        // when aggregation inside the filter aggregation points to a nested object (e.g. product.properties.id) we have to add all filters         // which points to the same association to the same "nesting" level like the nested aggregation for this association         $path = $nested instanceof NestedAggregation ? $nested->getPath() : null;
        
'type' => 'avg',
                        'field' => 'stock',
                    ],
                ],
            ],
            $criteria,
            $exception
        );
        static::assertCount(0, iterator_to_array($exception->getErrors()));
        static::assertCount(2, $criteria->getAggregations());

        $maxAggregation = $criteria->getAggregation('max_agg');
        static::assertInstanceOf(MaxAggregation::class$maxAggregation);
        static::assertEquals('max_agg', $maxAggregation->getName());
        static::assertEquals('product.tax.taxRate', $maxAggregation->getField());

        $avgAggregation = $criteria->getAggregation('avg_agg');
        static::assertInstanceOf(AvgAggregation::class$avgAggregation);
        static::assertEquals('avg_agg', $avgAggregation->getName());
        static::assertEquals('product.stock', $avgAggregation->getField());
    }

    public function testBuildAggregationsWithSameName(): void
    {
$foo = new FooListingFilterHandler()],
            $this->createMock(EventDispatcherInterface::class)
        );

        $context = $this->createMock(SalesChannelContext::class);

        $criteria = new Criteria();
        $processor->prepare(new Request()$criteria$context);

        static::assertCount(1, $criteria->getAggregations());
        static::assertCount(1, $criteria->getPostFilters());
        static::assertInstanceOf(TermsAggregation::class$criteria->getAggregation('foo'));
        static::assertTrue($foo->called);
    }

    public function testByPassProcess(): void
    {
        $processor = new AggregationListingProcessor(
            [$foo = new FooListingFilterHandler()],
            $this->createMock(EventDispatcherInterface::class)
        );

        $context = $this->createMock(SalesChannelContext::class);

        
Home | Imprint | This part of the site doesn't use cookies.