|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This is part of the webuni/front-matter package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Martin Hasoň <[email protected]> |
|
7
|
|
|
* (c) Webuni s.r.o. <[email protected]> |
|
8
|
|
|
* |
|
9
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
10
|
|
|
* file that was distributed with this source code. |
|
11
|
|
|
*/ |
|
12
|
|
|
|
|
13
|
|
|
namespace Webuni\FrontMatter\Twig; |
|
14
|
|
|
|
|
15
|
|
|
class DataToTwigConvertor |
|
16
|
|
|
{ |
|
17
|
|
|
/** @var callable */ |
|
18
|
|
|
private $convertor; |
|
19
|
|
|
|
|
20
|
|
|
protected function __construct(callable $convertor) |
|
21
|
|
|
{ |
|
22
|
|
|
$this->convertor = $convertor; |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Data will not be written to the twig template |
|
27
|
|
|
* |
|
28
|
|
|
* @return self |
|
29
|
|
|
*/ |
|
30
|
|
|
public static function nothing(): self |
|
31
|
|
|
{ |
|
32
|
|
|
return new self(function (array $data) { |
|
|
|
|
|
|
33
|
|
|
return ''; |
|
34
|
|
|
}); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @psalm-suppress MixedAssignment |
|
39
|
|
|
* @param bool $force Define the value of the variable even if it is passed to the template |
|
40
|
|
|
* @return self |
|
41
|
|
|
*/ |
|
42
|
|
|
public static function vars(bool $force = true): self |
|
43
|
|
|
{ |
|
44
|
|
|
return new self(function (array $data) use ($force) { |
|
45
|
|
|
$content = ''; |
|
46
|
|
|
foreach ($data as $key => $value) { |
|
47
|
|
|
if (is_int($key)) { |
|
48
|
|
|
continue; |
|
49
|
|
|
} |
|
50
|
|
|
$content .= "{% set $key = " . ($force ? '' : "$key is defined ? $key : ") . self::valueToTwig($value) . " %}\n"; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
return $content; |
|
54
|
|
|
}); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Converts data to one twig variable |
|
59
|
|
|
* |
|
60
|
|
|
* @param string $name Variable name |
|
61
|
|
|
* @param bool $force Define the value of the variable even if it is passed to the template |
|
62
|
|
|
* @return self |
|
63
|
|
|
*/ |
|
64
|
|
|
public static function var(string $name, bool $force = true): self |
|
65
|
|
|
{ |
|
66
|
|
|
return new self(function (array $data) use ($name, $force) { |
|
67
|
|
|
return "{% set $name = " . ($force ? '' : "$name is defined ? $name : ") . self::valueToTwig($data) . "%}\n"; |
|
68
|
|
|
}); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
public function convert(array $data): string |
|
72
|
|
|
{ |
|
73
|
|
|
$convertor = $this->convertor; |
|
74
|
|
|
|
|
75
|
|
|
return (string) $convertor($data); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* @psalm-suppress MixedAssignment |
|
80
|
|
|
* @param mixed $value |
|
81
|
|
|
* @return string |
|
82
|
|
|
*/ |
|
83
|
|
|
protected static function valueToTwig($value): string |
|
84
|
|
|
{ |
|
85
|
|
|
if ($value instanceof \DateTimeInterface) { |
|
86
|
|
|
return '(' . $value->getTimestamp() . "|date_modify('0sec'))"; |
|
87
|
|
|
} elseif (is_array($value)) { |
|
88
|
|
|
$twig = '{'; |
|
89
|
|
|
foreach ($value as $key => $val) { |
|
90
|
|
|
$twig .= "$key: ".static::valueToTwig($val).", "; |
|
91
|
|
|
} |
|
92
|
|
|
return $twig."}"; |
|
93
|
|
|
} else { |
|
94
|
|
|
return (string) json_encode($value, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE); |
|
95
|
|
|
} |
|
96
|
|
|
} |
|
97
|
|
|
} |
|
98
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.