Passed
Push — main ( fd0a18...f48e09 )
by Thomas
13:11
created

TemplateContext::add()   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
nc 1
nop 2
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
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
            function ($value): mixed {
37 39
                if (is_object($value)) {
38 20
                    foreach ($this->whitelist as $whitelisted) {
39
                        if (
40 3
                            $value::class === $whitelisted
41 3
                            || is_subclass_of($value::class, $whitelisted)
42
                        ) {
43 3
                            return $value;
44
                        }
45
                    }
46
                }
47
48 39
                if (is_scalar($value) && !is_string($value)) {
49 3
                    return $value;
50
                }
51
52 39
                if (is_null($value)) {
53 1
                    return null;
54
                }
55
56 39
                return Wrapper::wrap($value);
57 49
            },
58 49
            array_merge($this->context, $values)
59 49
        );
60
    }
61
62 1
    public function add(string $key, mixed $value): void
63
    {
64 1
        $this->context[$key] = $value;
65
    }
66
67 3
    public function e(
68
        string|Value $value,
69
        int $flags = self::ESCAPE_FLAGS,
70
        string $encoding = self::ESCAPE_ENCODING,
71
    ): string {
72 3
        if ($value instanceof Value) {
73 1
            return htmlspecialchars((string)$value->unwrap(), $flags, $encoding);
74
        }
75
76 2
        return htmlspecialchars($value, $flags, $encoding);
77
    }
78
79 2
    public function escape(
80
        string|Value $value,
81
        int $flags = self::ESCAPE_FLAGS,
82
        string $encoding = self::ESCAPE_ENCODING,
83
    ): string {
84 2
        return $this->e($value, $flags, $encoding);
85
    }
86
87 1
    public function clean(
88
        string $value,
89
        HtmlSanitizerConfig $config = null,
90
        bool $removeEmptyLines = true,
91
    ): string {
92 1
        return Sanitizer::clean($value, $config, $removeEmptyLines);
93
    }
94
95 2
    public function url(string|Value $value): string
96
    {
97 2
        return Url::clean($value instanceof Value ? (string)$value->unwrap() : $value);
98
    }
99
100
    /**
101
     * @psalm-param non-empty-string $path
102
     */
103 13
    public function layout(string $path, ?array $context = null): void
104
    {
105 13
        $this->template->setLayout(new LayoutValue($path, $context));
106
    }
107
108
    /**
109
     * Includes another template into the current template.
110
     *
111
     * If no context is passed it shares the context of the calling template.
112
     *
113
     * @psalm-param non-empty-string $path
114
     */
115 1
    public function insert(string $path, array $context = []): void
116
    {
117 1
        $path = $this->template->engine->getFile($path);
118 1
        $template = new Template(
119 1
            $path,
120 1
            sections: $this->template->sections,
121 1
            engine: $this->template->engine,
122 1
        );
123
124 1
        if (func_num_args() > 1) {
125 1
            echo $template->render($context, $this->whitelist, $this->autoescape);
126
        } else {
127 1
            echo $template->render($this->context, $this->whitelist, $this->autoescape);
128
        }
129
    }
130
131 3
    public function begin(string $name): void
132
    {
133 3
        $this->template->sections->begin($name);
134
    }
135
136 1
    public function append(string $name): void
137
    {
138 1
        $this->template->sections->append($name);
139
    }
140
141 1
    public function prepend(string $name): void
142
    {
143 1
        $this->template->sections->prepend($name);
144
    }
145
146 3
    public function end(): void
147
    {
148 3
        $this->template->sections->end();
149
    }
150
151 2
    public function section(string $name): string
152
    {
153 2
        return $this->template->sections->get($name);
154
    }
155
156 2
    public function has(string $name): bool
157
    {
158 2
        return $this->template->sections->has($name);
159
    }
160
}
161