DataTablesTwigExtension   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 99
Duplicated Lines 0 %

Importance

Changes 4
Bugs 1 Features 0
Metric Value
wmc 7
eloc 21
c 4
b 1
f 0
dl 0
loc 99
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A jQueryDataTablesOptionsFunction() 0 2 1
A jQueryDataTablesFunction() 0 2 1
A jQueryDataTablesStandaloneFunction() 0 2 1
A renderDataTablesFunction() 0 2 1
A getFunctions() 0 17 1
A jQueryDataTablesNameFunction() 0 2 1
A getFilters() 0 5 1
1
<?php
2
3
/*
4
 * This file is part of the jquery-datatables-bundle package.
5
 *
6
 * (c) 2018 WEBEWEB
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace WBW\Bundle\JQuery\DataTablesBundle\Twig\Extension;
13
14
use Symfony\Component\Filesystem\Exception\FileNotFoundException;
15
use Twig\TwigFilter;
16
use Twig\TwigFunction;
17
use WBW\Bundle\JQuery\DataTablesBundle\Api\DataTablesWrapperInterface;
18
use WBW\Bundle\JQuery\DataTablesBundle\Helper\DataTablesWrapperHelper;
19
use WBW\Library\Types\Helper\ArrayHelper;
20
21
/**
22
 * DataTables Twig extension.
23
 *
24
 * @author webeweb <https://github.com/webeweb>
25
 * @package WBW\Bundle\JQuery\DataTablesBundle\Twig\Extension
26
 */
27
class DataTablesTwigExtension extends AbstractDataTablesTwigExtension {
28
29
    /**
30
     * Service name.
31
     *
32
     * @var string
33
     */
34
    const SERVICE_NAME = "wbw.jquery.datatables.twig.extension";
35
36
    /**
37
     * Get the Twig filters.
38
     *
39
     * @return TwigFilter[] Returns the Twig filters.
40
     */
41
    public function getFilters(): array {
42
43
        return [
44
            new TwigFilter("jQueryDataTablesName", [$this, "jQueryDataTablesNameFunction"], ["is_safe" => ["html"]]),
45
            new TwigFilter("jQueryDTName", [$this, "jQueryDataTablesNameFunction"], ["is_safe" => ["html"]]),
46
        ];
47
    }
48
49
    /**
50
     * Get the Twig functions.
51
     *
52
     * @return TwigFunction[] Returns the Twig functions.
53
     */
54
    public function getFunctions(): array {
55
56
        return [
57
            new TwigFunction("jQueryDataTables", [$this, "jQueryDataTablesFunction"], ["is_safe" => ["html"]]),
58
            new TwigFunction("jQueryDT", [$this, "jQueryDataTablesFunction"], ["is_safe" => ["html"]]),
59
60
            new TwigFunction("jQueryDataTablesName", [$this, "jQueryDataTablesNameFunction"], ["is_safe" => ["html"]]),
61
            new TwigFunction("jQueryDTName", [$this, "jQueryDataTablesNameFunction"], ["is_safe" => ["html"]]),
62
63
            new TwigFunction("jQueryDataTablesOptions", [$this, "jQueryDataTablesOptionsFunction"], ["is_safe" => ["html"]]),
64
            new TwigFunction("jQueryDTOptions", [$this, "jQueryDataTablesOptionsFunction"], ["is_safe" => ["html"]]),
65
66
            new TwigFunction("jQueryDataTablesStandalone", [$this, "jQueryDataTablesStandaloneFunction"], ["is_safe" => ["html"]]),
67
            new TwigFunction("jQueryDTStandalone", [$this, "jQueryDataTablesStandaloneFunction"], ["is_safe" => ["html"]]),
68
69
            new TwigFunction("renderDataTables", [$this, "renderDataTablesFunction"], ["is_safe" => ["html"]]),
70
            new TwigFunction("renderDT", [$this, "renderDataTablesFunction"], ["is_safe" => ["html"]]),
71
        ];
72
    }
73
74
    /**
75
     * Display a jQuery DataTables.
76
     *
77
     * @param DataTablesWrapperInterface $dtWrapper The wrapper.
78
     * @param array<string,mixed> $args The arguments.
79
     * @return string Returns the jQuery DataTables.
80
     * @throws FileNotFoundException Throws a file not found exception if the language file does not exist.
81
     */
82
    public function jQueryDataTablesFunction(DataTablesWrapperInterface $dtWrapper, array $args = []): string {
83
        return $this->jQueryDataTables($dtWrapper, ArrayHelper::get($args, "selector"), ArrayHelper::get($args, "language"));
84
    }
85
86
    /**
87
     * Display a jQuery DataTables name.
88
     *
89
     * @param DataTablesWrapperInterface $dtWrapper The wrapper.
90
     * @return string Returns the jQuery DataTables name.
91
     */
92
    public function jQueryDataTablesNameFunction(DataTablesWrapperInterface $dtWrapper): string {
93
        return DataTablesWrapperHelper::getName($dtWrapper);
94
    }
95
96
    /**
97
     * Display a jQuery DataTables options.
98
     *
99
     * @param DataTablesWrapperInterface $dtWrapper The wrapper.
100
     * @return array<string,mixed> Returns the jQuery DataTables options.
101
     */
102
    public function jQueryDataTablesOptionsFunction(DataTablesWrapperInterface $dtWrapper): array {
103
        return DataTablesWrapperHelper::getOptions($dtWrapper);
104
    }
105
106
    /**
107
     * Display a jQuery DataTables "Standalone".
108
     *
109
     * @param array<string,mixed> $args The arguments.
110
     * @return string Returns the jQuery DataTables "Standalone".
111
     * @throws FileNotFoundException Throws a file not found exception if the language file does not exist.
112
     */
113
    public function jQueryDataTablesStandaloneFunction(array $args = []): string {
114
        return $this->jQueryDataTablesStandalone(ArrayHelper::get($args, "selector", ".table"), ArrayHelper::get($args, "language"), ArrayHelper::get($args, "options", []));
115
    }
116
117
    /**
118
     * Render a DataTables.
119
     *
120
     * @param DataTablesWrapperInterface $dtWrapper The wrapper.
121
     * @param array<string,mixed> $args The arguments.
122
     * @return string Returns the rendered DataTables.
123
     */
124
    public function renderDataTablesFunction(DataTablesWrapperInterface $dtWrapper, array $args = []): string {
125
        return $this->renderDataTables($dtWrapper, ArrayHelper::get($args, "class"), ArrayHelper::get($args, "thead", true), ArrayHelper::get($args, "tfoot", true));
126
    }
127
}
128