Completed
Push — master ( bf06ad...9a7adb )
by Martin
01:18
created

DataToTwigConvertor::vars()   A

Complexity

Conditions 4
Paths 1

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 13
rs 9.8333
c 0
b 0
f 0
cc 4
nc 1
nop 1
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 static
29
     */
30
    public static function nothing(): self
31
    {
32
        return new static(function (array $data) {
0 ignored issues
show
Unused Code introduced by
The parameter $data is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
33
            return '';
34
        });
35
    }
36
37
    /**
38
     * @param bool $force Define the value of the variable even if it is passed to the template
39
     * @return static
40
     */
41
    public static function vars(bool $force = true): self
42
    {
43
        return new static(function (array $data) use ($force) {
44
            $content = '';
45
            foreach ($data as $key => $value) {
46
                if (preg_match('/^[a-z][0-9a-z_]*$/i', $key)) {
47
                    $content .= "{% set $key = " . ($force ? '' : "$key is defined ? $key : ") . static::valueToTwig($value) . " %}\n";
48
                }
49
            }
50
51
            return $content;
52
        });
53
    }
54
55
    /**
56
     * Converts data to one twig variable
57
     *
58
     * @param string $name  Variable name
59
     * @param bool   $force Define the value of the variable even if it is passed to the template
60
     * @return static
61
     */
62
    public static function var(string $name, bool $force = true): self
63
    {
64
        return new static(function (array $data) use ($name, $force) {
65
            return "{% set $name = " . ($force ? '' : "$name is defined ? $name : ") . static::valueToTwig($data) . "%}\n";
66
        });
67
    }
68
69
    public function convert(array $data): string
70
    {
71
        $convertor = $this->convertor;
72
73
        return $convertor($data);
74
    }
75
76
    public static function valueToTwig($value): string
77
    {
78
        if ($value instanceof \DateTimeInterface) {
79
            return '(' . $value->getTimestamp() . "|date_modify('0sec'))";
80
        } elseif (is_array($value)) {
81
            $twig = '{';
82
            foreach ($value as $key => $val) {
83
                $twig .= "$key: ".static::valueToTwig($val).", ";
84
            }
85
            return $twig."}";
86
        } else {
87
            return json_encode($value);
88
        }
89
    }
90
}
91