@@ -12,14 +12,16 @@ class ObjectGetterExtractor implements FieldTransformerInterface
1212
1313 public function configureOptions (OptionsResolver $ resolver ): void
1414 {
15- $ resolver ->setRequired (['method ' , 'arguments ' , 'clean_string ' ]);
15+ $ resolver ->setRequired (['method ' , 'arguments ' , 'clean_string ' , ' format ' ]);
1616 $ resolver ->setAllowedTypes ('method ' , ['string ' ]);
1717 $ resolver ->setAllowedTypes ('arguments ' , ['array ' ]);
1818 $ resolver ->setAllowedTypes ('clean_string ' , ['boolean ' ]);
19+ $ resolver ->setAllowedTypes ('format ' , ['null ' , 'string ' ]);
1920 $ resolver ->setDefaults ([
2021 'method ' => 'id ' ,
2122 'clean_string ' => true ,
22- 'arguments ' => []
23+ 'arguments ' => [],
24+ 'format ' => null
2325 ]);
2426 }
2527
@@ -28,7 +30,7 @@ public function setOptions(array $options): void
2830 $ this ->options = $ options ;
2931 }
3032
31- public function transformData (string $ dispatchTransformerName , ResourceContainerInterface $ resourceContainer ): ? string
33+ public function transformData (string $ dispatchTransformerName , ResourceContainerInterface $ resourceContainer ): null | bool | int | float | string | array
3234 {
3335 if (!$ resourceContainer ->hasAttribute ('type ' )) {
3436 return null ;
@@ -40,12 +42,30 @@ public function transformData(string $dispatchTransformerName, ResourceContainer
4042 }
4143
4244 $ value = call_user_func_array ([$ data , $ this ->options ['method ' ]], $ this ->options ['arguments ' ]);
43- if (!is_string ($ value )) {
44- return null ;
45+
46+ if ($ value instanceof \DateTimeInterface) {
47+ return $ this ->options ['format ' ] ? $ value ->format ($ this ->options ['format ' ]) : $ value ->getTimestamp ();
4548 }
46-
47- if ($ this ->options ['clean_string ' ] === true ) {
48- return trim (preg_replace ('/\s+/ ' , ' ' , strip_tags ($ value )));
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 ;
58+ }
59+ return $ value ;
60+ }
61+
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+ }
4969 }
5070
5171 return $ value ;
0 commit comments