1010use KaririCode \PropertyInspector \Contract \PropertyAttributeHandler ;
1111use KaririCode \PropertyInspector \Contract \PropertyChangeApplier ;
1212use KaririCode \PropertyInspector \Processor \ProcessorConfigBuilder ;
13+ use KaririCode \PropertyInspector \Processor \ProcessorValidator ;
1314use KaririCode \PropertyInspector \Utility \PropertyAccessor ;
1415
1516class AttributeHandler implements PropertyAttributeHandler, PropertyChangeApplier
1617{
17- private array $ processedValues = [];
18- private array $ processingErrors = [];
19- private array $ processingMessages = [];
18+ private array $ processedPropertyValues = [];
19+ private array $ processingResultErrors = [];
20+ private array $ processingResultMessages = [];
2021
2122 public function __construct (
2223 private readonly string $ processorType ,
2324 private readonly ProcessorBuilder $ builder ,
25+ private readonly ProcessorValidator $ validator = new ProcessorValidator (),
2426 private readonly ProcessorConfigBuilder $ configBuilder = new ProcessorConfigBuilder ()
2527 ) {
2628 }
@@ -36,20 +38,49 @@ public function handleAttribute(string $propertyName, object $attribute, mixed $
3638
3739 try {
3840 $ processedValue = $ this ->processValue ($ value , $ processorsConfig );
39- $ this ->storeProcessedValue ( $ propertyName , $ processedValue , $ messages );
41+ $ errors = $ this ->validateProcessors ( $ processorsConfig , $ messages );
4042
41- return $ processedValue ; // Return the processed value, not the original
43+ $ this ->storeProcessedPropertyValue ($ propertyName , $ processedValue , $ messages );
44+
45+ if (!empty ($ errors )) {
46+ $ this ->storeProcessingResultErrors ($ propertyName , $ errors );
47+ }
48+
49+ return $ processedValue ;
4250 } catch (\Exception $ e ) {
43- $ this ->storeProcessingError ($ propertyName , $ e ->getMessage ());
51+ $ this ->storeProcessingResultError ($ propertyName , $ e ->getMessage ());
4452
4553 return $ value ;
4654 }
4755 }
4856
57+ private function validateProcessors (array $ processorsConfig , array $ messages ): array
58+ {
59+ $ errors = [];
60+ foreach ($ processorsConfig as $ processorName => $ config ) {
61+ $ processor = $ this ->builder ->build ($ this ->processorType , $ processorName , $ config );
62+ $ validationError = $ this ->validator ->validate (
63+ $ processor ,
64+ $ processorName ,
65+ $ messages
66+ );
67+
68+ if (null !== $ validationError ) {
69+ $ errors [$ processorName ] = $ validationError ;
70+ }
71+ }
72+
73+ return $ errors ;
74+ }
75+
76+ private function storeProcessingResultErrors (string $ propertyName , array $ errors ): void
77+ {
78+ $ this ->processingResultErrors [$ propertyName ] = $ errors ;
79+ }
80+
4981 private function extractCustomMessages (ProcessableAttribute $ attribute , array &$ processorsConfig ): array
5082 {
5183 $ messages = [];
52-
5384 if ($ attribute instanceof CustomizableMessageAttribute) {
5485 foreach ($ processorsConfig as $ processorName => &$ config ) {
5586 $ customMessage = $ attribute ->getMessage ($ processorName );
@@ -65,45 +96,48 @@ private function extractCustomMessages(ProcessableAttribute $attribute, array &$
6596
6697 private function processValue (mixed $ value , array $ processorsConfig ): mixed
6798 {
68- $ pipeline = $ this ->builder ->buildPipeline ($ this ->processorType , $ processorsConfig );
99+ $ pipeline = $ this ->builder ->buildPipeline (
100+ $ this ->processorType ,
101+ $ processorsConfig
102+ );
69103
70104 return $ pipeline ->process ($ value );
71105 }
72106
73- private function storeProcessedValue (string $ propertyName , mixed $ processedValue , array $ messages ): void
107+ private function storeProcessedPropertyValue (string $ propertyName , mixed $ processedValue , array $ messages ): void
74108 {
75- $ this ->processedValues [$ propertyName ] = [
109+ $ this ->processedPropertyValues [$ propertyName ] = [
76110 'value ' => $ processedValue ,
77111 'messages ' => $ messages ,
78112 ];
79- $ this ->processingMessages [$ propertyName ] = $ messages ;
113+ $ this ->processingResultMessages [$ propertyName ] = $ messages ;
80114 }
81115
82- private function storeProcessingError (string $ propertyName , string $ errorMessage ): void
116+ private function storeProcessingResultError (string $ propertyName , string $ errorMessage ): void
83117 {
84- $ this ->processingErrors [$ propertyName ][] = $ errorMessage ;
118+ $ this ->processingResultErrors [$ propertyName ][] = $ errorMessage ;
85119 }
86120
87121 public function applyChanges (object $ entity ): void
88122 {
89- foreach ($ this ->processedValues as $ propertyName => $ data ) {
123+ foreach ($ this ->processedPropertyValues as $ propertyName => $ data ) {
90124 $ accessor = new PropertyAccessor ($ entity , $ propertyName );
91125 $ accessor ->setValue ($ data ['value ' ]);
92126 }
93127 }
94128
95- public function getProcessedValues (): array
129+ public function getProcessedPropertyValues (): array
96130 {
97- return $ this ->processedValues ;
131+ return $ this ->processedPropertyValues ;
98132 }
99133
100- public function getProcessingErrors (): array
134+ public function getProcessingResultErrors (): array
101135 {
102- return $ this ->processingErrors ;
136+ return $ this ->processingResultErrors ;
103137 }
104138
105- public function getProcessingMessages (): array
139+ public function getProcessingResultMessages (): array
106140 {
107- return $ this ->processingMessages ;
141+ return $ this ->processingResultMessages ;
108142 }
109143}
0 commit comments