@@ -82,6 +82,13 @@ public function initializeArguments()
82
82
'string ' ,
83
83
'A date() format for DateTime values to JSON-compatible values. NULL means JS UNIXTIME (time()*1000) '
84
84
);
85
+ $ this ->registerArgument (
86
+ 'prettyPrint ' ,
87
+ 'boolean ' ,
88
+ 'If TRUE the JSON-output will be in pretty-print ' ,
89
+ false ,
90
+ false
91
+ );
85
92
}
86
93
87
94
/**
@@ -97,8 +104,9 @@ public static function renderStatic(array $arguments, \Closure $renderChildrenCl
97
104
$ preventRecursion = (boolean ) $ arguments ['preventRecursion ' ];
98
105
$ recursionMarker = $ arguments ['recursionMarker ' ];
99
106
$ dateTimeFormat = $ arguments ['dateTimeFormat ' ];
107
+ $ prettyPrint = (boolean ) $ arguments ['prettyPrint ' ];
100
108
static ::$ encounteredClasses = [];
101
- $ json = static ::encodeValue ($ value , $ useTraversableKeys , $ preventRecursion , $ recursionMarker , $ dateTimeFormat );
109
+ $ json = static ::encodeValue ($ value , $ useTraversableKeys , $ preventRecursion , $ recursionMarker , $ dateTimeFormat, $ prettyPrint );
102
110
return $ json ;
103
111
}
104
112
@@ -108,10 +116,11 @@ public static function renderStatic(array $arguments, \Closure $renderChildrenCl
108
116
* @param boolean $preventRecursion
109
117
* @param string $recursionMarker
110
118
* @param string $dateTimeFormat
119
+ * @param boolean $prettyPrint
111
120
* @return string
112
121
* @throws Exception
113
122
*/
114
- protected static function encodeValue ($ value , $ useTraversableKeys , $ preventRecursion , $ recursionMarker , $ dateTimeFormat )
123
+ protected static function encodeValue ($ value , $ useTraversableKeys , $ preventRecursion , $ recursionMarker , $ dateTimeFormat, $ prettyPrint )
115
124
{
116
125
if (true === $ value instanceof \Traversable) {
117
126
// Note: also converts ObjectStorage to \Vendor\Extname\Domain\Model\ObjectType[] which are each converted
@@ -128,7 +137,11 @@ protected static function encodeValue($value, $useTraversableKeys, $preventRecur
128
137
$ value = static ::recursiveArrayOfDomainObjectsToArray ($ value , $ preventRecursion , $ recursionMarker );
129
138
$ value = static ::recursiveDateTimeToUnixtimeMiliseconds ($ value , $ dateTimeFormat );
130
139
}
131
- $ json = json_encode ($ value , JSON_HEX_AMP | JSON_HEX_QUOT | JSON_HEX_APOS | JSON_HEX_TAG );
140
+ $ encodeOptions = JSON_HEX_AMP | JSON_HEX_QUOT | JSON_HEX_APOS | JSON_HEX_TAG ;
141
+ if ($ prettyPrint ) {
142
+ $ encodeOptions |= JSON_PRETTY_PRINT ;
143
+ }
144
+ $ json = json_encode ($ value , $ encodeOptions );
132
145
if (JSON_ERROR_NONE !== json_last_error ()) {
133
146
ErrorUtility::throwViewHelperException ('The provided argument cannot be converted into JSON. ' , 1358440181 );
134
147
}
0 commit comments