10
10
use KaririCode \PropertyInspector \Contract \PropertyAttributeHandler ;
11
11
use KaririCode \PropertyInspector \Contract \PropertyChangeApplier ;
12
12
use KaririCode \PropertyInspector \Processor \ProcessorConfigBuilder ;
13
+ use KaririCode \PropertyInspector \Processor \ProcessorValidator ;
13
14
use KaririCode \PropertyInspector \Utility \PropertyAccessor ;
14
15
15
16
class AttributeHandler implements PropertyAttributeHandler, PropertyChangeApplier
16
17
{
17
- private array $ processedValues = [];
18
- private array $ processingErrors = [];
19
- private array $ processingMessages = [];
18
+ private array $ processedPropertyValues = [];
19
+ private array $ processingResultErrors = [];
20
+ private array $ processingResultMessages = [];
20
21
21
22
public function __construct (
22
23
private readonly string $ processorType ,
23
24
private readonly ProcessorBuilder $ builder ,
25
+ private readonly ProcessorValidator $ validator = new ProcessorValidator (),
24
26
private readonly ProcessorConfigBuilder $ configBuilder = new ProcessorConfigBuilder ()
25
27
) {
26
28
}
@@ -36,20 +38,49 @@ public function handleAttribute(string $propertyName, object $attribute, mixed $
36
38
37
39
try {
38
40
$ processedValue = $ this ->processValue ($ value , $ processorsConfig );
39
- $ this ->storeProcessedValue ( $ propertyName , $ processedValue , $ messages );
41
+ $ errors = $ this ->validateProcessors ( $ processorsConfig , $ messages );
40
42
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 ;
42
50
} catch (\Exception $ e ) {
43
- $ this ->storeProcessingError ($ propertyName , $ e ->getMessage ());
51
+ $ this ->storeProcessingResultError ($ propertyName , $ e ->getMessage ());
44
52
45
53
return $ value ;
46
54
}
47
55
}
48
56
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
+
49
81
private function extractCustomMessages (ProcessableAttribute $ attribute , array &$ processorsConfig ): array
50
82
{
51
83
$ messages = [];
52
-
53
84
if ($ attribute instanceof CustomizableMessageAttribute) {
54
85
foreach ($ processorsConfig as $ processorName => &$ config ) {
55
86
$ customMessage = $ attribute ->getMessage ($ processorName );
@@ -65,45 +96,48 @@ private function extractCustomMessages(ProcessableAttribute $attribute, array &$
65
96
66
97
private function processValue (mixed $ value , array $ processorsConfig ): mixed
67
98
{
68
- $ pipeline = $ this ->builder ->buildPipeline ($ this ->processorType , $ processorsConfig );
99
+ $ pipeline = $ this ->builder ->buildPipeline (
100
+ $ this ->processorType ,
101
+ $ processorsConfig
102
+ );
69
103
70
104
return $ pipeline ->process ($ value );
71
105
}
72
106
73
- private function storeProcessedValue (string $ propertyName , mixed $ processedValue , array $ messages ): void
107
+ private function storeProcessedPropertyValue (string $ propertyName , mixed $ processedValue , array $ messages ): void
74
108
{
75
- $ this ->processedValues [$ propertyName ] = [
109
+ $ this ->processedPropertyValues [$ propertyName ] = [
76
110
'value ' => $ processedValue ,
77
111
'messages ' => $ messages ,
78
112
];
79
- $ this ->processingMessages [$ propertyName ] = $ messages ;
113
+ $ this ->processingResultMessages [$ propertyName ] = $ messages ;
80
114
}
81
115
82
- private function storeProcessingError (string $ propertyName , string $ errorMessage ): void
116
+ private function storeProcessingResultError (string $ propertyName , string $ errorMessage ): void
83
117
{
84
- $ this ->processingErrors [$ propertyName ][] = $ errorMessage ;
118
+ $ this ->processingResultErrors [$ propertyName ][] = $ errorMessage ;
85
119
}
86
120
87
121
public function applyChanges (object $ entity ): void
88
122
{
89
- foreach ($ this ->processedValues as $ propertyName => $ data ) {
123
+ foreach ($ this ->processedPropertyValues as $ propertyName => $ data ) {
90
124
$ accessor = new PropertyAccessor ($ entity , $ propertyName );
91
125
$ accessor ->setValue ($ data ['value ' ]);
92
126
}
93
127
}
94
128
95
- public function getProcessedValues (): array
129
+ public function getProcessedPropertyValues (): array
96
130
{
97
- return $ this ->processedValues ;
131
+ return $ this ->processedPropertyValues ;
98
132
}
99
133
100
- public function getProcessingErrors (): array
134
+ public function getProcessingResultErrors (): array
101
135
{
102
- return $ this ->processingErrors ;
136
+ return $ this ->processingResultErrors ;
103
137
}
104
138
105
- public function getProcessingMessages (): array
139
+ public function getProcessingResultMessages (): array
106
140
{
107
- return $ this ->processingMessages ;
141
+ return $ this ->processingResultMessages ;
108
142
}
109
143
}
0 commit comments