Completed
Pull Request — master (#169)
by Mikołaj
02:27
created

ContentParser::getFunctionArguments()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 17
rs 9.7
c 0
b 0
f 0
cc 2
nc 2
nop 2
1
<?php
2
3
/*
4
 * This file has been created by developers from BitBag.
5
 * Feel free to contact us once you face any issues or want to start
6
 * another great project.
7
 * You can find more information about us on https://bitbag.shop and write us
8
 * an email on [email protected].
9
 */
10
11
declare(strict_types=1);
12
13
namespace BitBag\SyliusCmsPlugin\Twig\Parser;
14
15
use Webmozart\Assert\Assert;
16
17
final class ContentParser implements ContentParserInterface
18
{
19
    /** @var \Twig_Environment */
20
    private $twigEnvironment;
21
22
    /** @var array */
23
    private $enabledFunctions;
24
25
    public function __construct(\Twig_Environment $twigEnvironment, array $enabledFunctions)
26
    {
27
        $this->twigEnvironment = $twigEnvironment;
28
        $this->enabledFunctions = $enabledFunctions;
29
    }
30
31
    public function parse(string $input): string
32
    {
33
        $functions = $this->twigEnvironment->getFunctions();
34
35
        foreach ($this->enabledFunctions as $function) {
36
            if (null !== $arguments = $this->getFunctionArguments($function, $input)) {
37
                $functionCall = '{{ ' . $function . '(\'' . rtrim(implode($arguments, '\' ,'), '\' ,') . '\') }}';
38
39
                try {
40
                    $functionResult = $this->callFunction($functions, $function, $arguments);
41
                } catch (\Exception $exception) {
42
                    $functionResult = '';
43
                }
44
45
                $input = str_replace($functionCall, $functionResult, $input);
46
            }
47
        }
48
49
        return $input;
50
    }
51
52
    private function getFunctionArguments(string $functionName, string $input): ?array
53
    {
54
        $start = '{{ ' . $functionName . '(';
55
        $end = ') }}';
56
        $functionParts = explode($start, $input);
57
58
        if (isset($functionParts[1])) {
59
            $functionParts = explode($end, $functionParts[1]);
60
            $arguments = explode(',', $functionParts[0]);
61
62
            return array_map(function (string $element): string {
63
                return trim($element, '\'');
64
            }, $arguments);
65
        }
66
67
        return null;
68
    }
69
70
    private function callFunction(array $functions, string $functionName, array $arguments): string
71
    {
72
        Assert::keyExists($functions, $functionName, sprintf('Function %s does not exist!', $functionName));
73
        /** @var \Twig_Function $function */
74
        $function = $functions[$functionName];
75
        $callable = $function->getCallable();
76
        $extension = $callable[0];
77
        $extensionMethod = $callable[1];
78
79
        return $extension->$extensionMethod(...$arguments);
80
    }
81
}
82