Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 0 additions & 38 deletions features/formatter.feature
Original file line number Diff line number Diff line change
Expand Up @@ -153,41 +153,3 @@ Feature: Format output
| [33mgaa/gaa-log[0m | * | [32m✔[0m |
| [33mgaa/gaa-nonsense[0m | v3.0.11 | [31m🛇[0m |
| [33mgaa/gaa-100%new[0m | v100%new | [32m✔[0m |

Scenario: Format data with boolean value
Given an empty directory
And a file.php file:
"""
<?php
$items = array(
array(
'id' => 1,
'status' => true,
'object' => new stdClass(),
'number' => 10,
'string' => 'foo',
),
array(
'id' => 2,
'status' => false,
'object' => new stdClass(),
'number' => 20,
'string' => 'bar',
),
);
$iterator = \WP_CLI\Utils\iterator_map(
$items,
function (array $item) {
return $item;
}
);
$assoc_args = array( 'format' => 'table' );
$formatter = new WP_CLI\Formatter( $assoc_args, array( 'id', 'status', 'object', 'number', 'string' ) );
$formatter->display_items($iterator);
"""

When I run `wp eval-file file.php --skip-wordpress`
Then STDOUT should be a table containing rows:
| id | status | object | number | string |
| 1 | true | {} | 10 | foo |
| 2 | false | {} | 20 | bar |
18 changes: 1 addition & 17 deletions php/WP_CLI/Formatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,7 @@ private function assoc_array_to_rows( $fields ) {
}

/**
* Transforms into shell-friendly output, as necessary
* Transforms objects and arrays to JSON as necessary
*
* @param mixed $item
* @return mixed
Expand All @@ -356,28 +356,12 @@ public function transform_item_values_to_json( $item ) {
foreach ( $this->args['fields'] as $field ) {
$true_field = $this->find_item_key( $item, $field );
$value = is_object( $item ) ? $item->$true_field : $item[ $true_field ];

// Transform into JSON.
if ( is_array( $value ) || is_object( $value ) ) {
if ( is_object( $item ) ) {
$item->$true_field = json_encode( $value );
} elseif ( is_array( $item ) ) {
$item[ $true_field ] = json_encode( $value );
}

continue;
}

// Transform boolean.
// See: https://github.com/wp-cli/wp-cli/issues/5542
if ( is_bool( $value ) ) {
if ( is_object( $item ) ) {
$item->$true_field = $value ? 'true' : 'false';
} elseif ( is_array( $item ) ) {
$item[ $true_field ] = $value ? 'true' : 'false';
}

continue;
}
}
return $item;
Expand Down