Handler/FormAddItemHandler.php
public function execute(CommandInterface $command, AggregateInterface $aggregate): AggregateInterface
    {
        $payload = $command->getPayload();

        if (isset($payload['data']['name'])) {
            // Clean the name to only contain lowercase letters.
            $payload['data']['name'] = strtolower(preg_replace('/[^A-Za-z0-9]/', '', $payload['data']['name']));
        }

        $itemName = $payload['itemName'];
        $data = $payload['data'];

        // Build item data.
        $newItem = [
            'uuid' => $command->getUuid(),
            'itemName' => $itemName,
            'data' => $data,
        ];

        // Add to items.
        $parentUuid = $payload['parent'] ?? null;

        if ($parentUuid && \is_string($parentUuid)) {
            // A function that add the new item to the target parent.
            $addItemFunction = function (&$item, &$collection) use ($newItem) {
                if (!isset($item['items'])) {
                    $item['items'] = [];
                }
                $item['items'][] = $newItem;
            };
            self::onItem($aggregate, $parentUuid, $addItemFunction);
        } else {
            // Add to Form root.
            $aggregate->items[] = $newItem;
        }

        return $aggregate;
    }
public function execute(CommandInterface $command, AggregateInterface $aggregate): AggregateInterface
    {
        $payload = $command->getPayload();

        if (isset($payload['data']['name'])) {
            // Clean the name to only contain lowercase letters.
            $payload['data']['name'] = strtolower(preg_replace('/[^A-Za-z0-9]/', '', $payload['data']['name']));
        }

        $itemName = $payload['itemName'];
        $data = $payload['data'];

        // Build item data.
        $newItem = [
            'uuid' => $command->getUuid(),
            'itemName' => $itemName,
            'data' => $data,
        ];

        // Add to items.
        $parentUuid = $payload['parent'] ?? null;

        if ($parentUuid && \is_string($parentUuid)) {
            // A function that add the new item to the target parent.
            $addItemFunction = function (&$item, &$collection) use ($newItem) {
                if (!isset($item['items'])) {
                    $item['items'] = [];
                }
                $item['items'][] = $newItem;
            };
            self::onItem($aggregate, $parentUuid, $addItemFunction);
        } else {
            // Add to Form root.
            $aggregate->items[] = $newItem;
        }

        return $aggregate;
    }
{@inheritdoc}

@var Form $aggregate