This repository was 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
/
Copy pathExplainedListElement.php
156 lines (138 loc) · 3.69 KB
/
ExplainedListElement.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
<?php
/**
* Copyright (C) GrizzIT, Inc. All rights reserved.
* See LICENSE for license details.
*/
namespace Ulrack\Cli\Component\Element;
use Ulrack\Cli\Common\Io\WriterInterface;
use Ulrack\Cli\Component\Theme\VoidStyle;
use Ulrack\Cli\Common\Theme\StyleInterface;
use Ulrack\Cli\Common\Element\ElementInterface;
class ExplainedListElement implements ElementInterface
{
/**
* Contains the writer to display the list.
*
* @var WriterInterface
*/
private $writer;
/**
* Contains the style of the key.
*
* @var StyleInterface
*/
private $keyStyle;
/**
* Contains the style of the description.
*
* @var StyleInterface
*/
private $descriptionStyle;
/**
* Contains the entries for the list.
*
* @var string[]
*/
private $items = ['items' => []];
/**
* Constructor.
*
* @param WriterInterface $writer
* @param StyleInterface $keyStyle
* @param StyleInterface $descriptionStyle
*/
public function __construct(
WriterInterface $writer,
StyleInterface $keyStyle = null,
StyleInterface $descriptionStyle = null
) {
$this->writer = $writer;
$this->descriptionStyle = $descriptionStyle ?? new VoidStyle();
$this->keyStyle = $keyStyle ?? new VoidStyle();
}
/**
* Renders the element.
*
* @return void
*/
public function render(): void
{
$items = $this->prepareArray(
$this->items['items']
);
$columnWidth = max(
array_map('strlen', array_column($items, 'key'))
) + 4;
foreach ($items as $item) {
$spaceless = ltrim($item['key']);
$spaces = str_replace($spaceless, '', $item['key']);
$this->writer->write($spaces);
$this->keyStyle->apply();
$this->writer->write($spaceless);
$this->keyStyle->reset();
$this->writer->write(
str_repeat(
' ',
$columnWidth - strlen($item['key'])
)
);
$this->descriptionStyle->apply();
$this->writer->write($item['description']);
$this->descriptionStyle->reset();
$this->writer->writeLine('');
}
}
/**
* Prepares the array for rendering.
*
* @param array $items
* @param int $depth
*
* @return array
*/
private function prepareArray(
array $items,
int $depth = 0
): array {
$return = [];
ksort($items);
foreach ($items as $key => $item) {
$return[] = [
'description' => $item['description'],
'key' => str_repeat(' ', $depth) . $key
];
if (
isset($item['items'])
&& !empty($item['items'])
) {
$return = array_merge(
$return,
$this->prepareArray(
$item['items'],
$depth + 1
)
);
}
}
return $return;
}
/**
* Adds an item to the list.
*
* @param string $description
* @param string[] $keys
*
* @return void
*/
public function addItem(string $description, string ...$keys): void
{
$items = &$this->items;
foreach ($keys as $key) {
if (!isset($items['items'][$key])) {
$items['items'][$key] = ['items' => [], 'description' => ''];
}
$items = &$items['items'][$key];
}
$items['description'] = $description;
}
}