OutputRenderer::addRenderer()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 2
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 2
nc 2
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Cycle\Schema\Renderer\ConsoleRenderer;
6
7
use Cycle\Schema\Renderer\SchemaRenderer;
8
9
class OutputRenderer implements SchemaRenderer
10
{
11
    /** @var Renderer[] */
12
    private array $renderers = [];
13
    private Formatter $formatter;
14
15
    public function __construct(Formatter $formatter, array $renderers = [])
16
    {
17
        $this->formatter = $formatter;
18
        $this->addRenderer(...$renderers);
19
    }
20
21
    final public function addRenderer(Renderer ...$renderers): void
22
    {
23
        foreach ($renderers as $renderer) {
24
            $this->renderers[] = $renderer;
25
        }
26
    }
27
28
    final public function render(array $schema): string
29
    {
30
        $result = '';
31
        foreach ($schema as $role => $roleSchema) {
32
            $result .= $this->renderRole($roleSchema, $role) . $this->formatter::ROLE_BLOCK_SEPARATOR;
33
        }
34
        return $result;
35
    }
36
37
    private function renderRole(array $schema, string $role): string
38
    {
39
        $rows = [];
40
41
        foreach ($this->renderers as $renderer) {
42
            if ($row = $renderer->render($this->formatter, $schema, $role)) {
43
                $rows[] = $row;
44
            }
45
        }
46
47
        return \implode($this->formatter::LINE_SEPARATOR, $rows);
48
    }
49
}
50