This repository has been archived by the owner on Mar 28, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ElementFactory.php
248 lines (224 loc) · 6.1 KB
/
ElementFactory.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
<?php
/**
* Copyright (C) GrizzIT, Inc. All rights reserved.
* See LICENSE for license details.
*/
namespace Ulrack\Cli\Factory;
use GrizzIt\Task\Common\TaskListInterface;
use Ulrack\Cli\Common\Io\WriterInterface;
use Ulrack\Cli\Common\Theme\ThemeInterface;
use Ulrack\Cli\Component\Element\ListElement;
use Ulrack\Cli\Component\Element\BlockElement;
use Ulrack\Cli\Component\Element\ChainElement;
use Ulrack\Cli\Component\Element\TableElement;
use Ulrack\Cli\Common\Element\ElementInterface;
use Ulrack\Cli\Component\Element\TextualElement;
use Ulrack\Cli\Common\Factory\IoFactoryInterface;
use Ulrack\Cli\Component\Element\ProgressElement;
use Ulrack\Cli\Common\Factory\ElementFactoryInterface;
use Ulrack\Cli\Component\Element\ExplainedListElement;
class ElementFactory implements ElementFactoryInterface
{
/**
* Contains the theme.
*
* @var ThemeInterface
*/
private $theme;
/**
* Contains the IO factory.
*
* @var IoFactoryInterface
*/
private $ioFactory;
/**
* Contains the used writer for the blocks.
*
* @var WriterInterface
*/
private $writer;
/**
* Constructor.
*
* @param ThemeInterface $theme
* @param IoFactoryInterface $ioFactory
* @param bool $useStderr
*/
public function __construct(
ThemeInterface $theme,
IoFactoryInterface $ioFactory,
bool $useStderr = false
) {
$this->theme = $theme;
$this->ioFactory = $ioFactory;
$this->writer = $useStderr
? $ioFactory->createErrorWriter()
: $ioFactory->createStandardWriter();
}
/**
* Creates a new text element.
*
* @param string $content
* @param bool $newLine
* @param string $styleKey
*
* @return ElementInterface
*/
public function createText(
string $content,
bool $newLine = true,
string $styleKey = 'text'
): ElementInterface {
return new TextualElement(
$content,
$this->writer,
$newLine,
$this->theme->getStyle($styleKey)
);
}
/**
* Creates a new table.
*
* @param string[] $keys
* @param array $items
* @param string $tableCharacters
* @param string $style
* @param string $boxStyle
* @param string $keyStyle
*
* @return ElementInterface
*/
public function createTable(
array $keys,
array $items,
string $tableCharacters = 'table-characters',
string $style = 'table-style',
string $boxStyle = 'table-box-style',
string $keyStyle = 'table-key-style'
): ElementInterface {
$table = new TableElement(
$this->writer,
$this->theme->getVariable($tableCharacters),
$this->theme->getStyle($style),
$this->theme->getStyle($boxStyle),
$this->theme->getStyle($keyStyle)
);
foreach ($keys as $key) {
$table->addKey($key);
}
$table->setItems($items);
return $table;
}
/**
* Creates a list.
*
* @param array $items
* @param string $style
*
* @return ElementInterface
*/
public function createList(
array $items,
string $style = 'list'
): ElementInterface {
$list = new ListElement(
$this->writer,
$this->theme->getStyle($style)
);
foreach ($items as $item) {
$list->addItem($item);
}
return $list;
}
/**
* Creates a new explained list.
*
* @param array $items
* @param string $keyStyle
* @param string $descriptionStyle
*
* @return ElementInterface
*/
public function createExplainedList(
array $items,
string $keyStyle = 'explained-list-key',
string $descriptionStyle = 'explained-list-description'
): ElementInterface {
$list = new ExplainedListElement(
$this->writer,
$this->theme->getStyle($keyStyle),
$this->theme->getStyle($descriptionStyle)
);
foreach ($items as $key => $description) {
$list->addItem($description, ...explode('.', $key));
}
return $list;
}
/**
* Creates a new block.
*
* @param string $content
* @param string $style
* @param string $padding
* @param string $margin
*
* @return ElementInterface
*/
public function createBlock(
string $content,
string $style = 'block',
string $padding = 'block-padding',
string $margin = 'block-margin'
): ElementInterface {
$block = new BlockElement(
$content,
$this->writer,
$this->ioFactory->createTerminal(),
$this->theme->getStyle($style)
);
$block->setPadding(
...$this->theme->getVariable($padding)
);
$block->setMargin(
...$this->theme->getVariable($margin)
);
return $block;
}
/**
* Creates a new chain element.
*
* @param ElementInterface ...$elements
*
* @return ElementInterface
*/
public function createChain(
ElementInterface ...$elements
): ElementInterface {
return new ChainElement(...$elements);
}
/**
* Creates a progress bar.
*
* @param TaskListInterface $taskList
* @param string $progressCharacters
* @param string $textStyle
* @param string $progressStyle
*
* @return ElementInterface
*/
public function createProgressBar(
TaskListInterface $taskList,
string $progressCharacters = 'progress-characters',
string $textStyle = 'progress-text',
string $progressStyle = 'progress-bar'
): ElementInterface {
return new ProgressElement(
$this->writer,
$this->theme->getVariable($progressCharacters),
$this->ioFactory->createTerminal(),
$taskList,
$this->theme->getStyle($textStyle),
$this->theme->getStyle($progressStyle)
);
}
}