-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProcessorAttributeHandler.php
102 lines (84 loc) · 3.03 KB
/
ProcessorAttributeHandler.php
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
<?php
declare(strict_types=1);
namespace KaririCode\ProcessorPipeline\Handler;
use KaririCode\Contract\Processor\ProcessorBuilder;
use KaririCode\Contract\Processor\ValidatableProcessor;
use KaririCode\ProcessorPipeline\Result\ProcessedData;
use KaririCode\ProcessorPipeline\Result\ProcessingError;
use KaririCode\ProcessorPipeline\Result\ProcessingResultCollection;
use KaririCode\PropertyInspector\AttributeHandler;
class ProcessorAttributeHandler extends AttributeHandler
{
protected ProcessingResultCollection $results;
public function __construct(
private readonly string $identifier,
private readonly ProcessorBuilder $builder
) {
parent::__construct($identifier, $builder);
$this->results = new ProcessingResultCollection();
}
public function processPropertyValue(string $property, mixed $value): mixed
{
$pipeline = $this->builder->buildPipeline(
$this->identifier,
$this->getPropertyProcessors($property)
);
try {
$processedValue = $pipeline->process($value);
$this->storeProcessedValue($property, $processedValue);
// Verifica se há erros de validação
$this->checkValidationErrors($property, $pipeline);
return $processedValue;
} catch (\Exception $e) {
$this->storeProcessingError($property, $e);
return $value;
}
}
protected function checkValidationErrors(string $property, $pipeline): void
{
foreach ($pipeline->getProcessors() as $processor) {
if ($processor instanceof ValidatableProcessor && !$processor->isValid()) {
$this->storeValidationError(
$property,
$processor->getErrorKey(),
$processor->getErrorMessage()
);
}
}
}
protected function storeProcessedValue(string $property, mixed $value): void
{
$processedData = new ProcessedData($property, $value);
$this->results->addProcessedData($processedData);
}
protected function storeProcessingError(string $property, \Exception $exception): void
{
$error = new ProcessingError(
$property,
'processingError',
$exception->getMessage()
);
$this->results->addError($error);
}
protected function storeValidationError(string $property, string $errorKey, string $message): void
{
$error = new ProcessingError($property, $errorKey, $message);
$this->results->addError($error);
}
public function getProcessingResults(): ProcessingResultCollection
{
return $this->results;
}
public function getProcessedPropertyValues(): array
{
return $this->results->getProcessedDataAsArray();
}
public function getProcessingResultErrors(): array
{
return $this->results->getErrorsAsArray();
}
public function reset(): void
{
$this->results = new ProcessingResultCollection();
}
}