Backup #795

ID795
Dateipfad/var/www/dev.campus.systemische-tools.de/src/Infrastructure/Validation/ValidationResult.php
Version1
Typ modified
Größe1.7 KB
Hash7610ca6dfb96aa10fbb82559cc9da0755e7cdafd2f47ad2f07c1c632c7e35ba0
Datum2025-12-23 08:06:28
Geändert vonclaude-code-hook
GrundClaude Code Pre-Hook Backup vor Edit-Operation
Datei existiert Ja

Dateiinhalt

<?php

declare(strict_types=1);

namespace Infrastructure\Validation;

/**
 * Value Object for validation results.
 *
 * Provides a type-safe way to return validation outcomes with optional
 * error messages and additional data. Immutable by design.
 *
 * @package Infrastructure\Validation
 */
final readonly class ValidationResult
{
    /**
     * @param bool        $valid Whether validation passed
     * @param string|null $error Error message if validation failed
     * @param mixed       $data  Additional data (e.g., validated value)
     */
    private function __construct(
        public bool $valid,
        public ?string $error,
        public mixed $data = null
    ) {
    }

    /**
     * Create a successful validation result.
     *
     * @param mixed $data Optional data to include (e.g., validated/normalized value)
     */
    public static function ok(mixed $data = null): self
    {
        return new self(true, null, $data);
    }

    /**
     * Create a failed validation result.
     *
     * @param string $message Error message describing the validation failure
     */
    public static function error(string $message): self
    {
        return new self(false, $message);
    }

    /**
     * Check if validation passed.
     */
    public function isValid(): bool
    {
        return $this->valid;
    }

    /**
     * Check if validation failed.
     */
    public function isError(): bool
    {
        return !$this->valid;
    }

    /**
     * Get error message or null if valid.
     */
    public function getError(): ?string
    {
        return $this->error;
    }

    /**
     * Get associated data.
     */
    public function getData(): mixed
    {
        return $this->data;
    }
}

Vollständig herunterladen

Aktionen

Herunterladen

← Zurück zur Übersicht