Completed
Pull Request — master (#45)
by Christoffer
02:18
created

Printer::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Digia\GraphQL\Language;
4
5
use Digia\GraphQL\Error\LanguageException;
6
use Digia\GraphQL\Language\AST\Node\NodeInterface;
7
use Digia\GraphQL\Language\Writer\WriterInterface;
8
9
class Printer implements PrinterInterface
10
{
11
    /**
12
     * @var array|WriterInterface[]
13
     */
14
    protected $writers;
15
16
    /**
17
     * Printer constructor.
18
     * @param array|WriterInterface[] $writers
19
     */
20
    public function __construct($writers)
21
    {
22
        $this->writers = $writers;
23
    }
24
25
    /**
26
     * @inheritdoc
27
     * @throws LanguageException
28
     */
29
    public function print(NodeInterface $node): string
30
    {
31
        if (($writer = $this->getWriter($node)) !== null) {
32
            return $writer->write($node);
33
        }
34
35
        throw new LanguageException(sprintf('Invalid AST Node: %s', (string)$node));
36
    }
37
38
    /**
39
     * @param NodeInterface $node
40
     * @return WriterInterface|null
41
     */
42
    protected function getWriter(NodeInterface $node): ?WriterInterface
43
    {
44
        foreach ($this->writers as $writer) {
45
            if ($writer instanceof WriterInterface && $writer->supportsWriter($node)) {
46
                return $writer;
47
            }
48
        }
49
50
        return null;
51
    }
52
}
53