ConfigDTO.php

Code Hygiene Score: 100

Keine Issues gefunden.

Klassen 1

Funktionen 4

Versionen 4

Code

<?php

declare(strict_types=1);

namespace UseCases\Config;

// @responsibility: Datenübertragungsobjekt für Content-Konfigurationen

final class ConfigDTO
{
    public function __construct(
        public readonly ?int $id,
        public readonly string $type,
        public readonly string $name,
        public readonly string $slug,
        public readonly ?string $description,
        public readonly string $content,
        public readonly string $version,
        public readonly string $status,
        public readonly ?int $parentId,
        public readonly ?int $promptId = null,
        public readonly int $sortOrder = 0,
        public readonly ?string $parentName = null,
        public readonly ?string $createdAt = null,
        public readonly ?string $updatedAt = null,
    ) {
    }

    public static function fromArray(array $data): self
    {
        return new self(
            id: isset($data['id']) ? (int) $data['id'] : null,
            type: $data['type'] ?? '',
            name: $data['name'] ?? '',
            slug: $data['slug'] ?? '',
            description: $data['description'] ?? null,
            content: $data['content'] ?? '{}',
            version: $data['version'] ?? '1.0',
            status: $data['status'] ?? 'draft',
            parentId: isset($data['parent_id']) ? (int) $data['parent_id'] : null,
            promptId: isset($data['prompt_id']) ? (int) $data['prompt_id'] : null,
            sortOrder: (int) ($data['sort_order'] ?? 0),
            parentName: $data['parent_name'] ?? null,
            createdAt: $data['created_at'] ?? null,
            updatedAt: $data['updated_at'] ?? null,
        );
    }

    public function getContentDecoded(): ?array
    {
        $decoded = json_decode($this->content, true);

        return is_array($decoded) ? $decoded : null;
    }

    public function toArray(): array
    {
        return [
            'id' => $this->id,
            'type' => $this->type,
            'name' => $this->name,
            'slug' => $this->slug,
            'description' => $this->description,
            'content' => $this->content,
            'version' => $this->version,
            'status' => $this->status,
            'parent_id' => $this->parentId,
            'prompt_id' => $this->promptId,
            'sort_order' => $this->sortOrder,
            'parent_name' => $this->parentName,
            'created_at' => $this->createdAt,
            'updated_at' => $this->updatedAt,
        ];
    }
}
← Übersicht Graph