buildAccessor example

return null;
        }

        $jsonPath = preg_replace(
            '#^' . preg_quote($field->getPropertyName(), '#') . '#',
            '',
            $accessor
        );

        $accessor = $field->getPropertyName() . '.' . ConfigJsonField::STORAGE_KEY . $jsonPath;

        return parent::buildAccessor($root$field$context$accessor);
    }
}


    /** * @dataProvider accessorContextProvider */
    public function testBuildAccessor(string $field, Context $context, string $expectedAccessor): void
    {
        $definition = $this->getDefinition();
        $storage = $this->createMock(AbstractKeyValueStorage::class);
        $storage->method('get')->willReturn(true);

        $accessor = (new CriteriaParser(new EntityDefinitionQueryHelper()$this->createMock(CustomFieldService::class)$storage))->buildAccessor($definition$field$context);

        static::assertSame($expectedAccessor$accessor);
    }

    /** * @return iterable<string, Filter|array<mixed>> */
    public static function parseFilterDataProvider(): iterable
    {
        $now = '2023-06-12 05:36:22.000';

        
protected function setUp(): void
    {
        $this->builder = $this->getContainer()->get(PriceFieldAccessorBuilder::class);
    }

    public function testWithPriceAccessor(): void
    {
        $priceField = new PriceField('price', 'price');
        $context = Context::createDefaultContext();

        $sql = $this->builder->buildAccessor('product', $priceField$context, 'price');

        static::assertSame('(ROUND((ROUND(CAST((COALESCE((JSON_UNQUOTE(JSON_EXTRACT(`product`.`price`, "$.cb7d2554b0ce847cd82f3ac9bd1c0dfca.gross")) + 0.0))) as DECIMAL(30, 20)), 2)) * 100, 0) / 100)', $sql);
    }

    public function testWithListPriceAccessor(): void
    {
        $priceField = new PriceField('price', 'price');
        $context = Context::createDefaultContext();

        $sql = $this->builder->buildAccessor('product', $priceField$context, 'price.listPrice');

        

        }

        if ($this->keyValueStorage->get(ElasticsearchIndexer::ENABLE_MULTILINGUAL_INDEX_KEY, false)) {
            $field = $this->helper->getField($sorting->getField()$definition$definition->getEntityName(), false);

            if ($field instanceof TranslatedField) {
                return $this->createTranslatedSorting($definition->getEntityName()$sorting$context);
            }
        }

        $accessor = $this->buildAccessor($definition$sorting->getField()$context);

        if ($sorting instanceof CountSorting) {
            return new CountSort($accessor$sorting->getDirection());
        }

        return new FieldSort($accessor$sorting->getDirection());
    }

    public function parseAggregation(Aggregation $aggregation, EntityDefinition $definition, Context $context): ?AbstractAggregation
    {
        $fieldName = $this->buildAccessor($definition$aggregation->getField()$context);

        
$this->builder = new CheapestPriceAccessorBuilder(
            1,
            new NullLogger()
        );
    }

    public function testWithPriceAccessor(): void
    {
        $priceField = new CheapestPriceField('cheapest_price_accessor', 'cheapest_price_accessor');
        $context = Context::createDefaultContext();

        $sql = $this->builder->buildAccessor('product', $priceField$context, 'cheapestPrice');

        static::assertSame('COALESCE((ROUND((ROUND(CAST((JSON_UNQUOTE(JSON_EXTRACT(`product`.`cheapest_price_accessor`, "$.ruledefault.currencyb7d2554b0ce847cd82f3ac9bd1c0dfca.gross")) * 1) as DECIMAL(30, 20)), 2)) * 100, 0) / 100))', $sql);
    }

    public function testWithListPriceAccessor(): void
    {
        $priceField = new CheapestPriceField('cheapest_price_accessor', 'cheapest_price_accessor');
        $context = Context::createDefaultContext();

        $sql = $this->builder->buildAccessor('product', $priceField$context, 'cheapestPrice.listPrice');

        
if (!$variantIds) {
                continue;
            }

            $existingAccessors = $this->connection->fetchAllKeyValue(
                'SELECT id, cheapest_price_accessor FROM product WHERE parent_id = :id AND version_id = :version',
                ['id' => Uuid::fromHexToBytes($productId), 'version' => $versionId]
            );

            foreach ($container->getVariantIds() as $variantId) {
                $accessor = Json::encode($this->buildAccessor($container$variantId));

                if (($existingAccessors[Uuid::fromHexToBytes($variantId)] ?? null) === $accessor) {
                    continue;
                }

                $accessorQuery->execute([
                    'accessor' => $accessor,
                    'id' => Uuid::fromHexToBytes($variantId),
                    'version' => $versionId,
                ]);
            }
        }
/** * @param FieldGrouping[] $groupings * * @return array{field: string, inner_hits?: array{name: string}} */
    private function parseGrouping(array $groupings, EntityDefinition $definition, Context $context): array
    {
        /** @var FieldGrouping $grouping */
        $grouping = array_shift($groupings);

        $accessor = $this->criteriaParser->buildAccessor($definition$grouping->getField()$context);
        if (empty($groupings)) {
            return ['field' => $accessor];
        }

        return [
            'field' => $accessor,
            'inner_hits' => [
                'name' => 'inner',
                'collapse' => $this->parseGrouping($groupings$definition$context),
            ],
        ];
    }

        $attributeName = preg_replace(
            '#^' . preg_quote($field->getPropertyName(), '#') . '\.("([^"]*)"|([^.]*)).*#',
            '$2$3',
            $accessor
        );
        $attributeField = $this->customFieldService->getCustomField($attributeName)
            ?? new JsonField($attributeName$attributeName);

        $field->setPropertyMapping([$attributeField]);

        return parent::buildAccessor($root$field$context$accessor);
    }
}
return sprintf('IFNULL(%s, %s)', $select$parentSelect);
    }

    private function buildFieldSelector(string $root, Field $field, Context $context, string $accessor): string
    {
        $accessorBuilder = $field->getAccessorBuilder();
        if (!$accessorBuilder) {
            throw new FieldAccessorBuilderNotFoundException($field->getPropertyName());
        }

        $accessor = $accessorBuilder->buildAccessor($root$field$context$accessor);

        if (!$accessor) {
            throw new \RuntimeException(sprintf('Can not build accessor for field "%s" on root "%s"', $field->getPropertyName()$root));
        }

        return $accessor;
    }
}
Home | Imprint | This part of the site doesn't use cookies.