ProtokollDTO.php

Code Hygiene Score: 100

Keine Issues gefunden.

Klassen 1

Funktionen 3

Versionen 5

Code

<?php

declare(strict_types=1);

namespace UseCases\Protokoll;

// @responsibility: Datenübertragungsobjekt für KI-Protokoll-Einträge

final class ProtokollDTO
{
    public function __construct(
        public readonly int $id,
        public readonly ?string $timestamp,
        public readonly ?string $clientName,
        public readonly ?string $modelName,
        public readonly ?string $request,
        public readonly ?string $response,
        public readonly ?string $status,
        public readonly ?int $tokensTotal,
        public readonly ?int $durationMs,
        public readonly ?string $requestPreview = null,
        public readonly ?string $requestFormatted = null,
        public readonly ?string $responseFormatted = null,
        public readonly ?string $requestIp = null,
        public readonly ?string $requestTimestamp = null,
        public readonly ?string $responseTimestamp = null,
        public readonly ?int $tokensInput = null,
        public readonly ?int $tokensOutput = null,
        public readonly ?string $errorMessage = null,
    ) {
    }

    public static function fromArray(array $data): self
    {
        return new self(
            id: (int) $data['id'],
            timestamp: $data['timestamp'] ?? null,
            clientName: $data['client_name'] ?? null,
            modelName: $data['model_name'] ?? null,
            request: $data['request'] ?? null,
            response: $data['response'] ?? null,
            status: $data['status'] ?? null,
            tokensTotal: isset($data['tokens_total']) ? (int) $data['tokens_total'] : null,
            durationMs: isset($data['duration_ms']) ? (int) $data['duration_ms'] : null,
            requestPreview: $data['request_preview'] ?? null,
            requestFormatted: $data['request_formatted'] ?? null,
            responseFormatted: $data['response_formatted'] ?? null,
            requestIp: $data['request_ip'] ?? null,
            requestTimestamp: $data['request_timestamp'] ?? null,
            responseTimestamp: $data['response_timestamp'] ?? null,
            tokensInput: isset($data['tokens_input']) ? (int) $data['tokens_input'] : null,
            tokensOutput: isset($data['tokens_output']) ? (int) $data['tokens_output'] : null,
            errorMessage: $data['error_message'] ?? null,
        );
    }

    public function toArray(): array
    {
        return [
            'id' => $this->id,
            'timestamp' => $this->timestamp,
            'client_name' => $this->clientName,
            'model_name' => $this->modelName,
            'request' => $this->request,
            'response' => $this->response,
            'status' => $this->status,
            'tokens_total' => $this->tokensTotal,
            'duration_ms' => $this->durationMs,
            'request_preview' => $this->requestPreview,
            'request_formatted' => $this->requestFormatted,
            'response_formatted' => $this->responseFormatted,
            'request_ip' => $this->requestIp,
            'request_timestamp' => $this->requestTimestamp,
            'response_timestamp' => $this->responseTimestamp,
            'tokens_input' => $this->tokensInput,
            'tokens_output' => $this->tokensOutput,
            'error_message' => $this->errorMessage,
        ];
    }
}
← Übersicht Graph