@@ -285,11 +285,17 @@ public function getParamExample(array $param): string
285285 $ output .= $ example ;
286286 break ;
287287 case self ::TYPE_OBJECT :
288- $ output .= ($ example === '{} ' )
289- ? '[object] '
290- : (($ formatted = json_encode (json_decode ($ example , true ), JSON_PRETTY_PRINT ))
291- ? 'new ' . preg_replace ('/\n/ ' , "\n " , $ formatted )
292- : 'new ' . $ example );
288+ if ($ example === '{} ' ) {
289+ $ output .= '[object] ' ;
290+ } else {
291+ $ decoded = json_decode ($ example , true );
292+ if ($ decoded && is_array ($ decoded )) {
293+ $ csharpObject = $ this ->formatCSharpAnonymousObject ($ decoded , 1 );
294+ $ output .= 'new ' . $ csharpObject ;
295+ } else {
296+ $ output .= 'new ' . $ example ;
297+ }
298+ }
293299 break ;
294300 case self ::TYPE_BOOLEAN :
295301 $ output .= ($ example ) ? 'true ' : 'false ' ;
@@ -458,4 +464,48 @@ public function getFilters(): array
458464 }),
459465 ];
460466 }
467+
468+ /**
469+ * Format a PHP array as a C# anonymous object
470+ */
471+ private function formatCSharpAnonymousObject (array $ data , int $ indentLevel = 0 ): string
472+ {
473+ $ propertyIndent = str_repeat (' ' , $ indentLevel + 1 );
474+ $ properties = [];
475+
476+ foreach ($ data as $ key => $ value ) {
477+ $ formattedValue = $ this ->formatCSharpValue ($ value , $ indentLevel + 2 );
478+ $ properties [] = $ propertyIndent . $ key . ' = ' . $ formattedValue ;
479+ }
480+
481+ if (count ($ properties ) === 1 ) {
482+ return '{ ' . trim ($ properties [0 ]) . ' } ' ;
483+ }
484+
485+ $ baseIndent = str_repeat (' ' , $ indentLevel );
486+ return "{ \n" . implode (", \n" , $ properties ) . "\n" . $ baseIndent . "} " ;
487+ }
488+
489+ /**
490+ * Format a value for C# anonymous object property
491+ */
492+ private function formatCSharpValue ($ value , int $ indentLevel = 0 ): string
493+ {
494+ if (is_string ($ value )) {
495+ return '" ' . $ value . '" ' ;
496+ } elseif (is_bool ($ value )) {
497+ return $ value ? 'true ' : 'false ' ;
498+ } elseif (is_numeric ($ value )) {
499+ return (string ) $ value ;
500+ } elseif (is_array ($ value )) {
501+ if (array_keys ($ value ) !== range (0 , count ($ value ) - 1 )) {
502+ return $ this ->formatCSharpAnonymousObject ($ value , $ indentLevel );
503+ } else {
504+ $ items = array_map (fn ($ item ) => $ this ->formatCSharpValue ($ item , $ indentLevel ), $ value );
505+ return 'new[] { ' . implode (', ' , $ items ) . ' } ' ;
506+ }
507+ } else {
508+ return 'null ' ;
509+ }
510+ }
461511}
0 commit comments