Passed
Push — main ( f6052f...80f8ac )
by Thomas
02:23
created

TemplateContext::has()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Conia\Boiler;
6
7
use Symfony\Component\HtmlSanitizer\HtmlSanitizerConfig;
8
9
/** @psalm-api */
10
class TemplateContext
11
{
12
    private const ESCAPE_FLAGS = ENT_QUOTES | ENT_SUBSTITUTE;
13
    private const ESCAPE_ENCODING = 'UTF-8';
14
15
    /**
16
     * @psalm-param list<class-string> $whitelist
17
     */
18 51
    public function __construct(
19
        protected readonly Template $template,
20
        public array $context,
21
        public readonly array $whitelist,
22
        public readonly bool $autoescape,
23
    ) {
24 51
    }
25
26 3
    public function __call(string $name, array $args): mixed
27
    {
28 3
        $callable = $this->template->getMethods()->get($name);
29
30 2
        return $callable(...$args);
31
    }
32
33 49
    public function context(array $values = []): array
34
    {
35 49
        return array_map(
36 49
            [$this, 'wrapIf'],
37 49
            array_merge($this->context, $values)
38 49
        );
39
    }
40
41 1
    public function add(string $key, mixed $value): mixed
42
    {
43 1
        $this->context[$key] = $value;
44
45 1
        return $this->wrapIf($value);
46
    }
47
48 3
    public function e(
49
        string|Value $value,
50
        int $flags = self::ESCAPE_FLAGS,
51
        string $encoding = self::ESCAPE_ENCODING,
52
    ): string {
53 3
        if ($value instanceof Value) {
54 1
            return htmlspecialchars((string)$value->unwrap(), $flags, $encoding);
55
        }
56
57 2
        return htmlspecialchars($value, $flags, $encoding);
58
    }
59
60 2
    public function escape(
61
        string|Value $value,
62
        int $flags = self::ESCAPE_FLAGS,
63
        string $encoding = self::ESCAPE_ENCODING,
64
    ): string {
65 2
        return $this->e($value, $flags, $encoding);
66
    }
67
68 1
    public function clean(
69
        string $value,
70
        HtmlSanitizerConfig $config = null,
71
        bool $removeEmptyLines = true,
72
    ): string {
73 1
        return Sanitizer::clean($value, $config, $removeEmptyLines);
74
    }
75
76 2
    public function url(string|Value $value): string
77
    {
78 2
        return Url::clean($value instanceof Value ? (string)$value->unwrap() : $value);
79
    }
80
81
    /**
82
     * @psalm-param non-empty-string $path
83
     */
84 13
    public function layout(string $path, ?array $context = null): void
85
    {
86 13
        $this->template->setLayout(new LayoutValue($path, $context));
87
    }
88
89
    /**
90
     * Includes another template into the current template.
91
     *
92
     * If no context is passed it shares the context of the calling template.
93
     *
94
     * @psalm-param non-empty-string $path
95
     */
96 1
    public function insert(string $path, array $context = []): void
97
    {
98 1
        $path = $this->template->engine->getFile($path);
99 1
        $template = new Template(
100 1
            $path,
101 1
            sections: $this->template->sections,
102 1
            engine: $this->template->engine,
103 1
        );
104
105 1
        if (func_num_args() > 1) {
106 1
            echo $template->render($context, $this->whitelist, $this->autoescape);
107
        } else {
108 1
            echo $template->render($this->context, $this->whitelist, $this->autoescape);
109
        }
110
    }
111
112 3
    public function begin(string $name): void
113
    {
114 3
        $this->template->sections->begin($name);
115
    }
116
117 1
    public function append(string $name): void
118
    {
119 1
        $this->template->sections->append($name);
120
    }
121
122 1
    public function prepend(string $name): void
123
    {
124 1
        $this->template->sections->prepend($name);
125
    }
126
127 3
    public function end(): void
128
    {
129 3
        $this->template->sections->end();
130
    }
131
132 2
    public function section(string $name): string
133
    {
134 2
        return $this->template->sections->get($name);
135
    }
136
137 2
    public function has(string $name): bool
138
    {
139 2
        return $this->template->sections->has($name);
140
    }
141
142 39
    protected function wrapIf(mixed $value): mixed
143
    {
144 39
        if ($value instanceof ValueInterface) {
145 2
            return $value;
146
        }
147
148 39
        if (is_object($value)) {
149 18
            foreach ($this->whitelist as $whitelisted) {
150
                if (
151 3
                    $value::class === $whitelisted
152 3
                    || is_subclass_of($value::class, $whitelisted)
153
                ) {
154 3
                    return $value;
155
                }
156
            }
157
        }
158
159 39
        return Wrapper::wrap($value);
160
    }
161
}
162