DeferrableTermFieldPrinter::printFields()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
eloc 4
nc 3
nop 2
dl 0
loc 9
ccs 0
cts 5
cp 0
crap 12
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Leonidas\Library\Admin\Printer;
4
5
use Leonidas\Contracts\Admin\Component\TermField\TermFieldInterface;
6
use Leonidas\Contracts\Admin\Printer\TermFieldPrinterInterface;
7
use Psr\Http\Message\ServerRequestInterface;
8
9
class DeferrableTermFieldPrinter implements TermFieldPrinterInterface
10
{
11
    protected ?TermFieldPrinterInterface $printer = null;
12
13
    public function __construct(?TermFieldPrinterInterface $printer = null)
14
    {
15
        $this->printer = $printer;
16
    }
17
18
    public function print(array $fields, ServerRequestInterface $request): string
19
    {
20
        return $this->printer
21
            ? $this->printer->print($fields, $request)
22
            : $this->printFields($fields, $request);
23
    }
24
25
    public function printOne(TermFieldInterface $field, ServerRequestInterface $request): string
26
    {
27
        return $this->printer
28
            ? $this->printer->printOne($field, $request)
29
            : $field->renderComponent($request);
30
    }
31
32
    /**
33
     * @param TermFieldInterface[] $fields
34
     */
35
    protected function printFields(array $fields, ServerRequestInterface $request): string
36
    {
37
        $output = '';
38
39
        foreach ($fields as $fields) {
40
            $output .= $fields->shouldBeRendered($request) ? $fields->renderComponent($request) : '';
41
        }
42
43
        return $output;
44
    }
45
}
46