1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Cycle\Schema\Renderer\ConsoleRenderer\Renderer; |
6
|
|
|
|
7
|
|
|
use Cycle\Schema\Renderer\ConsoleRenderer\Formatter; |
8
|
|
|
use Cycle\Schema\Renderer\ConsoleRenderer\Renderer; |
9
|
|
|
|
10
|
|
|
class ListenersRenderer implements Renderer |
11
|
|
|
{ |
12
|
|
|
private int $property; |
13
|
|
|
private string $title; |
14
|
|
|
|
15
|
|
|
public function __construct(int $property, string $title) |
16
|
|
|
{ |
17
|
|
|
$this->property = $property; |
18
|
|
|
$this->title = $title; |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
public function render(Formatter $formatter, array $schema, string $role): ?string |
22
|
|
|
{ |
23
|
|
|
if (! isset($schema[$this->property])) { |
24
|
|
|
return null; |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
$listeners = (array)($schema[$this->property] ?? []); |
28
|
|
|
|
29
|
|
|
if ($listeners === []) { |
30
|
|
|
return null; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
$rows = [ |
34
|
|
|
\sprintf('%s:', $formatter->title($this->title)), |
35
|
|
|
]; |
36
|
|
|
|
37
|
|
|
foreach ($listeners as $definition) { |
38
|
|
|
$data = (array)$definition; |
39
|
|
|
|
40
|
|
|
// Listener class |
41
|
|
|
$class = \is_string($data[0] ?? null) |
42
|
|
|
? $formatter->typecast($data[0]) |
43
|
|
|
: $formatter->error('undefined'); |
44
|
|
|
$rows[] = \sprintf('%s%s', $formatter->title(' '), $class); |
45
|
|
|
|
46
|
|
|
// Listener arguments |
47
|
|
|
$args = $data[1] ?? null; |
48
|
|
|
if ($args === null) { |
49
|
|
|
continue; |
50
|
|
|
} |
51
|
|
|
if (\is_array($args)) { |
52
|
|
|
foreach ($args as $key => $value) { |
53
|
|
|
$row = $formatter->title(' ') . ' - '; |
54
|
|
|
if (\is_string($key)) { |
55
|
|
|
$row .= $formatter->property($key) . ' : '; |
56
|
|
|
} |
57
|
|
|
$row .= $formatter->info($this->printValue($value, $formatter)); |
58
|
|
|
$rows[] = $row; |
59
|
|
|
} |
60
|
|
|
} elseif (is_string($args)) { |
61
|
|
|
$rows[] = $formatter->title(' ') . ' - ' . $formatter->info($this->printValue($args, $formatter)); |
62
|
|
|
} else { |
63
|
|
|
$rows[] = $formatter->typecast($this->printValue($data, $formatter)); |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
return \implode($formatter::LINE_SEPARATOR, $rows); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @param mixed $value |
72
|
|
|
*/ |
73
|
|
|
private function printValue($value, Formatter $formatter): string |
74
|
|
|
{ |
75
|
|
|
$data = \trim(\var_export($value, true), '\''); |
76
|
|
|
$data = \array_map( |
77
|
|
|
static fn (string $row): string => $formatter->title(' ') . $row, |
78
|
|
|
\explode("\n", $data) |
79
|
|
|
); |
80
|
|
|
|
81
|
|
|
return \ltrim( |
82
|
|
|
\implode("\n", $data) |
83
|
|
|
); |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|