Skip to content

Commit c8fb939

Browse files
committed
Use native value types for machine-readable output formats
The table format is meant for humans, so booleans and empty arrays keep their readable placeholders ([true], [false], [empty array]). For the json, csv, and yaml formats those placeholders discarded the value type, handing consumers the string "[false]" instead of a real boolean. Keep the native types for the machine-readable formats: booleans and empty arrays now serialize as-is. CSV is a flat format that cannot carry an array, so empty arrays are JSON-encoded there to avoid an "Array to string conversion" warning.
1 parent 55e0d64 commit c8fb939

1 file changed

Lines changed: 29 additions & 13 deletions

File tree

‎src/Theme_Mod_Command.php‎

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -120,8 +120,10 @@ public function get( $args, $assoc_args ) {
120120
}
121121

122122
// Take our Formatter-friendly list and adjust it according to the requested format.
123-
switch ( Utils\get_flag_value( $assoc_args, 'format' ) ) {
124-
// For tables we use a double space to indent child items.
123+
$format = Utils\get_flag_value( $assoc_args, 'format', 'table' );
124+
switch ( $format ) {
125+
// For tables we use a double space to indent child items, and render
126+
// types like booleans and empty arrays as readable placeholders.
125127
case 'table':
126128
$mod_list = array_map(
127129
static function ( $item ) use ( $separator ) {
@@ -130,24 +132,39 @@ static function ( $item ) use ( $separator ) {
130132
if ( ! empty( $parts ) ) {
131133
$new_key = str_repeat( ' ', count( $parts ) ) . $new_key;
132134
}
135+
136+
$value = $item['value'];
137+
if ( is_bool( $value ) ) {
138+
$value = $value ? '[true]' : '[false]';
139+
} elseif ( is_array( $value ) ) {
140+
$value = '[empty array]';
141+
}
142+
133143
return [
134144
'key' => $new_key,
135-
'value' => $item['value'],
145+
'value' => $value,
136146
];
137147
},
138148
$mod_list
139149
);
140150
break;
141151

142-
// For JSON, CSV, and YAML formats we use dot notation to show the hierarchy.
152+
// For JSON, CSV, and YAML formats we use dot notation to show the
153+
// hierarchy and keep the native value types. CSV is a flat format
154+
// that cannot represent an array, so those are JSON-encoded.
143155
case 'csv':
144156
case 'yaml':
145157
case 'json':
146158
$mod_list = array_map(
147-
static function ( $item ) use ( $separator ) {
159+
static function ( $item ) use ( $separator, $format ) {
160+
$value = $item['value'];
161+
if ( 'csv' === $format && is_array( $value ) ) {
162+
$value = (string) wp_json_encode( $value );
163+
}
164+
148165
return [
149166
'key' => str_replace( $separator, '.', $item['key'] ),
150-
'value' => $item['value'],
167+
'value' => $value,
151168
];
152169
},
153170
$mod_list
@@ -173,11 +190,13 @@ private function mod_to_string( $key, $value, &$mod_list, $separator ) {
173190
// Convert objects to arrays for easier handling.
174191
$value = (array) $value;
175192

176-
// Explicitly handle empty arrays to ensure they are displayed.
193+
// Explicitly handle empty arrays to ensure they are displayed. The
194+
// value is kept native here and only rendered as a placeholder for
195+
// the table format.
177196
if ( empty( $value ) ) {
178197
$mod_list[] = [
179198
'key' => $key,
180-
'value' => '[empty array]',
199+
'value' => [],
181200
];
182201
return;
183202
}
@@ -192,11 +211,8 @@ private function mod_to_string( $key, $value, &$mod_list, $separator ) {
192211
$this->mod_to_string( $key . $separator . $child_key, $child_value, $mod_list, $separator );
193212
}
194213
} else {
195-
// Explicitly handle boolean values to ensure they are displayed correctly.
196-
if ( is_bool( $value ) ) {
197-
$value = $value ? '[true]' : '[false]';
198-
}
199-
214+
// Scalars (including booleans) are kept as their native type here.
215+
// The table format renders booleans as readable placeholders later.
200216
$mod_list[] = [
201217
'key' => $key,
202218
'value' => $value,

0 commit comments

Comments
 (0)