77use KaririCode \Contract \Processor \Attribute \CustomizableMessageAttribute ;
88use KaririCode \Contract \Processor \Attribute \ProcessableAttribute ;
99use KaririCode \Contract \Processor \ProcessorBuilder ;
10- use KaririCode \ProcessorPipeline \Exception \ProcessingException ;
1110use KaririCode \PropertyInspector \Contract \PropertyAttributeHandler ;
1211use KaririCode \PropertyInspector \Contract \PropertyChangeApplier ;
12+ use KaririCode \PropertyInspector \Processor \ProcessorConfigBuilder ;
1313use KaririCode \PropertyInspector \Utility \PropertyAccessor ;
1414
1515class AttributeHandler implements PropertyAttributeHandler, PropertyChangeApplier
1616{
1717 private array $ processedValues = [];
1818 private array $ processingErrors = [];
19+ private array $ processingMessages = [];
1920
2021 public function __construct (
2122 private readonly string $ processorType ,
2223 private readonly ProcessorBuilder $ builder ,
24+ private readonly ProcessorConfigBuilder $ configBuilder = new ProcessorConfigBuilder ()
2325 ) {
2426 }
2527
@@ -29,38 +31,65 @@ public function handleAttribute(string $propertyName, object $attribute, mixed $
2931 return null ;
3032 }
3133
32- $ processors = $ attribute ->getProcessors ();
34+ $ processorsConfig = $ this ->configBuilder ->build ($ attribute );
35+ $ messages = $ this ->extractCustomMessages ($ attribute , $ processorsConfig );
36+
37+ try {
38+ $ processedValue = $ this ->processValue ($ value , $ processorsConfig );
39+ $ this ->storeProcessedValue ($ propertyName , $ processedValue , $ messages );
40+
41+ return $ processedValue ; // Return the processed value, not the original
42+ } catch (\Exception $ e ) {
43+ $ this ->storeProcessingError ($ propertyName , $ e ->getMessage ());
44+
45+ return $ value ;
46+ }
47+ }
48+
49+ private function extractCustomMessages (ProcessableAttribute $ attribute , array &$ processorsConfig ): array
50+ {
51+ $ messages = [];
3352
3453 if ($ attribute instanceof CustomizableMessageAttribute) {
35- foreach ($ processors as $ processorName => &$ config ) {
54+ foreach ($ processorsConfig as $ processorName => &$ config ) {
3655 $ customMessage = $ attribute ->getMessage ($ processorName );
3756 if (null !== $ customMessage ) {
3857 $ config ['customMessage ' ] = $ customMessage ;
58+ $ messages [$ processorName ] = $ customMessage ;
3959 }
4060 }
4161 }
4262
43- $ pipeline = $ this ->builder ->buildPipeline ($ this ->processorType , $ processors );
63+ return $ messages ;
64+ }
4465
45- try {
46- $ processedValue = $ pipeline -> process ( $ value );
47- $ this ->processedValues [ $ propertyName ] = $ processedValue ;
66+ private function processValue ( mixed $ value , array $ processorsConfig ): mixed
67+ {
68+ $ pipeline = $ this ->builder -> buildPipeline ( $ this -> processorType , $ processorsConfig ) ;
4869
49- return $ processedValue ;
50- } catch (ProcessingException $ e ) {
51- $ this ->processingErrors [$ propertyName ][] = $ e ->getMessage ();
70+ return $ pipeline ->process ($ value );
71+ }
5272
53- return $ value ; // Return original value in case of processing error
54- }
73+ private function storeProcessedValue (string $ propertyName , mixed $ processedValue , array $ messages ): void
74+ {
75+ $ this ->processedValues [$ propertyName ] = [
76+ 'value ' => $ processedValue ,
77+ 'messages ' => $ messages ,
78+ ];
79+ $ this ->processingMessages [$ propertyName ] = $ messages ;
80+ }
81+
82+ private function storeProcessingError (string $ propertyName , string $ errorMessage ): void
83+ {
84+ $ this ->processingErrors [$ propertyName ][] = $ errorMessage ;
5585 }
5686
5787 public function applyChanges (object $ entity ): void
5888 {
59- foreach ($ this ->processedValues as $ propertyName => $ value ) {
89+ foreach ($ this ->processedValues as $ propertyName => $ data ) {
6090 $ accessor = new PropertyAccessor ($ entity , $ propertyName );
61- $ accessor ->setValue ($ value );
91+ $ accessor ->setValue ($ data [ ' value ' ] );
6292 }
63- $ this ->processedValues = []; // Clear the processed values after applying
6493 }
6594
6695 public function getProcessedValues (): array
0 commit comments