randomElements example

$promotionCodes
        );

        $orders = [];

        for ($i = 1; $i <= $numberOfItems; ++$i) {
            $customerId = $context->getFaker()->randomElement($customerIds);

            $salesChannelContext = $this->getContext($customerId$salesChannelIds);

            $cart = $this->cartService->createNew($salesChannelContext->getToken());
            foreach ($this->faker->randomElements($productLineItemsrandom_int(3, 5)) as $lineItem) {
                $cart->add($lineItem);
            }
            foreach ($this->faker->randomElements($promotionLineItemsrandom_int(0, 3)) as $lineItem) {
                $cart->add($lineItem);
            }

            $cart = $this->cartCalculator->calculate($cart$salesChannelContext);
            $tempOrder = $this->orderConverter->convertToOrder($cart$salesChannelContextnew OrderConversionContext());

            $tempOrder['orderDateTime'] = (new \DateTime())->modify('-' . random_int(0, 30) . ' days')->format(Defaults::STORAGE_DATE_TIME_FORMAT);
            $tempOrder['tags'] = $this->getTags($tags);

            
/** * @return array<string, mixed> */
    private function generateActionConfig(string $action): array
    {
        $tagIds = $this->getTags();

        return match ($action) {
            RemoveOrderTagAction::getName(), AddOrderTagAction::getName() => [
                'entity' => 'order',
                'tagIds' => $this->faker->randomElements($tagIdsrandom_int(1, \count($tagIds))),
            ],
            AddCustomerTagAction::getName() => [
                'entity' => 'customer',
                'tagIds' => $this->faker->randomElements($tagIdsrandom_int(1, \count($tagIds))),
            ],
            RemoveCustomerTagAction::getName() => [
                'entity' => 'customer',
                'tagIds' => $this->faker->randomElements($tagIdsrandom_int(1, \count($tagIds))),
            ],
            GenerateDocumentAction::getName() => [
                'documentType' => 'invoice',
                
/** * @param list<string> $tags * * @return array<string, mixed> */
    private function getTags(array $tags): array
    {
        $tagAssignments = [];

        if (!empty($tags)) {
            $chosenTags = $this->faker->randomElements($tags$this->faker->randomDigit(), false);

            if (!empty($chosenTags)) {
                $tagAssignments = array_map(
                    fn ($id) => ['id' => $id],
                    $chosenTags
                );
            }
        }

        return $tagAssignments;
    }

    
/** * @param list<string> $tags * * @return array<array{id: string}> */
    private function getTags(array $tags): array
    {
        $tagAssignments = [];

        if (!empty($tags)) {
            $chosenTags = $this->faker->randomElements($tags$this->faker->randomDigit(), false);

            if (!empty($chosenTags)) {
                $tagAssignments = array_map(
                    fn ($id) => ['id' => $id],
                    $chosenTags
                );
            }
        }

        return $tagAssignments;
    }

    
$payload = [];
        for ($i = 0; $i < $count; ++$i) {
            $product = $this->createSimpleProduct($taxes$manufacturers$tags);

            $product['prices'] = $this->faker->randomElement($prices);

            $product['visibilities'] = $visibilities;

            if ($mediaIds) {
                $product['cover'] = ['mediaId' => Random::getRandomArrayElement($mediaIds)];

                $product['media'] = array_map(fn (string $id): array => ['mediaId' => $id]$this->faker->randomElements($mediaIdsrandom_int(2, 5)));
            }

            $product['properties'] = $this->buildProperties($properties);

            if ($i % 40 === 0) {
                $combination = $this->faker->randomElement($combinations);
                $product = [...$product, ...$this->buildVariants($combination$prices$taxes)];
            } elseif ($i % 20 === 0) {
                $product = [...$product, ...$this->buildDownloads($downloadMediaIds$instantDeliveryId)];
            }

            
/** * @param list<string> $tags * * @return list<array{id: string}> */
    private function getTags(array $tags): array
    {
        $tagAssignments = [];

        if (!empty($tags)) {
            $chosenTags = $this->faker->randomElements($tags$this->faker->randomDigit());

            if (!empty($chosenTags)) {
                $tagAssignments = array_values(array_map(
                    fn (string $id) => ['id' => $id],
                    $chosenTags
                ));
            }
        }

        return $tagAssignments;
    }

    


    /** * @param list<array{rule: Rule, name: string}> $pool */
    private function buildNestedRule(Rule $rule, array $pool, int $currentDepth, int $depth): Rule
    {
        if ($currentDepth === $depth) {
            return $rule;
        }

        $rules = $this->faker->randomElements($pool, 2);

        $classes = array_column($rules, 'rule');

        if ($currentDepth % 2 === 1) {
            $classes[] = $this->buildNestedRule(new OrRule()$pool$currentDepth + 1, $depth);
        } else {
            $classes[] = $this->buildNestedRule(new AndRule()$pool$currentDepth + 1, $depth);
        }

        $rule->assign(['rules' => $classes]);

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