Passed
Branch master (c65ffc)
by Dāvis
03:08
created

PaginationExtension::getFunctions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Sludio\HelperBundle\Pagination\Twig;
4
5
use Sludio\HelperBundle\Pagination\Twig\Behaviour\FixedLength;
6
use Sludio\HelperBundle\Pagination\Twig\Behaviour\PaginationBehaviour;
7
8
class PaginationExtension extends \Twig_Extension
9
{
10
    /**
11
     * @var PaginationBehaviour[]
12
     */
13
    private $functions;
14
15
    public function __construct($container)
16
    {
17
        $shortFunctions = $container->hasParameter('sludio_helper.script.short_functions') && $container->getParameter('sludio_helper.script.short_functions', false);
18
19
        $this->functions = [];
20
        if ($container->hasParameter('sludio_helper.pagination.behaviour') && !empty($container->getParameter('sludio_helper.pagination.behaviour', []))) {
21
            $functions = $container->getParameter('sludio_helper.pagination.behaviour');
22
            foreach ($functions as $function) {
23
                if ($shortFunctions) {
24
                    array_push($this->functions, $this->withFunction(array_keys($function)[0], array_values($function)[0]));
25
                }
26
                array_push($this->functions, $this->withFunction('sludio_'.array_keys($function)[0], array_values($function)[0]));
27
            }
28
        }
29
    }
30
31
    /**
32
     * @return string
33
     */
34
    public function getName()
35
    {
36
        return 'sludio_helper.twig.pagination_extension';
37
    }
38
39
    public function withFunction($functionName, $behaviour)
40
    {
41
        $functionName = $this->suffixFunctionName($functionName);
42
        $behaviour = new FixedLength($behaviour);
43
44
        $clone = clone $this;
45
46
        $clone->functions[$functionName] = new \Twig_SimpleFunction($functionName, [
47
            $behaviour,
48
            'getPaginationData',
49
        ]);
50
51
        return $clone->functions[$functionName];
52
    }
53
54
    public function withoutFunction($functionName)
55
    {
56
        $functionName = $this->suffixFunctionName($functionName);
57
58
        $clone = clone $this;
59
        unset($clone->functions[$functionName]);
60
61
        return $clone;
62
    }
63
64
    /**
65
     * @param string $functionName
66
     *
67
     * @return string
68
     */
69
    private function suffixFunctionName($functionName)
70
    {
71
        // Make sure the function name is not suffixed twice.
72
        $functionName = preg_replace('/(_pagination)$/', '', (string)$functionName);
73
74
        return $functionName.'_pagination';
75
    }
76
77
    /**
78
     * @return \Twig_SimpleFunction[]
79
     */
80
    public function getFunctions()
81
    {
82
        return array_values($this->functions);
83
    }
84
}
85