private function format($items)
{
$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);
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']) {
echo json_encode($out);
} else {
if ('yaml' === $this->args['format']) {
echo \Spyc::YAMLDump($out, 2, 0);
}
}
break;
default:
\WP_CLI::error('Invalid format: ' . $this->args['format']);
}
}