DataTableTwigExtension::toHeadersFilter()   C
last analyzed

Complexity

Conditions 15
Paths 20

Size

Total Lines 43
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 43
rs 5.0504
c 0
b 0
f 0
cc 15
eloc 21
nc 20
nop 2

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Knp\RadBundle\Twig;
4
5
class DataTableTwigExtension extends \Twig_Extension
6
{
7
8
    private $container;
9
10
    public function __construct($container)
11
    {
12
        $this->container = $container;
13
    }
14
15
    public function getFunctions()
16
    {
17
        return array(
18
            new \Twig_SimpleFunction('bootstrap_datatable', array($this, 'getDataTableRender'), array('is_safe' => array('html'))),
19
            new \Twig_SimpleFunction('bootstrap_datatable_row', array($this, 'getDataTableRowRender'), array('is_safe' => array('html'))),
20
        );
21
    }
22
23
    public function getFilters()
24
    {
25
        return array(
26
            new \Twig_SimpleFilter('toArray', array($this, 'toArrayFilter')),
27
            new \Twig_SimpleFilter('toHeadersArray', array($this, 'toHeadersFilter')),
28
        );
29
    }
30
31
    public function getDataTableRender($elements, $options = array())
32
    {
33
        $options = array_merge(array('bootstrap' => "Default"), $options);
34
35
        return $this
36
            ->container
37
            ->get('templating')
38
            ->render(
39
                'KnpRadBundle:Twig:Datatable/' . $options['bootstrap'] . '/datatable.html.twig',
40
                array('elements' => $elements, 'options' => $options)
41
            )
42
        ;
43
    }
44
45
    public function getDataTableRowRender($element, $headers, $options = array())
46
    {
47
        $options = array_merge(array('bootstrap' => "Default"), $options);
48
49
        $routes = isset($options['routes'])
50
            ? $options['routes']
51
            : array()
52
        ;
53
54
        return $this
55
            ->container
56
            ->get('templating')
57
            ->render(
58
                'KnpRadBundle:Twig:Datatable/' . $options['bootstrap'] . '/datatable_row.html.twig',
59
                array(
60
                    'element' => $element,
61
                    'headers' => $headers,
62
                    'options' => $options,
63
                    'routes'  => $routes
64
                )
65
            )
66
        ;
67
    }
68
69
    public function toHeadersFilter($els, $options = array ())
70
    {
71
72
        $fields = isset($options['fields'])
73
            ? $options['fields']
74
            : array()
75
        ;
76
77
        $headers = array();
78
79
        foreach ($els as $el) {
0 ignored issues
show
Coding Style introduced by
Blank line found at start of control structure
Loading history...
80
81
            if (is_object($el)) {
0 ignored issues
show
Coding Style introduced by
Blank line found at start of control structure
Loading history...
82
83
                foreach ($this->getGettersWithoutParameters($el) as $method) {
84
                    preg_match('#get(?P<name>.*)#', $method->name, $matches);
85
                    $name = strtolower($matches['name']);
86
                    if (!in_array($name, $headers) && (0 === count($fields) || array_key_exists($name, $fields))) {
87
                        $headers[$name] = array_key_exists($name, $fields)
88
                            ? $fields[$name]
89
                            : $name
90
                        ;
91
                    }
92
                }
93
0 ignored issues
show
Coding Style introduced by
Blank line found at end of control structure
Loading history...
94
            } elseif (is_array($el)) {
0 ignored issues
show
Coding Style introduced by
Blank line found at start of control structure
Loading history...
95
96
                foreach ($el as $key => $value) {
97
                    if (!in_array($key, $headers) && (0 === count($fields) || array_key_exists($key, $fields))) {
98
                        $headers[$key] = array_key_exists($key, $fields)
99
                            ? $fields[$key]
100
                            : $key
101
                        ;
102
                    }
103
                }
104
0 ignored issues
show
Coding Style introduced by
Blank line found at end of control structure
Loading history...
105
            }
106
0 ignored issues
show
Coding Style introduced by
Blank line found at end of control structure
Loading history...
107
        }
108
109
        return $headers;
110
111
    }
112
113
    public function toArrayFilter($el, $options = array ())
114
    {
115
116
        $fields = isset($options['fields'])
117
            ? $options['fields']
118
            : array()
119
        ;
120
121
        $values = array();
122
123
        if (is_object($el)) {
0 ignored issues
show
Coding Style introduced by
Blank line found at start of control structure
Loading history...
124
125
            foreach ($this->getGettersWithoutParameters($el) as $method) {
126
                preg_match('#get(?P<name>.*)#', $method->name, $matches);
127
128
                $name = strtolower($matches['name']);
129
                if (0 === count($fields) || array_key_exists($name, $fields)) {
130
                    $value = $el->{$method->name}();
131
                    if (!is_object($value) && !is_array($value)) {
132
                        $values[$name] = $value;
133
                    }
134
                }
135
            }
136
0 ignored issues
show
Coding Style introduced by
Blank line found at end of control structure
Loading history...
137
        } elseif (is_array($el)) {
0 ignored issues
show
Coding Style introduced by
Blank line found at start of control structure
Loading history...
138
139
            foreach ($el as $key => $value) {
140
                if (!is_object($value) && !is_array($value)) {
141
                    $values[$key] = $value;
142
                }
143
            }
144
0 ignored issues
show
Coding Style introduced by
Blank line found at end of control structure
Loading history...
145
        }
146
147
        return $values;
148
    }
149
150
    public function getGettersWithoutParameters($el)
151
    {
152
        $rfl = new \ReflectionClass($el);
153
154
        return array_filter(
155
            $rfl->getMethods(\ReflectionMethod::IS_PUBLIC),
156
            function ($method) {
157
                return preg_match('#get.*#', $method->name) && 0 === count($method->getParameters());
158
            }
159
0 ignored issues
show
Coding Style introduced by
Empty lines are not allowed in multi-line function calls
Loading history...
160
        );
161
    }
162
163
    public function getName()
164
    {
165
        return 'knp_rad.twig.datatable';
166
    }
167
168
}
169