getFilter example


        if ($aggregation instanceof EntityAggregation) {
            return [
                'name' => $aggregation->getName(),
                'type' => 'entity',
                'field' => $aggregation->getField(),
                'definition' => $aggregation->getEntity(),
            ];
        }
        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) {
            
$criteria,
            $exception
        );

        static::assertCount(0, iterator_to_array($exception->getErrors()));
        static::assertCount(1, $criteria->getAggregations());

        /** @var FilterAggregation|null $aggregation */
        $aggregation = $criteria->getAggregation('filter_test');

        static::assertInstanceOf(FilterAggregation::class$aggregation);
        static::assertCount(2, $aggregation->getFilter());

        $filters = $aggregation->getFilter();
        $filter = array_shift($filters);

        static::assertInstanceOf(ContainsFilter::class$filter);
        static::assertSame('product.foo', $filter->getField());
        static::assertSame('bar', $filter->getValue());

        $filter = array_shift($filters);

        static::assertInstanceOf(EqualsAnyFilter::class$filter);
        
$isoList = $this->createIsoList($metaData);
        $languageFiles = $this->getSnippetFilesByIso($isoList);

        $fileSnippets = $this->getFileSnippets($languageFiles$isoList);
        $dbSnippets = $this->databaseSnippetsToArray($this->findSnippetInDatabase(new Criteria()$context)$fileSnippets);

        $snippets = array_replace_recursive($fileSnippets$dbSnippets);
        $snippets = $this->fillBlankSnippets($snippets$isoList);

        foreach ($requestFilters as $requestFilterName => $requestFilterValue) {
            $snippets = $this->snippetFilterFactory->getFilter($requestFilterName)->filter($snippets$requestFilterValue);
        }

        $snippets = $this->sortSnippets($sort$snippets);

        $total = 0;
        foreach ($snippets as &$set) {
            $total = $total > 0 ? $total : \count($set['snippets']);
            $set['snippets'] = array_chunk($set['snippets']$limit, true)[$page] ?? [];
        }

        return [
            

        $this->router  = $router ?? Services::router();
        $this->filters = $filters ?? Services::filters();
    }

    private function getRouteFilters(string $uri): array
    {
        $this->router->handle($uri);

        $multipleFiltersEnabled = config(Feature::class)->multipleFilters ?? false;
        if ($multipleFiltersEnabled) {
            $filter = $this->router->getFilter();

            return $filter === null ? [] : [$filter];
        }

        return $this->router->getFilters();
    }

    /** * @param string $uri URI path to find filters for * * @return array{before: list<string>, after: list<string>} array of filter alias or classname */
$processor->prepare(
            new Request(['reduce-aggregations' => true]),
            $criteria = new Criteria(),
            $this->createMock(SalesChannelContext::class)
        );

        static::assertCount(2, $criteria->getAggregations());
        static::assertCount(2, $criteria->getPostFilters());

        $agg = $criteria->getAggregation('foo');
        static::assertInstanceOf(FilterAggregation::class$agg);
        static::assertCount(1, $agg->getFilter());
        $filter = $agg->getFilter();
        $filter = \array_shift($filter);
        static::assertInstanceOf(EqualsFilter::class$filter);
        static::assertEquals('bar', $filter->getField());

        $agg = $criteria->getAggregation('bar');
        static::assertInstanceOf(FilterAggregation::class$agg);

        // filter is set to excluded and should not be removed by own list (property filter scenario where you also have to add the property filter to calculate the property filter)         static::assertCount(2, $agg->getFilter());
    }
}


    /** * Gets all product line items of the entire cart that * match the rules and conditions of the provided discount item. */
    public function getMatchingItems(DiscountLineItem $discount, Cart $cart, SalesChannelContext $context): DiscountPackageCollection
    {
        $allItems = $cart->getLineItems()->filter(fn (LineItem $lineItem) => $lineItem->getType() === LineItem::PRODUCT_LINE_ITEM_TYPE && $lineItem->isStackable());

        $priceDefinition = $discount->getPriceDefinition();
        if ($priceDefinition instanceof FilterableInterface && $priceDefinition->getFilter()) {
            $allItems = $allItems->filter(fn (LineItem $lineItem) => $priceDefinition->getFilter()->match(new LineItemScope($lineItem$context)));
        }

        $discountPackage = $this->getDiscountPackage($allItems);
        if ($discountPackage === null) {
            return new DiscountPackageCollection([]);
        }

        return new DiscountPackageCollection([$discountPackage]);
    }

    
