DataToTwigConvertor   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 83
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 0

Importance

Changes 0
Metric Value
wmc 13
lcom 2
cbo 0
dl 0
loc 83
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A nothing() 0 6 1
A vars() 0 14 4
A var() 0 6 2
A convert() 0 6 1
A valueToTwig() 0 14 4
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) {
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
     * @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