|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace N98\Util\Console\Helper; |
|
4
|
|
|
|
|
5
|
|
|
use N98\Util\Console\Helper\Table\Renderer\RendererFactory; |
|
6
|
|
|
use N98\Util\Console\Helper\Table\Renderer\RendererInterface; |
|
7
|
|
|
use Symfony\Component\Console\Helper\Helper as AbstractHelper; |
|
8
|
|
|
use Symfony\Component\Console\Helper\Table; |
|
9
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Text Table Helper |
|
13
|
|
|
* @author Timothy Anido <[email protected]> |
|
14
|
|
|
* |
|
15
|
|
|
* Based on draw_text_table by Paul Maunders |
|
16
|
|
|
* Available at http://www.pyrosoft.co.uk/blog/2007/07/01/php-array-to-text-table-function/ |
|
17
|
|
|
*/ |
|
18
|
|
|
class TableHelper extends AbstractHelper |
|
19
|
|
|
{ |
|
20
|
|
|
/** |
|
21
|
|
|
* @var string |
|
22
|
|
|
*/ |
|
23
|
|
|
protected $format; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @var array |
|
27
|
|
|
*/ |
|
28
|
|
|
protected $headers = []; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @var array |
|
32
|
|
|
*/ |
|
33
|
|
|
protected $rows = []; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @param string $format |
|
37
|
|
|
* @return $this |
|
38
|
|
|
*/ |
|
39
|
|
|
public function setFormat($format) |
|
40
|
|
|
{ |
|
41
|
|
|
$this->format = $format; |
|
42
|
|
|
|
|
43
|
|
|
return $this; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @return string |
|
48
|
|
|
*/ |
|
49
|
|
|
public function getFormat() |
|
50
|
|
|
{ |
|
51
|
|
|
return $this->format; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @param OutputInterface $outputInterface |
|
56
|
|
|
* @param array $rows |
|
57
|
|
|
* @param string $format [optional] |
|
|
|
|
|
|
58
|
|
|
*/ |
|
59
|
|
|
public function renderByFormat(OutputInterface $outputInterface, array $rows, $format = null) |
|
60
|
|
|
{ |
|
61
|
|
|
$rendererFactory = new RendererFactory(); |
|
62
|
|
|
$renderer = $rendererFactory->create($format); |
|
63
|
|
|
if ($renderer && $renderer instanceof RendererInterface) { |
|
64
|
|
|
foreach ($rows as &$row) { |
|
65
|
|
|
$row = array_combine($this->headers, $row); |
|
66
|
|
|
} |
|
67
|
|
|
$renderer->render($outputInterface, $rows); |
|
68
|
|
|
} else { |
|
69
|
|
|
$this->setRows($rows); |
|
70
|
|
|
$this->render($outputInterface); |
|
71
|
|
|
} |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* Takes a two dimensional tabular array with headers as keys in the first row and outputs an ascii table |
|
76
|
|
|
* |
|
77
|
|
|
* @deprecated since 1.98.0 use original Symfony table instead. |
|
78
|
|
|
* |
|
79
|
|
|
* @param OutputInterface $output |
|
80
|
|
|
* @param array $rows |
|
81
|
|
|
*/ |
|
82
|
|
|
public function write(OutputInterface $output, array $rows) |
|
83
|
|
|
{ |
|
84
|
|
|
$this->setHeaders(array_keys($rows[0])); |
|
85
|
|
|
$this->setRows($rows); |
|
86
|
|
|
$this->render($output); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* @param OutputInterface $output |
|
91
|
|
|
* @param array $rows |
|
92
|
|
|
*/ |
|
93
|
|
|
public function render(OutputInterface $output, $rows = []) |
|
94
|
|
|
{ |
|
95
|
|
|
if (empty($rows)) { |
|
96
|
|
|
$rows = $this->rows; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
$baseTable = new Table($output); |
|
100
|
|
|
$baseTable->setRows($rows); |
|
101
|
|
|
$baseTable->setHeaders($this->headers); |
|
102
|
|
|
$baseTable->render(); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* @inheritDoc |
|
107
|
|
|
*/ |
|
108
|
|
|
public function getName() |
|
109
|
|
|
{ |
|
110
|
|
|
return 'table'; |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* @param array $rows |
|
115
|
|
|
* @return $this |
|
116
|
|
|
*/ |
|
117
|
|
|
public function setRows(array $rows) |
|
118
|
|
|
{ |
|
119
|
|
|
$this->rows = $rows; |
|
120
|
|
|
|
|
121
|
|
|
return $this; |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* @param array|string[] $headers |
|
126
|
|
|
* @return $this |
|
127
|
|
|
*/ |
|
128
|
|
|
public function setHeaders(array $headers) |
|
129
|
|
|
{ |
|
130
|
|
|
$this->headers = array_values($headers); |
|
131
|
|
|
|
|
132
|
|
|
return $this; |
|
133
|
|
|
} |
|
134
|
|
|
} |
|
135
|
|
|
|
This check looks for
@paramannotations where the type inferred by our type inference engine differs from the declared type.It makes a suggestion as to what type it considers more descriptive.
Most often this is a case of a parameter that can be null in addition to its declared types.