WP_CLI

Formatter::format()privateWP-CLI 1.0

Format items according to arguments.

Метод класса: Formatter{}

Хуков нет.

Возвращает

null. Ничего.

Использование

// private - только в коде основоного (родительского) класса
$result = $this->format( $items, $ascii_pre_colorized );
$items(массив) (обязательный)
-
$ascii_pre_colorized(true|false|массив)
A boolean or an array of booleans to pass to show_table() if items in the table are pre-colorized.
По умолчанию: false

Код Formatter::format() 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'] );
	}
}