|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace KaririCode\ProcessorPipeline\Handler; |
| 6 | + |
| 7 | +use KaririCode\Contract\Processor\Attribute\CustomizableMessageAttribute; |
| 8 | +use KaririCode\Contract\Processor\Attribute\ProcessableAttribute; |
| 9 | +use KaririCode\Contract\Processor\ProcessorBuilder; |
| 10 | +use KaririCode\Contract\Processor\ProcessorValidator as ProcessorProcessorContract; |
| 11 | +use KaririCode\ProcessorPipeline\Contract\ProcessorConfigBuilder as ProcessorConfigBuilderContract; |
| 12 | +use KaririCode\ProcessorPipeline\Processor\ProcessorConfigBuilder; |
| 13 | +use KaririCode\ProcessorPipeline\Processor\ProcessorValidator; |
| 14 | +use KaririCode\PropertyInspector\Contract\PropertyAttributeHandler; |
| 15 | +use KaririCode\PropertyInspector\Contract\PropertyChangeApplier; |
| 16 | +use KaririCode\PropertyInspector\Utility\PropertyAccessor; |
| 17 | + |
| 18 | +class AttributeHandler implements PropertyAttributeHandler, PropertyChangeApplier |
| 19 | +{ |
| 20 | + private array $processedPropertyValues = []; |
| 21 | + private array $processingResultErrors = []; |
| 22 | + private array $processingResultMessages = []; |
| 23 | + private array $processorCache = []; |
| 24 | + |
| 25 | + public function __construct( |
| 26 | + private readonly string $processorType, |
| 27 | + private readonly ProcessorBuilder $builder, |
| 28 | + private readonly ProcessorProcessorContract $validator = new ProcessorValidator(), |
| 29 | + private readonly ProcessorConfigBuilderContract $configBuilder = new ProcessorConfigBuilder() |
| 30 | + ) { |
| 31 | + } |
| 32 | + |
| 33 | + public function handleAttribute(string $propertyName, object $attribute, mixed $value): mixed |
| 34 | + { |
| 35 | + if (!$attribute instanceof ProcessableAttribute) { |
| 36 | + return null; |
| 37 | + } |
| 38 | + |
| 39 | + try { |
| 40 | + return $this->processAttribute($propertyName, $attribute, $value); |
| 41 | + } catch (\Exception $e) { |
| 42 | + $this->processingResultErrors[$propertyName][] = $e->getMessage(); |
| 43 | + |
| 44 | + return $value; |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + private function processAttribute(string $propertyName, ProcessableAttribute $attribute, mixed $value): mixed |
| 49 | + { |
| 50 | + $config = $this->configBuilder->build($attribute); |
| 51 | + $messages = []; |
| 52 | + |
| 53 | + if ($attribute instanceof CustomizableMessageAttribute) { |
| 54 | + foreach ($config as $processorName => &$processorConfig) { |
| 55 | + if ($message = $attribute->getMessage($processorName)) { |
| 56 | + $processorConfig['customMessage'] = $message; |
| 57 | + $messages[$processorName] = $message; |
| 58 | + } |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + $processedValue = $this->processValue($value, $config); |
| 63 | + |
| 64 | + if ($errors = $this->validateProcessors($config, $messages)) { |
| 65 | + $this->processingResultErrors[$propertyName] = $errors; |
| 66 | + } |
| 67 | + |
| 68 | + $this->processedPropertyValues[$propertyName] = [ |
| 69 | + 'value' => $processedValue, |
| 70 | + 'messages' => $messages, |
| 71 | + ]; |
| 72 | + |
| 73 | + $this->processingResultMessages[$propertyName] = $messages; |
| 74 | + |
| 75 | + return $processedValue; |
| 76 | + } |
| 77 | + |
| 78 | + private function validateProcessors(array $processorsConfig, array $messages): array |
| 79 | + { |
| 80 | + $errors = []; |
| 81 | + foreach ($processorsConfig as $processorName => $config) { |
| 82 | + // Simplify cache key to processor name |
| 83 | + if (!isset($this->processorCache[$processorName])) { |
| 84 | + $this->processorCache[$processorName] = $this->builder->build( |
| 85 | + $this->processorType, |
| 86 | + $processorName, |
| 87 | + $config |
| 88 | + ); |
| 89 | + } |
| 90 | + |
| 91 | + $processor = $this->processorCache[$processorName]; |
| 92 | + |
| 93 | + if ($error = $this->validator->validate($processor, $processorName, $messages)) { |
| 94 | + $errors[$processorName] = $error; |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + return $errors; |
| 99 | + } |
| 100 | + |
| 101 | + private function processValue(mixed $value, array $config): mixed |
| 102 | + { |
| 103 | + return $this->builder |
| 104 | + ->buildPipeline($this->processorType, $config) |
| 105 | + ->process($value); |
| 106 | + } |
| 107 | + |
| 108 | + public function applyChanges(object $entity): void |
| 109 | + { |
| 110 | + foreach ($this->processedPropertyValues as $propertyName => $data) { |
| 111 | + (new PropertyAccessor($entity, $propertyName))->setValue($data['value']); |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + public function getProcessedPropertyValues(): array |
| 116 | + { |
| 117 | + return $this->processedPropertyValues; |
| 118 | + } |
| 119 | + |
| 120 | + public function getProcessingResultErrors(): array |
| 121 | + { |
| 122 | + return $this->processingResultErrors; |
| 123 | + } |
| 124 | + |
| 125 | + public function getProcessingResultMessages(): array |
| 126 | + { |
| 127 | + return $this->processingResultMessages; |
| 128 | + } |
| 129 | +} |
0 commit comments