WP_CLI

Formatter::show_multiple_fields()privateWP-CLI 1.0

Show multiple fields of an object.

Method of the class: Formatter{}

No Hooks.

Return

null. Nothing.

Usage

// private - for code of main (parent) class only
$result = $this->show_multiple_fields( $data, $format, $ascii_pre_colorized );
$data(object|array) (required)
Data to display
$format(string) (required)
Format to display the data in
$ascii_pre_colorized(true|false|array)
A boolean or an array of booleans to pass to show_table() if the item in the table is pre-colorized.
Default: false

Formatter::show_multiple_fields() code WP-CLI 2.8.0-alpha

private function show_multiple_fields( $data, $format, $ascii_pre_colorized = false ) {

	$true_fields = [];
	foreach ( $this->args['fields'] as $field ) {
		$true_fields[] = $this->find_item_key( $data, $field );
	}

	foreach ( $data as $key => $value ) {
		if ( ! in_array( $key, $true_fields, true ) ) {
			if ( is_array( $data ) ) {
				unset( $data[ $key ] );
			} elseif ( is_object( $data ) ) {
				unset( $data->$key );
			}
		}
	}

	switch ( $format ) {

		case 'table':
		case 'csv':
			$rows   = $this->assoc_array_to_rows( $data );
			$fields = [ 'Field', 'Value' ];
			if ( 'table' === $format ) {
				self::show_table( $rows, $fields, $ascii_pre_colorized );
			} elseif ( 'csv' === $format ) {
				Utils\write_csv( STDOUT, $rows, $fields );
			}
			break;

		case 'yaml':
		case 'json':
			WP_CLI::print_value(
				$data,
				[
					'format' => $format,
				]
			);
			break;

		default:
			WP_CLI::error( 'Invalid format: ' . $format );
			break;

	}

}