81a9cfa27bb3db3ee80169d24a115cda695cfbac
[yaffs-website] / vendor / drush / drush / commands / core / outputformat / table.inc
1 <?php
2
3 /**
4  * Output formatter 'table'
5  *
6  * @param $data
7  *   The $data parameter is expected to be an array (keys ignored) of
8  *   rows; each row, in turn, is an array of key / value pairs.  Every
9  *   row is expected to have the same set of keys.  The data is rendered
10  *   as a formatted word-wrapped table with rows of data cells aligned in
11  *   columns.
12  * @param $metadata
13  *   'field-labels' - If present, contains an array of key / value pairs
14  *     that map from the keys in the row columns to the label for the
15  *     column header.
16  *   'column-widths' - If present, contains an array of key / value pairs,
17  *     where the key is the integer column number, and the value is the
18  *     width that column should be formatted to.
19  *
20  * Code:
21  *
22  *   return array(
23  *     "a" => array("b" => 2, "c" => 3),
24  *     "d" => array("b" => 5, "c" => 6)
25  *   );
26  *
27  * Output with --format=table:
28  *
29  *    b  c
30  *    2  3
31  *    5  6
32  */
33 class drush_outputformat_table extends drush_outputformat {
34   function format($input, $metadata) {
35     $field_list = isset($metadata['field-labels']) ? $metadata['field-labels'] : array();
36     $widths = array();
37     $col = 0;
38     foreach($field_list as $key => $label) {
39       if (isset($metadata['column-widths'][$key])) {
40         $widths[$col] = $metadata['column-widths'][$key];
41       }
42       ++$col;
43     }
44     $rows = drush_rows_of_key_value_to_array_table($input, $field_list, $metadata);
45     $field_labels = array_key_exists('include-field-labels', $metadata) && $metadata['include-field-labels'];
46     if (!$field_labels) {
47       array_shift($rows);
48     }
49     return drush_format_table($rows, $field_labels, $widths);
50   }
51 }