1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of plumphp/plum-console. |
5
|
|
|
* |
6
|
|
|
* (c) Florian Eckerstorfer <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Plum\PlumConsole; |
13
|
|
|
|
14
|
|
|
use Cocur\Vale\Vale; |
15
|
|
|
use InvalidArgumentException; |
16
|
|
|
use Plum\Plum\Writer\WriterInterface; |
17
|
|
|
use Symfony\Component\Console\Helper\Table; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* ConsoleTableWriter. |
21
|
|
|
* |
22
|
|
|
* @author Florian Eckerstorfer |
23
|
|
|
* @copyright 2015 Florian Eckerstorfer |
24
|
|
|
*/ |
25
|
|
|
class ConsoleTableWriter implements WriterInterface |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* @var Table |
29
|
|
|
*/ |
30
|
|
|
protected $table; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var string[]|null |
34
|
|
|
*/ |
35
|
|
|
protected $header; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var bool |
39
|
|
|
*/ |
40
|
|
|
protected $autoDetectHeader = false; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @param Table $table |
44
|
|
|
* |
45
|
|
|
* @codeCoverageIgnore |
46
|
|
|
*/ |
47
|
|
|
public function __construct(Table $table) |
48
|
|
|
{ |
49
|
|
|
$this->table = $table; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @param string[] $header |
54
|
|
|
* |
55
|
|
|
* @return ConsoleTableWriter |
56
|
|
|
*/ |
57
|
1 |
|
public function setHeader(array $header) |
58
|
|
|
{ |
59
|
1 |
|
$this->header = $header; |
60
|
|
|
|
61
|
1 |
|
return $this; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @param bool $autoDetectHeader |
66
|
|
|
* |
67
|
|
|
* @return ConsoleTableWriter |
68
|
|
|
*/ |
69
|
1 |
|
public function autoDetectHeader($autoDetectHeader = true) |
70
|
|
|
{ |
71
|
1 |
|
$this->autoDetectHeader = $autoDetectHeader; |
72
|
|
|
|
73
|
1 |
|
return $this; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Write the given item. |
78
|
|
|
* |
79
|
|
|
* @param mixed $item |
80
|
|
|
*/ |
81
|
5 |
|
public function writeItem($item) |
82
|
|
|
{ |
83
|
5 |
|
if ($this->autoDetectHeader && !$this->header) { |
84
|
2 |
|
$this->handleAutoDetectHeader($item); |
85
|
1 |
|
} |
86
|
|
|
|
87
|
4 |
|
$this->table->addRow( |
88
|
4 |
|
$this->getValues($item, $this->getKeys($item)) |
89
|
3 |
|
); |
90
|
3 |
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Prepare the writer. |
94
|
|
|
* |
95
|
|
|
* |
96
|
|
|
* @codeCoverageIgnore |
97
|
|
|
*/ |
98
|
|
|
public function prepare() |
99
|
|
|
{ |
100
|
|
|
if ($this->header !== null) { |
101
|
|
|
$this->table->setHeaders($this->header); |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Finish the writer. |
107
|
|
|
*/ |
108
|
1 |
|
public function finish() |
109
|
|
|
{ |
110
|
1 |
|
$this->table->render(); |
111
|
1 |
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @param mixed $item |
115
|
|
|
*/ |
116
|
2 |
|
protected function handleAutoDetectHeader($item) |
117
|
|
|
{ |
118
|
2 |
|
if (!is_array($item)) { |
119
|
1 |
|
throw new InvalidArgumentException('Plum\PlumConsole\ConsoleTableWriter can only auto detect headers for '. |
120
|
1 |
|
'array items. For items of a type other than array the headers have '. |
121
|
1 |
|
'to be set manually.'); |
122
|
|
|
} |
123
|
|
|
|
124
|
1 |
|
$this->header = array_keys($item); |
|
|
|
|
125
|
1 |
|
$this->table->setHeaders($this->header); |
126
|
1 |
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* @param mixed $item |
130
|
|
|
* |
131
|
|
|
* @return string[] |
|
|
|
|
132
|
|
|
*/ |
133
|
4 |
|
protected function getKeys($item) |
134
|
|
|
{ |
135
|
4 |
|
if (is_array($item)) { |
136
|
2 |
|
return array_keys($item); |
137
|
2 |
|
} elseif ($this->header && is_object($item)) { |
138
|
1 |
|
return $this->header; |
139
|
|
|
} |
140
|
|
|
|
141
|
1 |
|
throw new InvalidArgumentException(sprintf( |
142
|
|
|
'Plum\PlumConsole\ConsoleTableWriter currently only supports array items or objects if headers '. |
143
|
1 |
|
'are set using the setHeader() method. You have passed an item of type "%s" to writeItem().', |
144
|
1 |
|
gettype($item) |
145
|
1 |
|
)); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* @param mixed $item |
150
|
|
|
* @param string[] $keys |
151
|
|
|
* |
152
|
|
|
* @return array |
153
|
|
|
*/ |
154
|
3 |
|
protected function getValues($item, array $keys) |
155
|
|
|
{ |
156
|
3 |
|
$values = []; |
157
|
3 |
|
foreach ($keys as $key) { |
158
|
3 |
|
$values[] = Vale::get($item, $key); |
159
|
3 |
|
} |
160
|
|
|
|
161
|
3 |
|
return $values; |
162
|
|
|
} |
163
|
|
|
} |
164
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..