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,16 +13,16 @@ class ObjectGetterExtractor implements FieldTransformerInterface
1213
1314 public function configureOptions (OptionsResolver $ resolver ): void
1415 {
15- $ resolver ->setRequired (['method ' , 'arguments ' , 'clean_string ' , 'format ' ]);
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 ' ]);
19- $ resolver ->setAllowedTypes ('format ' , ['null ' , 'string ' ]);
20+ $ resolver ->setAllowedTypes ('transform_callback ' , ['null ' , 'closure ' ]);
2021 $ resolver ->setDefaults ([
2122 'method ' => 'id ' ,
2223 'clean_string ' => true ,
2324 'arguments ' => [],
24- 'format ' => null
25+ 'transform_callback ' => null ,
2526 ]);
2627 }
2728
@@ -43,29 +44,18 @@ public function transformData(string $dispatchTransformerName, ResourceContainer
4344
4445 $ value = call_user_func_array ([$ data , $ this ->options ['method ' ]], $ this ->options ['arguments ' ]);
4546
46- if ($ value instanceof \DateTimeInterface) {
47- return $ this ->options ['format ' ] ? $ value ->format ($ this ->options ['format ' ]) : $ value ->getTimestamp ();
48- }
49-
50- if (is_bool ($ value )) {
51- return $ value ;
52- }
53-
54- if (is_numeric ($ value )) {
55- $ value = (float ) $ value ;
56- if (floor ($ value ) === $ value ) {
57- return (int ) $ value ;
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+ );
5854 }
59- return $ value ;
6055 }
6156
62- if (is_string ($ value )) {
63- if ($ this ->options ['format ' ] === 'nl2br ' ) {
64- return nl2br ($ value );
65- }
66- if ($ this ->options ['clean_string ' ] === true ) {
67- return trim (preg_replace ('/\s+/ ' , ' ' , strip_tags ($ value )));
68- }
57+ if (is_string ($ value ) && $ this ->options ['clean_string ' ] === true ) {
58+ return trim (preg_replace ('/\s+/ ' , ' ' , strip_tags ($ value )));
6959 }
7060
7161 return $ value ;
0 commit comments