Completed
Push — master ( 249c16...582c12 )
by Paweł
16s
created

GimmeExtension::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 2
crap 1
1
<?php
2
3
/*
4
 * This file is part of the Superdesk Web Publisher Templates System.
5
 *
6
 * Copyright 2015 Sourcefabric z.ú. and contributors.
7
 *
8
 * For the full copyright and license information, please see the
9
 * AUTHORS and LICENSE files distributed with this source code.
10
 *
11
 * @copyright 2015 Sourcefabric z.ú
12
 * @license http://www.superdesk.org/license
13
 */
14
15
namespace SWP\Component\TemplatesSystem\Twig\Extension;
16
17
use SWP\Component\TemplatesSystem\Gimme\Context\Context;
18
use SWP\Component\TemplatesSystem\Gimme\Loader\LoaderInterface;
19
use SWP\Component\TemplatesSystem\Twig\TokenParser\GimmeListTokenParser;
20
use SWP\Component\TemplatesSystem\Twig\TokenParser\GimmeTokenParser;
21
22
class GimmeExtension extends \Twig_Extension implements \Twig_Extension_GlobalsInterface
23
{
24
    /**
25
     * @var LoaderInterface
26
     */
27
    protected $loader;
28
29
    /**
30
     * @var Context
31
     */
32
    protected $context;
33
34
    /**
35
     * GimmeExtension constructor.
36
     *
37
     * @param Context         $context
38
     * @param LoaderInterface $loader
39
     */
40 110
    public function __construct(Context $context, LoaderInterface $loader)
41
    {
42 110
        $this->context = $context;
43 110
        $this->loader = $loader;
44 110
    }
45
46
    /**
47
     * @return LoaderInterface
48
     */
49 9
    public function getLoader()
50
    {
51 9
        return $this->loader;
52
    }
53
54
    /**
55
     * @return Context
56
     */
57 1
    public function getContext()
58
    {
59 1
        return $this->context;
60
    }
61
62
    /**
63
     * @return array
64
     */
65 19
    public function getTokenParsers()
66
    {
67
        return [
68 19
            new GimmeTokenParser(),
69 19
            new GimmeListTokenParser(),
70
        ];
71
    }
72
73
    /**
74
     * @return array
75
     */
76 19
    public function getFilters()
77
    {
78
        return [
79
            new \Twig_SimpleFilter('start', function ($node, $value) {
80
                $node['_collection_type_filters']['start'] = $value;
81
82
                return $node;
83 19
            }, ['needs_context' => false]),
84
            new \Twig_SimpleFilter('limit', function ($node, $value) {
85
                $node['_collection_type_filters']['limit'] = $value;
86
87
                return $node;
88 19
            }, ['needs_context' => false]),
89 19
            new \Twig_SimpleFilter('order', function ($node, $value1, $value2) {
90
                $node['_collection_type_filters']['order'] = [$value1, $value2];
91
92
                return $node;
93 19
            }, ['needs_context' => false]),
94
            new \Twig_SimpleFilter('dateRange', function ($node, $value1, $value2) {
95
                $node['_collection_type_filters']['date_range'] = [$value1, $value2];
96
97
                return $node;
98
            }, ['needs_context' => false]),
99
        ];
100 26
    }
101
102 26
    /**
103
     * @return array
104
     */
105
    public function getGlobals()
106
    {
107
        return ['gimme' => $this->context];
108 109
    }
109
110 109
    /**
111
     * @return string
112
     */
113
    public function getName()
114
    {
115
        return self::class;
116
    }
117
}
118