Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for formatting boolean values in WP_CLI\Formatter #5928

Merged
merged 5 commits into from Apr 26, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
32 changes: 32 additions & 0 deletions features/formatter.feature
Expand Up @@ -153,3 +153,35 @@ Feature: Format output
| gaa/gaa-log | * | ✔ |
| gaa/gaa-nonsense | v3.0.11 | 🛇 |
| gaa/gaa-100%new | v100%new | ✔ |

Scenario: Format data with boolean value
Given an empty directory
And a file.php file:
"""
<?php
$items = array(
array(
'id' => 1,
'status' => true,
),
array(
'id' => 2,
'status' => false,
),
);
$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' ) );
$formatter->display_items($iterator);
"""

When I run `wp eval-file file.php --skip-wordpress`
Then STDOUT should be a table containing rows:
| id | status |
| 1 | true |
| 2 | false |
24 changes: 20 additions & 4 deletions php/WP_CLI/Formatter.php
Expand Up @@ -89,9 +89,9 @@ public function display_items( $items, $ascii_pre_colorized = false ) {

if ( in_array( $this->args['format'], [ 'table', 'csv' ], true ) ) {
if ( $items instanceof Iterator ) {
$items = Utils\iterator_map( $items, [ $this, 'transform_item_values_to_json' ] );
$items = Utils\iterator_map( $items, [ $this, 'transform_item_values' ] );
} else {
$items = array_map( [ $this, 'transform_item_values_to_json' ], $items );
$items = array_map( [ $this, 'transform_item_values' ], $items );
}
}

Expand Down Expand Up @@ -347,21 +347,37 @@ private function assoc_array_to_rows( $fields ) {
}

/**
* Transforms objects and arrays to JSON as necessary
* Transforms into shell-friendly output, as necessary
*
* @param mixed $item
* @return mixed
*/
public function transform_item_values_to_json( $item ) {
public function transform_item_values( $item ) {
swissspidy marked this conversation as resolved.
Show resolved Hide resolved
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 );
}

return $item;
swissspidy marked this conversation as resolved.
Show resolved Hide resolved
}

// 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';
}

return $item;
}
}
return $item;
Expand Down