1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Matks\Vivian\Structure; |
4
|
|
|
|
5
|
|
|
use Matks\Vivian\Border\Border; |
6
|
|
|
use Matks\Vivian\Util; |
7
|
|
|
use Exception; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Structures (array printing) manager |
11
|
|
|
*/ |
12
|
|
|
class StructureManager |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* PHP does not allow use of function names |
16
|
|
|
* such as 'list' or 'array' |
17
|
|
|
*/ |
18
|
|
|
const ARRAY_STANDARD = 's_array'; |
19
|
|
|
const ARRAY_PHP = 's_phpArray'; |
20
|
|
|
const ARRAY_LIST = 's_list1'; |
21
|
|
|
const ARRAY_LIST2 = 's_list2'; |
22
|
|
|
const ARRAY_MATRIX = 's_matrix'; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Static calls interface |
26
|
|
|
*/ |
27
|
|
|
public static function __callstatic($name, $params) |
28
|
|
|
{ |
29
|
1 |
|
if (!in_array($name, static::getDisplayableStructures())) { |
30
|
1 |
|
throw new Exception("Unknown structure function name $name"); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
// target string is expected to be: |
34
|
1 |
|
$target = $params[0][0]; |
35
|
1 |
|
$functionName = '__' . $name; |
36
|
|
|
|
37
|
1 |
|
return static::$functionName($target); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Get implemented data structures display |
42
|
|
|
* |
43
|
|
|
* @return string[] |
44
|
|
|
*/ |
45
|
|
|
public static function getDisplayableStructures() |
46
|
1 |
|
{ |
47
|
|
|
$structures = array( |
48
|
1 |
|
static::ARRAY_STANDARD, |
49
|
1 |
|
static::ARRAY_PHP, |
50
|
1 |
|
static::ARRAY_LIST, |
51
|
1 |
|
static::ARRAY_LIST2, |
52
|
|
|
static::ARRAY_MATRIX |
53
|
1 |
|
); |
54
|
|
|
|
55
|
1 |
|
return $structures; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Render array displayed as structure |
60
|
|
|
* |
61
|
|
|
* @param array $array |
62
|
|
|
* @param Structure $structure |
63
|
|
|
* |
64
|
|
|
* @return string |
65
|
|
|
* @throws Exception |
66
|
|
|
*/ |
67
|
|
|
public static function buildStructure($array, Structure $structure) |
68
|
|
|
{ |
69
|
1 |
|
switch ($structure->getType()) { |
70
|
1 |
|
case Structure::TYPE_LIST: |
71
|
1 |
|
$result = self::buildList($array, $structure); |
72
|
1 |
|
break; |
73
|
1 |
|
case Structure::TYPE_ARRAY: |
74
|
1 |
|
$result = self::buildArray($array, $structure); |
75
|
1 |
|
break; |
76
|
1 |
|
default: |
77
|
|
|
throw new Exception('Unknown structure type ' . $structure->getType()); |
78
|
1 |
|
} |
79
|
|
|
|
80
|
1 |
|
return $result; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Shortcut to render a list |
85
|
|
|
* |
86
|
|
|
* @param $list |
87
|
|
|
* |
88
|
|
|
* @return string |
89
|
|
|
*/ |
90
|
|
|
private static function __s_list1($list) |
91
|
1 |
|
{ |
92
|
1 |
|
$structure = new Structure(Structure::TYPE_LIST); |
93
|
|
|
|
94
|
1 |
|
return static::buildStructure($list, $structure); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Shortcut to render a list |
99
|
|
|
* |
100
|
|
|
* @param $list |
101
|
|
|
* |
102
|
|
|
* @return string |
103
|
|
|
*/ |
104
|
|
|
private static function __s_list2($list) |
105
|
|
|
{ |
106
|
1 |
|
$structure = new Structure(Structure::TYPE_LIST, '-', Structure::FOUR_SPACE_TAB); |
107
|
|
|
|
108
|
|
|
return static::buildStructure($list, $structure); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Shortcut to render an array with borders |
113
|
|
|
* |
114
|
|
|
* @param $array |
115
|
|
|
* |
116
|
|
|
* @return string |
117
|
|
|
*/ |
118
|
|
|
private static function __s_array($array) |
119
|
|
|
{ |
120
|
|
|
$border = new Border(Border::TYPE_FRAME); |
121
|
|
|
$structure = new Structure(Structure::TYPE_ARRAY, '', null, '|', $border); |
122
|
|
|
|
123
|
|
|
return static::buildStructure($array, $structure); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Shortcut to render an array as a php array declaration |
128
|
|
|
* |
129
|
|
|
* @param $array |
130
|
|
|
* |
131
|
|
|
* @return string |
132
|
|
|
*/ |
133
|
|
|
private static function __s_phpArray($array) |
134
|
|
|
{ |
135
|
|
|
$structure = new Structure(Structure::TYPE_ARRAY, '', Structure::FOUR_SPACE_TAB, '=>'); |
136
|
|
|
|
137
|
|
|
return static::buildStructure($array, $structure); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Shortcut to render a matrix |
142
|
|
|
* |
143
|
|
|
* @param $array |
144
|
|
|
* |
145
|
|
|
* @throws \RuntimeException |
146
|
|
|
*/ |
147
|
|
|
private static function __s_matrix($array) |
|
|
|
|
148
|
|
|
{ |
149
|
|
|
throw new \RuntimeException('Not implemented yet'); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* Render a structure of type 'list |
154
|
|
|
* |
155
|
|
|
* @param array $list |
156
|
|
|
* @param Structure $structure |
157
|
|
|
* |
158
|
|
|
* @return string |
159
|
|
|
*/ |
160
|
|
|
private static function buildList(array $list, Structure $structure) |
161
|
|
|
{ |
162
|
1 |
|
$insertTab = ($structure->getTab()) ? true : false; |
163
|
|
|
|
164
|
1 |
|
$result = ''; |
165
|
1 |
|
foreach ($list as $value) { |
166
|
1 |
|
$result .= ($insertTab ? $structure->getTab() : '') . $structure->getIteratorCharacter() . ' ' . $value . PHP_EOL; |
167
|
1 |
|
} |
168
|
|
|
|
169
|
1 |
|
return $result; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* Render a structure of type 'array' |
174
|
|
|
* |
175
|
|
|
* @param array $array |
176
|
|
|
* @param Structure $structure |
177
|
|
|
* |
178
|
|
|
* @return string |
179
|
|
|
*/ |
180
|
|
|
private static function buildArray(array $array, Structure $structure) |
181
|
|
|
{ |
182
|
1 |
|
$maxKeyLength = Util::getMaxKeyLength($array); |
183
|
1 |
|
$maxValueLength = Util::getMaxValueLength($array); |
184
|
1 |
|
$drawBorders = ($structure->getBorder()) ? true : false; |
185
|
1 |
|
$insertTab = ($structure->getTab()) ? true : false; |
186
|
|
|
|
187
|
1 |
|
if ($drawBorders) { |
188
|
1 |
|
$firstLine = self::printBorder($structure->getBorder(), $maxKeyLength, $maxValueLength); |
189
|
1 |
|
if ($insertTab) { |
190
|
|
|
$firstLine = $structure->getTab() . $firstLine; |
191
|
|
|
} |
192
|
1 |
|
} else { |
193
|
1 |
|
$firstLine = ''; |
194
|
|
|
} |
195
|
|
|
|
196
|
1 |
|
$lines = ''; |
197
|
1 |
|
foreach ($array as $key => $value) { |
198
|
1 |
|
$lines .= self::printStructureLine($key, $value, $structure, $maxKeyLength, $maxValueLength); |
199
|
1 |
|
} |
200
|
|
|
|
201
|
1 |
|
$lastLine = $firstLine; |
202
|
1 |
|
$result = $firstLine . $lines . $lastLine; |
203
|
|
|
|
204
|
1 |
|
return $result; |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* @param Border $border |
209
|
|
|
* @param int $maxKeyLength |
210
|
|
|
* @param int $maxValueLength |
211
|
|
|
* |
212
|
|
|
* @return string |
213
|
|
|
*/ |
214
|
|
|
private static function printBorder(Border $border, $maxKeyLength, $maxValueLength) |
215
|
|
|
{ |
216
|
1 |
|
$result = ''; |
217
|
|
|
|
218
|
1 |
|
$keyBorderLine = Util::buildPatternLine($border->getLineCharacter(), $maxKeyLength + 2); |
219
|
1 |
|
$valueBorderLine = Util::buildPatternLine($border->getLineCharacter(), $maxValueLength + 2); |
220
|
|
|
|
221
|
1 |
|
$result .= $border->getCrossCharacter() . $keyBorderLine . $border->getCrossCharacter(); |
222
|
1 |
|
$result .= $valueBorderLine . $border->getCrossCharacter() . PHP_EOL; |
223
|
|
|
|
224
|
1 |
|
return $result; |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
/** |
228
|
|
|
* @param string $key |
229
|
|
|
* @param string $value |
230
|
|
|
* @param Structure $structure |
231
|
|
|
* @param int $maxKeyLength |
232
|
|
|
* @param int $maxValueLength |
233
|
|
|
* |
234
|
|
|
* @return string |
235
|
|
|
*/ |
236
|
|
|
private static function printStructureLine($key, $value, Structure $structure, $maxKeyLength, $maxValueLength) |
237
|
|
|
{ |
238
|
1 |
|
$result = ''; |
239
|
1 |
|
$insertTab = ($structure->getTab()) ? true : false; |
240
|
1 |
|
$border = $structure->getBorder(); |
241
|
|
|
|
242
|
1 |
|
if ($border) { |
243
|
1 |
|
$keyLength = Util::getVisibleStringLength($key); |
244
|
1 |
|
$missingKeyLength = $maxKeyLength - $keyLength; |
245
|
1 |
|
$fillingKeySpace = Util::buildPatternLine(' ', $missingKeyLength); |
246
|
|
|
|
247
|
1 |
|
$valueLength = Util::getVisibleStringLength($value); |
248
|
1 |
|
$missingValueLength = $maxValueLength - $valueLength; |
249
|
1 |
|
$fillingValueSpace = Util::buildPatternLine(' ', $missingValueLength); |
250
|
|
|
|
251
|
1 |
|
$result .= ($insertTab ? $structure->getTab() : ''); |
252
|
1 |
|
$result .= $border->getColumnCharacter() . ' ' . $key . $fillingKeySpace . ' '; |
253
|
1 |
|
$result .= $structure->getKeyToValueCharacter() . ' ' . $value . $fillingValueSpace . ' '; |
254
|
1 |
|
$result .= $border->getColumnCharacter() . PHP_EOL; |
255
|
1 |
|
} else { |
256
|
1 |
|
$keyLength = Util::getVisibleStringLength($key); |
257
|
1 |
|
$missingKeyLength = $maxKeyLength - $keyLength; |
258
|
1 |
|
$fillingKeySpace = Util::buildPatternLine(' ', $missingKeyLength); |
259
|
|
|
|
260
|
1 |
|
$result .= ($insertTab ? $structure->getTab() : ''); |
261
|
1 |
|
$result .= $key . $fillingKeySpace . ' '; |
262
|
1 |
|
$result .= $structure->getKeyToValueCharacter() . ' ' . $value; |
263
|
1 |
|
$result .= PHP_EOL; |
264
|
|
|
} |
265
|
|
|
|
266
|
1 |
|
return $result; |
267
|
|
|
} |
268
|
|
|
} |
269
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.