aboutsummaryrefslogtreecommitdiffstats
path: root/vendor/bakame/http-structured-fields/src/Validation/ItemValidator.php
blob: 21d8cdd2f31c30f5a180c0152ff92ae009a28fe0 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
<?php

declare(strict_types=1);

namespace Bakame\Http\StructuredFields\Validation;

use Bakame\Http\StructuredFields\Item;
use Bakame\Http\StructuredFields\StructuredFieldProvider;
use Bakame\Http\StructuredFields\SyntaxError;
use Stringable;

/**
 * Structured field Item validator.
 *
 * @phpstan-import-type SfType from StructuredFieldProvider
 */
final class ItemValidator
{
    /** @var callable(SfType): (string|bool) */
    private mixed $valueConstraint;
    private ParametersValidator $parametersConstraint;

    /**
     * @param callable(SfType): (string|bool) $valueConstraint
     */
    private function __construct(
        callable $valueConstraint,
        ParametersValidator $parametersConstraint,
    ) {
        $this->valueConstraint = $valueConstraint;
        $this->parametersConstraint = $parametersConstraint;
    }

    public static function new(): self
    {
        return new self(fn (mixed $value) => false, ParametersValidator::new());
    }

    /**
     * Validates the Item value.
     *
     * On success populate the result item property
     * On failure populates the result errors property
     *
     * @param callable(SfType): (string|bool) $constraint
     */
    public function value(callable $constraint): self
    {
        return new self($constraint, $this->parametersConstraint);
    }

    /**
     * Validates the Item parameters as a whole.
     *
     * On failure populates the result errors property
     */
    public function parameters(ParametersValidator $constraint): self
    {
        return new self($this->valueConstraint, $constraint);
    }

    public function __invoke(Item|Stringable|string $item): bool|string
    {
        $result = $this->validate($item);

        return $result->isSuccess() ? true : (string) $result->errors;
    }

    /**
     * Validates the structured field Item.
     */
    public function validate(Item|Stringable|string $item): Result
    {
        $violations = new ViolationList();
        if (!$item instanceof Item) {
            try {
                $item = Item::fromHttpValue($item);
            } catch (SyntaxError $exception) {
                $violations->add(ErrorCode::ItemFailedParsing->value, new Violation('The item string could not be parsed.', previous: $exception));

                return Result::failed($violations);
            }
        }

        try {
            $itemValue = $item->value($this->valueConstraint);
        } catch (Violation $exception) {
            $itemValue = null;
            $violations->add(ErrorCode::ItemValueFailedValidation->value, $exception);
        }

        $validate = $this->parametersConstraint->validate($item->parameters());
        $violations->addAll($validate->errors);
        if ($violations->isNotEmpty()) {
            return Result::failed($violations);
        }

        /** @var ValidatedParameters $validatedParameters */
        $validatedParameters = $validate->data;

        return Result::success(new ValidatedItem($itemValue, $validatedParameters));
    }
}