22
33namespace DsTrinityDataBundle \Resource \FieldTransformer \Object ;
44
5+ use DynamicSearchBundle \Exception \TransformerException ;
56use DynamicSearchBundle \Resource \Container \ResourceContainerInterface ;
67use DynamicSearchBundle \Resource \FieldTransformerInterface ;
78use Symfony \Component \OptionsResolver \OptionsResolver ;
@@ -12,14 +13,16 @@ class ObjectGetterExtractor implements FieldTransformerInterface
1213
1314 public function configureOptions (OptionsResolver $ resolver ): void
1415 {
15- $ resolver ->setRequired (['method ' , 'arguments ' , 'clean_string ' ]);
16+ $ resolver ->setRequired (['method ' , 'arguments ' , 'clean_string ' , ' transform_callback ' ]);
1617 $ resolver ->setAllowedTypes ('method ' , ['string ' ]);
1718 $ resolver ->setAllowedTypes ('arguments ' , ['array ' ]);
1819 $ resolver ->setAllowedTypes ('clean_string ' , ['boolean ' ]);
20+ $ resolver ->setAllowedTypes ('transform_callback ' , ['null ' , 'closure ' ]);
1921 $ resolver ->setDefaults ([
2022 'method ' => 'id ' ,
2123 'clean_string ' => true ,
22- 'arguments ' => []
24+ 'arguments ' => [],
25+ 'transform_callback ' => null ,
2326 ]);
2427 }
2528
@@ -28,7 +31,7 @@ public function setOptions(array $options): void
2831 $ this ->options = $ options ;
2932 }
3033
31- public function transformData (string $ dispatchTransformerName , ResourceContainerInterface $ resourceContainer ): ? string
34+ public function transformData (string $ dispatchTransformerName , ResourceContainerInterface $ resourceContainer ): null | bool | int | float | string | array
3235 {
3336 if (!$ resourceContainer ->hasAttribute ('type ' )) {
3437 return null ;
@@ -40,11 +43,18 @@ public function transformData(string $dispatchTransformerName, ResourceContainer
4043 }
4144
4245 $ value = call_user_func_array ([$ data , $ this ->options ['method ' ]], $ this ->options ['arguments ' ]);
43- if (!is_string ($ value )) {
44- return null ;
46+
47+ if (is_callable ($ this ->options ['transform_callback ' ])) {
48+ try {
49+ $ value = $ this ->options ['transform_callback ' ]($ value );
50+ } catch (\Throwable $ e ) {
51+ throw new TransformerException (
52+ sprintf ('error while executing transform_callback: %s ' , $ e ->getMessage ())
53+ );
54+ }
4555 }
46-
47- if ($ this ->options ['clean_string ' ] === true ) {
56+
57+ if (is_string ( $ value ) && $ this ->options ['clean_string ' ] === true ) {
4858 return trim (preg_replace ('/\s+/ ' , ' ' , strip_tags ($ value )));
4959 }
5060
0 commit comments