UpdatePromptCommand.php

Code Hygiene Score: 100

Keine Issues gefunden.

Dependencies 1

Klassen 1

Funktionen 4

Verwendet von 1

Versionen 3

Code

<?php

declare(strict_types=1);

namespace UseCases\Command;

// @responsibility: Command-Objekt für Prompt-Aktualisierung

use Domain\Constants;

final class UpdatePromptCommand
{
    public function __construct(
        public readonly int $id,
        public readonly string $name,
        public readonly string $version,
        public readonly string $content,
        public readonly bool $isActive,
    ) {
    }

    /**
     * @param array<string, mixed> $data
     */
    public static function fromRequest(int $id, array $data): self
    {
        return new self(
            id: $id,
            name: trim((string) ($data['name'] ?? '')),
            version: trim((string) ($data['version'] ?? '1.0')),
            content: trim((string) ($data['content'] ?? '')),
            isActive: isset($data['is_active']),
        );
    }

    /**
     * @return array<string>
     */
    public function validate(): array
    {
        $errors = [];

        if ($this->id < 1) {
            $errors[] = 'Ungültige Prompt-ID.';
        }

        if ($this->name === '') {
            $errors[] = 'Name ist erforderlich.';
        }

        if (mb_strlen($this->name) > Constants::NAME_MAX_LENGTH) {
            $errors[] = 'Name darf maximal ' . Constants::NAME_MAX_LENGTH . ' Zeichen lang sein.';
        }

        if ($this->content === '') {
            $errors[] = 'Inhalt ist erforderlich.';
        }

        return $errors;
    }

    public function isValid(): bool
    {
        return $this->validate() === [];
    }
}
← Übersicht Graph