Completed
Push — master ( 207a43...23e505 )
by WEBEWEB
01:31
created

renderDataTablesRow()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 21
rs 9.584
c 0
b 0
f 0
cc 4
nc 3
nop 2
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\Environment;
16
use WBW\Bundle\CoreBundle\Service\TwigEnvironmentTrait;
17
use WBW\Bundle\CoreBundle\Twig\Extension\AbstractTwigExtension;
18
use WBW\Bundle\CoreBundle\Twig\Extension\RendererTwigExtension;
19
use WBW\Bundle\CoreBundle\Twig\Extension\RendererTwigExtensionTrait;
20
use WBW\Bundle\JQuery\DataTablesBundle\API\DataTablesColumnInterface;
21
use WBW\Bundle\JQuery\DataTablesBundle\API\DataTablesWrapperInterface;
22
use WBW\Bundle\JQuery\DataTablesBundle\Helper\DataTablesWrapperHelper;
23
use WBW\Library\Core\Argument\StringHelper;
24
25
/**
26
 * Abstract DataTables Twig extension.
27
 *
28
 * @author webeweb <https://github.com/webeweb/>
29
 * @package WBW\Bundle\JQuery\DataTablesBundle\Twig\Extension
30
 * @abstract
31
 */
32
abstract class AbstractDataTablesTwigExtension extends AbstractTwigExtension {
33
34
    use RendererTwigExtensionTrait;
35
    use TwigEnvironmentTrait;
36
37
    /**
38
     * jQuery DataTables.
39
     *
40
     * @var string
41
     */
42
    const JQUERY_DATATABLES = <<< EOT
43
    $(document).ready(function () {
44
        var %var% = $("%selector%").DataTable(%options%);
45
    });
46
EOT;
47
48
    /**
49
     * jQuery DataTables.
50
     *
51
     * @var string
52
     */
53
    const JQUERY_DATATABLES_STANDALONE = <<< EOT
54
    $(document).ready(function () {
55
        $("%selector%").DataTable(%options%);
56
    });
57
EOT;
58
59
    /**
60
     * Constructor.
61
     *
62
     * @param Environment $twigEnvironment The Twig environment.
63
     * @param RendererTwigExtension $rendererTwigExtension The renderer Twig extension.
64
     */
65
    public function __construct(Environment $twigEnvironment, RendererTwigExtension $rendererTwigExtension) {
66
        parent::__construct($twigEnvironment);
0 ignored issues
show
Compatibility introduced by
$twigEnvironment of type object<Twig\Environment> is not a sub-type of object<Twig_Environment>. It seems like you assume a child class of the class Twig\Environment to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
67
        $this->setRendererTwigExtension($rendererTwigExtension);
68
    }
69
70
    /**
71
     * Encode the options.
72
     *
73
     * @param array $options The options.
74
     * @return string Returns the encoded options.
75
     */
76
    protected function encodeOptions(array $options) {
77
        if (0 === count($options)) {
78
            return "{}";
79
        }
80
        ksort($options);
81
        $output = json_encode($options, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
82
        return str_replace("\n", "\n        ", $output);
83
    }
84
85
    /**
86
     * Displays a jQuery DataTables.
87
     *
88
     * @param DataTablesWrapperInterface $dtWrapper The wrapper.
89
     * @param string $selector The selector.
90
     * @param string $language The language.
91
     * @return string Returns the jQuery DataTables.
92
     * @throws FileNotFoundException Throws a file not found exception if the language file does not exist.
93
     */
94
    protected function jQueryDataTables(DataTablesWrapperInterface $dtWrapper, $selector, $language) {
95
96
        $dtOptions = DataTablesWrapperHelper::getOptions($dtWrapper);
97
98
        $var     = DataTablesWrapperHelper::getName($dtWrapper);
99
        $options = $dtOptions;
100
        if (null !== $language) {
101
            $options["language"] = ["url" => DataTablesWrapperHelper::getLanguageURL($language)];
102
        }
103
104
        $searches = ["%var%", "%selector%", "%options%"];
105
        $replaces = [$var, null === $selector ? "#" . $var : $selector, $this->encodeOptions($options)];
106
107
        $javascript = StringHelper::replace(self::JQUERY_DATATABLES, $searches, $replaces);
108
        return $this->getRendererTwigExtension()->coreScriptFilter($javascript);
109
    }
110
111
    /**
112
     * Displays a jQuery DataTables "standalone".
113
     *
114
     * @param string $selector The selector.
115
     * @param string $language The language.
116
     * @param array $options The options.
117
     * @return string Returns the jQuery DataTables "Standalone".
118
     * @throws FileNotFoundException Throws a file not found exception if the language file does not exist.
119
     */
120
    protected function jQueryDataTablesStandalone($selector, $language, array $options) {
121
122
        if (null !== $language) {
123
            $options["language"] = ["url" => DataTablesWrapperHelper::getLanguageURL($language)];
124
        }
125
126
        $searches = ["%selector%", "%options%"];
127
        $replaces = [$selector, $this->encodeOptions($options)];
128
129
        $javascript = StringHelper::replace(self::JQUERY_DATATABLES_STANDALONE, $searches, $replaces);
130
        return $this->getRendererTwigExtension()->coreScriptFilter($javascript);
131
    }
132
133
    /**
134
     * Render a DataTables.
135
     *
136
     * @param DataTablesWrapperInterface $dtWrapper The wrapper.
137
     * @param string $class The class.
138
     * @param boolean $includeTHead Include thead ?
139
     * @param boolean $includeTFoot Include tfoot ?
140
     * @return string Returns the rendered DataTables.
141
     */
142
    protected function renderDataTables(DataTablesWrapperInterface $dtWrapper, $class, $includeTHead, $includeTFoot) {
143
144
        $attributes = [];
145
146
        $attributes["class"] = ["table", $class];
147
        $attributes["id"]    = DataTablesWrapperHelper::getName($dtWrapper);
148
149
        $thead = true === $includeTHead ? $this->renderDataTablesRow($dtWrapper, "thead") . "\n" : "";
150
        $tfoot = true === $includeTFoot ? $this->renderDataTablesRow($dtWrapper, "tfoot") . "\n" : "";
151
152
        $inner = "\n" . $thead . $tfoot;
153
154
        return static::coreHTMLElement("table", $inner, $attributes);
155
    }
156
157
    /**
158
     * Render a column.
159
     *
160
     * @param DataTablesColumnInterface $dtColumn The column.
161
     * @param bool $rowScope Row scope ?
162
     * @return string Returns the rendered column.
163
     */
164
    private function renderDataTablesColumn(DataTablesColumnInterface $dtColumn, $rowScope = false) {
165
166
        $attributes = [];
167
168
        $attributes["scope"] = true === $rowScope ? "row" : null;
169
        $attributes["class"] = $dtColumn->getClassname();
170
        $attributes["width"] = $dtColumn->getWidth();
171
172
        return static::coreHTMLElement("th", $dtColumn->getTitle(), $attributes);
173
    }
174
175
    /**
176
     * Render a row.
177
     *
178
     * @param DataTablesWrapperInterface $dtWrapper The wrapper.
179
     * @param string $wrapper The wrapper (thead or tfoot)
180
     * @return string Returns the rendered row.
181
     */
182
    private function renderDataTablesRow(DataTablesWrapperInterface $dtWrapper, $wrapper) {
183
184
        $innerHTML = "";
185
186
        $count = count($dtWrapper->getColumns());
187
        for ($i = 0; $i < $count; ++$i) {
188
189
            $dtColumn = array_values($dtWrapper->getColumns())[$i];
190
191
            $th = $this->renderDataTablesColumn($dtColumn, ("thead" === $wrapper && 0 === $i));
192
            if ("" === $th) {
193
                continue;
194
            }
195
196
            $innerHTML .= $th . "\n";
197
        }
198
199
        $tr = static::coreHTMLElement("tr", "\n" . $innerHTML);
200
201
        return static::coreHTMLElement($wrapper, "\n" . $tr . "\n");
202
    }
203
}
204