Yaffs site version 1.1
[yaffs-website] / vendor / drush / drush / commands / core / outputformat / key_value.inc
1 <?php
2
3 /**
4  * Output formatter 'key_value'
5  *
6  * @param $data
7  *   The $data parameter contains an array of key / value pairs which
8  *   are rendered as "key   :   value" in a formatted word-wrapped table
9  *   with aligned columns.  'value' is expected to always be a simple string;
10  *   if it is not, it is rendered with var_export.
11  * @param $metadata
12  *   'label' - If present, creates a section header "[label]" prior to the data
13  *   'separator' - If present, used instead of ', ' when impoding data values
14  *   'ini-item' - If present, selects a single item from any data value that is
15  *     an array and uses it instead of imploding all values together.
16  *
17  * Code:
18  *
19  *   return array(
20  *     "b" => "Two B or ! Two B, that is the comparison",
21  *     "c" => "I see that C has gone to Sea"
22  *   );
23  *
24  * Output with --format=key-value:
25  *
26  *     b   :  Two B or ! Two B,
27  *            that is the
28  *            comparison
29  *     c   :  I see that C has gone
30  *            to Sea
31  *
32  * Code:
33  *
34  *   return array(
35  *     "a" => array(
36  *       "b" => "Two B or ! Two B, that is the comparison",
37  *       "c" => "I see that C has gone to Sea"
38  *     ),
39  *     "d" => array(
40  *       "e" => "Elephants and electron microscopes",
41  *       "f" => "My margin is too small"
42  *     )
43  *   );
44  *
45  * Output with --format=key-value-list:
46  *
47  *     b   :  Two B or ! Two B,
48  *            that is the
49  *            comparison
50  *     c   :  I see that C has gone
51  *            to Sea
52  *
53  *     e   :  Elephants and
54  *            electron microscopes
55  *     f   :  My margin is too
56  *            small
57  */
58 class drush_outputformat_key_value extends drush_outputformat {
59   function format($input, $metadata) {
60     if (!is_array($input)) {
61       if (isset($metadata['label'])) {
62         $input = array(dt($metadata['label']) => $input);
63       }
64       else {
65         return $this->format_error(dt('No label provided.'));
66       }
67     }
68     $kv_metadata = isset($metadata['table-metadata']) ? $metadata['table-metadata'] : array();
69     if ((!isset($kv_metadata['key-value-item'])) && (isset($metadata['field-labels']))) {
70       $input = drush_select_output_fields($input, $metadata['field-labels'], $metadata['field-mappings']);
71     }
72     if (isset($metadata['include-field-labels'])) {
73       $kv_metadata['include-field-labels'] = $metadata['include-field-labels'];
74     }
75     $formatted_table = drush_key_value_to_array_table($input, $kv_metadata);
76     if ($formatted_table === FALSE) {
77       return FALSE;
78     }
79     return drush_format_table($formatted_table, FALSE, array());
80   }
81 }