WP_CLI
Formatter::format()
Format items according to arguments.
Method of the class: Formatter{}
No Hooks.
Return
null
. Nothing (null).
Usage
// private - for code of main (parent) class only $result = $this->format( $items, $ascii_pre_colorized );
- $items(array) (required)
- -
- $ascii_pre_colorized(true|false|array)
- A boolean or an array of booleans to pass to show_table() if items in the table are pre-colorized.
Default: false
Formatter::format() Formatter::format code WP-CLI 2.8.0-alpha
private function format( $items, $ascii_pre_colorized = false ) { $fields = $this->args['fields']; switch ( $this->args['format'] ) { case 'count': if ( ! is_array( $items ) ) { $items = iterator_to_array( $items ); } echo count( $items ); break; case 'ids': if ( ! is_array( $items ) ) { $items = iterator_to_array( $items ); } echo implode( ' ', $items ); break; case 'table': self::show_table( $items, $fields, $ascii_pre_colorized ); break; case 'csv': Utils\write_csv( STDOUT, $items, $fields ); break; case 'json': case 'yaml': $out = []; foreach ( $items as $item ) { $out[] = Utils\pick_fields( $item, $fields ); } if ( 'json' === $this->args['format'] ) { if ( defined( 'JSON_PARTIAL_OUTPUT_ON_ERROR' ) ) { // phpcs:ignore PHPCompatibility.Constants.NewConstants.json_partial_output_on_errorFound echo json_encode( $out, JSON_PARTIAL_OUTPUT_ON_ERROR ); } else { echo json_encode( $out ); } } elseif ( 'yaml' === $this->args['format'] ) { echo Spyc::YAMLDump( $out, 2, 0 ); } break; default: WP_CLI::error( 'Invalid format: ' . $this->args['format'] ); } }