$aggregation instanceof RangeAggregation => $this->parseRangeAggregation($aggregation$query$definition$context),
            default => throw new InvalidAggregationQueryException(sprintf('Aggregation of type %s not supported', $aggregation::class)),
        };
    }

    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,
        

            '_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());
    }
}
$children = $this->calculateLineItems($lineItem->getChildren()$context$behavior);

            $lineItem->setChildren($children);

            return $children->getPrices()->sum();
        }

        $definition = $lineItem->getPriceDefinition();

        if ($definition instanceof AbsolutePriceDefinition) {
            // reduce line items for provided filter             $prices = $this->filterLineItems($calculated$definition->getFilter()$context)
                ->getPrices();

            return $this->absolutePriceCalculator->calculate($definition->getPrice()$prices$context);
        }

        if ($definition instanceof PercentagePriceDefinition) {
            // reduce line items for provided filter             $prices = $this->filterLineItems($calculated$definition->getFilter()$context)
                ->getPrices();

            return $this->percentagePriceCalculator->calculate($definition->getPercentage()$prices$context);
        }


    private function isRulesFilterValid(LineItem $item, PriceDefinitionInterface $priceDefinition, SalesChannelContext $context): bool
    {
        // if the price definition doesnt allow filters,         // then return valid for the item         if (!method_exists($priceDefinition, 'getFilter')) {
            return true;
        }

        /** @var Rule|null $filter */
        $filter = $priceDefinition->getFilter();

        // if the definition exists, but is empty         // this means we have no restrictions and thus its valid         if (!$filter instanceof Rule) {
            return true;
        }

        // if our price definition has a filter rule         // then extract it, and check if it matches         $scope = new LineItemScope($item$context);

        
static::assertInstanceOf(Filter::class$filter);
        static::assertEquals('manufacturer', $filter->getName());
        static::assertTrue($filter->isFiltered());

        $aggregations = $filter->getAggregations();
        static::assertCount(1, $aggregations);
        static::assertInstanceOf(EntityAggregation::class$aggregations[0]);
        static::assertEquals('manufacturer', $aggregations[0]->getName());
        static::assertEquals('product.manufacturerId', $aggregations[0]->getField());
        static::assertEquals('product_manufacturer', $aggregations[0]->getEntity());

        $criteriaFilter = $filter->getFilter();
        static::assertInstanceOf(EqualsAnyFilter::class$criteriaFilter);
        static::assertEquals('product.manufacturerId', $criteriaFilter->getField());
        static::assertEquals($manufacturerIds$criteriaFilter->getValue());
        static::assertEquals($manufacturerIds$filter->getValues());
    }

    public function testCreateWithEmptyManufacturerIds(): void
    {
        $request = new Request();
        $request->request->set('manufacturer-filter', true);
        $request->request->set('manufacturer', '');

        


        if ($this->search !== null) {
            $options['search'] = $this->search;
        }

        if ($this->orderBy !== null) {
            $options['orderBy'] = $this->orderBy;
            $options['orderSequence'] = $this->orderSequence;
        }

        foreach ($this->getFilter() as $filter) {
            $options = [...$options, ...$filter->getQueryParameter()];
        }

        return $options;
    }

    public function setLimit(int $limit): void
    {
        $this->limit = $limit;
    }

    
return $filtered;
    }

    public function filtered(): FilterCollection
    {
        return $this->filter(fn (Filter $filter) => $filter->isFiltered() ? $filter : null);
    }

    public function getFilters(): array
    {
        return $this->fmap(fn (Filter $filter) => $filter->getFilter());
    }

    protected function getExpectedClass(): ?string
    {
        return Filter::class;
    }
}
$ruleCollection = new RuleCollection();
        $ruleCollection->add($discountRuleEntity);
        $discount->setDiscountRules($ruleCollection);

        $expectedRule = new OrRule();
        $expectedRule->addRule($discountFilter);

        $item = $builder->buildDiscountLineItem('', $this->promotion, $discount, 'C1', $currencyFactor);

        static::assertInstanceOf(AbsolutePriceDefinition::class$item->getPriceDefinition());
        static::assertEquals($expectedRule$item->getPriceDefinition()->getFilter());
    }

    /** * This test verifies that the correct discount filter * is set in the discountItemBuilder * * @group promotions */
    public function testDiscountTargetFilterIfDiscountRulesShouldBeIgnored(): void
    {
        $currencyFactor = random_int(0, mt_getrandmax()) / mt_getrandmax();

        
class FilterExpression extends CallExpression
{
    public function __construct(Node $node, ConstantExpression $filterName, Node $arguments, int $lineno, string $tag = null)
    {
        parent::__construct(['node' => $node, 'filter' => $filterName, 'arguments' => $arguments][]$lineno$tag);
    }

    public function compile(Compiler $compiler): void
    {
        $name = $this->getNode('filter')->getAttribute('value');
        $filter = $compiler->getEnvironment()->getFilter($name);

        $this->setAttribute('name', $name);
        $this->setAttribute('type', 'filter');
        $this->setAttribute('needs_environment', $filter->needsEnvironment());
        $this->setAttribute('needs_context', $filter->needsContext());
        $this->setAttribute('arguments', $filter->getArguments());
        $this->setAttribute('callable', $filter->getCallable());
        $this->setAttribute('is_variadic', $filter->isVariadic());

        $this->compileCallable($compiler);
    }
}
Home | Imprint | This part of the site doesn't use cookies.