-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTwig.php
43 lines (34 loc) · 1.1 KB
/
Twig.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
<?php declare(strict_types = 1);
namespace Vairogs\Twig;
use Vairogs\Functions\Char;
use function is_array;
use function is_numeric;
use function sprintf;
trait Twig
{
public function makeArray(array $input, string $key, string $class): array
{
$output = [];
foreach ($this->parseFunctions(input: $input, key: $key) as $call => $function) {
if (is_array(value: $function)) {
$options = $function[2] ?? [];
unset($function[2]);
$output[] = new $class($call, $function, $options);
continue;
}
$output[] = new $class($call, [$this, $function, ]);
}
return $output;
}
private function parseFunctions(array $input, string $key): array
{
$functions = [];
foreach ($input as $call => $function) {
if (is_numeric(value: $call)) {
$call = (new Char())->toSnakeCase(string: $function, skipCamel: true);
}
$functions[sprintf('%s_%s', $key, $call)] = $function;
}
return $functions;
}
}