Passed
Push — main ( be944b...f6052f )
by Thomas
02:20
created

TemplateContext::context()   A

Complexity

Conditions 6
Paths 1

Size

Total Lines 22
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 6

Importance

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