createInstanceFromNamedArguments example

if (empty($json)) {
            return $this->variantFacet = null;
        }

        /** @var array<class-string<VariantFacet>, array{label: string, depth: string}> $arr */
        $arr = json_decode($json, true);

        if (empty($arr)) {
            return $this->variantFacet = null;
        }

        $variantFacet = $this->reflectionHelper->createInstanceFromNamedArguments(key($arr)reset($arr));
        $this->variantFacet = $variantFacet;

        return $this->variantFacet;
    }

    /** * @throws RuntimeException * @throws InvalidArgumentException */
    public function joinPrices(QueryBuilder $query, ShopContextInterface $context, Criteria $criteria)
    {
        

        $reflector = new ReflectionHelper();
        if (empty($serialized)) {
            return [];
        }

        $sortings = [];
        foreach ($serialized as $className => $arguments) {
            $className = explode('|', $className);
            /** @var class-string<ConditionInterface> $className */
            $className = $className[0];
            $condition = $reflector->createInstanceFromNamedArguments($className$arguments);
            $sortings[] = $condition;
        }

        return $sortings;
    }
}

    public function unserialize($serialized$errorSource)
    {
        $classes = [];

        foreach ($serialized as $className => $arguments) {
            $className = explode('|', $className);
            /** @var class-string<ConditionInterface|SortingInterface|FacetInterface> $className */
            $className = $className[0];

            try {
                $classes[] = $this->reflector->createInstanceFromNamedArguments($className$arguments);
            } catch (Exception $e) {
                $this->logger->critical($errorSource . ': ' . $e->getMessage());
            }
        }

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