|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace KaririCode\ProcessorPipeline\Handler; |
| 6 | + |
| 7 | +use KaririCode\Contract\Processor\ProcessorBuilder; |
| 8 | +use KaririCode\Contract\Processor\ValidatableProcessor; |
| 9 | +use KaririCode\ProcessorPipeline\Result\ProcessedData; |
| 10 | +use KaririCode\ProcessorPipeline\Result\ProcessingError; |
| 11 | +use KaririCode\ProcessorPipeline\Result\ProcessingResultCollection; |
| 12 | +use KaririCode\PropertyInspector\AttributeHandler; |
| 13 | + |
| 14 | +class ProcessorAttributeHandler extends AttributeHandler |
| 15 | +{ |
| 16 | + protected ProcessingResultCollection $results; |
| 17 | + |
| 18 | + public function __construct( |
| 19 | + private readonly string $identifier, |
| 20 | + private readonly ProcessorBuilder $builder |
| 21 | + ) { |
| 22 | + parent::__construct($identifier, $builder); |
| 23 | + $this->results = new ProcessingResultCollection(); |
| 24 | + } |
| 25 | + |
| 26 | + public function processPropertyValue(string $property, mixed $value): mixed |
| 27 | + { |
| 28 | + $pipeline = $this->builder->buildPipeline( |
| 29 | + $this->identifier, |
| 30 | + $this->getPropertyProcessors($property) |
| 31 | + ); |
| 32 | + |
| 33 | + try { |
| 34 | + $processedValue = $pipeline->process($value); |
| 35 | + $this->storeProcessedValue($property, $processedValue); |
| 36 | + |
| 37 | + // Verifica se há erros de validação |
| 38 | + $this->checkValidationErrors($property, $pipeline); |
| 39 | + |
| 40 | + return $processedValue; |
| 41 | + } catch (\Exception $e) { |
| 42 | + $this->storeProcessingError($property, $e); |
| 43 | + |
| 44 | + return $value; |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + protected function checkValidationErrors(string $property, $pipeline): void |
| 49 | + { |
| 50 | + foreach ($pipeline->getProcessors() as $processor) { |
| 51 | + if ($processor instanceof ValidatableProcessor && !$processor->isValid()) { |
| 52 | + $this->storeValidationError( |
| 53 | + $property, |
| 54 | + $processor->getErrorKey(), |
| 55 | + $processor->getErrorMessage() |
| 56 | + ); |
| 57 | + } |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + protected function storeProcessedValue(string $property, mixed $value): void |
| 62 | + { |
| 63 | + $processedData = new ProcessedData($property, $value); |
| 64 | + $this->results->addProcessedData($processedData); |
| 65 | + } |
| 66 | + |
| 67 | + protected function storeProcessingError(string $property, \Exception $exception): void |
| 68 | + { |
| 69 | + $error = new ProcessingError( |
| 70 | + $property, |
| 71 | + 'processingError', |
| 72 | + $exception->getMessage() |
| 73 | + ); |
| 74 | + $this->results->addError($error); |
| 75 | + } |
| 76 | + |
| 77 | + protected function storeValidationError(string $property, string $errorKey, string $message): void |
| 78 | + { |
| 79 | + $error = new ProcessingError($property, $errorKey, $message); |
| 80 | + $this->results->addError($error); |
| 81 | + } |
| 82 | + |
| 83 | + public function getProcessingResults(): ProcessingResultCollection |
| 84 | + { |
| 85 | + return $this->results; |
| 86 | + } |
| 87 | + |
| 88 | + public function getProcessedPropertyValues(): array |
| 89 | + { |
| 90 | + return $this->results->getProcessedDataAsArray(); |
| 91 | + } |
| 92 | + |
| 93 | + public function getProcessingResultErrors(): array |
| 94 | + { |
| 95 | + return $this->results->getErrorsAsArray(); |
| 96 | + } |
| 97 | + |
| 98 | + public function reset(): void |
| 99 | + { |
| 100 | + $this->results = new ProcessingResultCollection(); |
| 101 | + } |
| 102 | +} |
0 commit comments