Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
ValidationResult
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
2 / 2
4
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getFirstError
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
3
1<?php
2
3declare(strict_types=1);
4
5/** @license For full copyright and license information, please see the LICENSE.md file. */
6
7namespace App\Shared\Infrastructure\Validation;
8
9defined('AMMONLY_APP') || exit('Direct script access is forbidden.');
10
11/**
12 * Encapsulates Request Validation Result.
13 *
14 * Holds validation success state, hydrated DTO instance, and validation error messages array.
15 *
16 * @package App\Shared\Infrastructure\Validation
17 * @template T of object
18 */
19final readonly class ValidationResult
20{
21    /**
22     * ValidationResult constructor.
23     *
24     * @param bool $isValid True if data passed all validation rules.
25     * @param T|null $dto Hydrated DTO instance or null if invalid.
26     * @param array<string, array<int, string>> $errors Array of field error messages.
27     */
28    public function __construct(
29        public bool $isValid,
30        public ?object $dto = null,
31        public array $errors = []
32    ) {
33    }
34
35    /**
36     * Gets first error message string if errors exist.
37     *
38     * @return string|null First error message or null.
39     */
40    public function getFirstError(): ?string
41    {
42        foreach ($this->errors as $fieldErrors) {
43            if (!empty($fieldErrors)) {
44                return $fieldErrors[0];
45            }
46        }
47
48        return null;
49    }
50}