1 | <?php declare(strict_types=1); |
||
2 | /** |
||
3 | * This file is part of the daikon-cqrs/validize project. |
||
4 | * |
||
5 | * For the full copyright and license information, please view the LICENSE |
||
6 | * file that was distributed with this source code. |
||
7 | */ |
||
8 | |||
9 | namespace Daikon\Validize\Validation; |
||
10 | |||
11 | use Daikon\DataStructure\TypedList; |
||
12 | use Daikon\Validize\ValueObject\Severity; |
||
13 | |||
14 | final class ValidationReport extends TypedList |
||
15 | { |
||
16 | public function __construct(iterable $incidents = []) |
||
17 | { |
||
18 | $this->init($incidents, [ValidationIncident::class]); |
||
19 | } |
||
20 | |||
21 | public function getIncident(string $path): ?ValidationIncident |
||
22 | { |
||
23 | $index = $this->search( |
||
24 | fn(ValidationIncident $incident): bool => $incident->getValidatorDefinition()->getPath() === $path |
||
25 | ); |
||
26 | return $index !== false ? $this->get($index) : null; |
||
0 ignored issues
–
show
introduced
by
![]() |
|||
27 | } |
||
28 | |||
29 | public function getIncidents(Severity $severity): self |
||
30 | { |
||
31 | return $this->filter( |
||
32 | fn(ValidationIncident $incident): bool => $incident->getSeverity()->equals($severity) |
||
33 | ); |
||
34 | } |
||
35 | |||
36 | public function getErrors(): self |
||
37 | { |
||
38 | return $this->filter( |
||
39 | fn(ValidationIncident $incident): bool => $incident->getSeverity()->isGreaterThanOrEqual(Severity::error()) |
||
40 | ); |
||
41 | } |
||
42 | |||
43 | public function getMessages(): array |
||
44 | { |
||
45 | $messages = []; |
||
46 | /** @var ValidationIncident $incident */ |
||
47 | foreach ($this as $incident) { |
||
48 | $messages[$incident->getValidatorDefinition()->getPath()] = $incident->getMessages(); |
||
49 | } |
||
50 | return $messages; |
||
51 | } |
||
52 | |||
53 | public function getStatusCode(): ?int |
||
54 | { |
||
55 | //returns first seen status |
||
56 | return $this->reduce( |
||
57 | function (?int $status, ValidationIncident $incident): ?int { |
||
58 | return $status ?? ($incident->getValidatorDefinition()->getSettings()['status'] ?? null); |
||
59 | } |
||
60 | ); |
||
61 | } |
||
62 | |||
63 | public function isProvided(string $depends): bool |
||
64 | { |
||
65 | //@todo handle array $depends |
||
66 | return $this->reduce( |
||
67 | function (bool $carry, ValidationIncident $incident) use ($depends): bool { |
||
68 | $provided = (array)($incident->getValidatorDefinition()->getSettings()['provides'] ?? []); |
||
69 | return $carry || ($incident->getSeverity()->isSuccess() && in_array($depends, $provided)); |
||
70 | }, |
||
71 | false |
||
72 | ); |
||
73 | } |
||
74 | } |
||
75 |