php/WP_CLI/Formatter.php
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':
				\WP_CLI\Utils\write_csv( STDOUT, $items, $fields );
				break;

			case 'json':
			case 'yaml':
				$out = array();
				foreach ( $items as $item ) {
					$out[] = \WP_CLI\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'] );
		}
	}
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':
				\WP_CLI\Utils\write_csv( STDOUT, $items, $fields );
				break;

			case 'json':
			case 'yaml':
				$out = array();
				foreach ( $items as $item ) {
					$out[] = \WP_CLI\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'] );
		}
	}
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' : \ WP_CLI \ Utils \ write_csv ( STDOUT , $ items , $ fields ) ; break ; case 'json' : case 'yaml' : $ out = array ( ) ; foreach ( $ items as $ item ) { $ out [ ] = \ WP_CLI \ 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' ] ) ; } }
Format items according to arguments.

@param array      $items
@param bool|array $ascii_pre_colorized Optional. A boolean or an array of booleans to pass to `show_table()` if items in the table are pre-colorized. Default false.
Format items according to arguments .