Passed
Push — master ( 6bc222...ffd20d )
by WEBEWEB
14:18
created

DataTablesWrapperHelper::getLanguageUrl()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 19
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 8
dl 0
loc 19
c 1
b 0
f 0
rs 10
cc 2
nc 2
nop 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\Helper;
13
14
use Symfony\Component\Filesystem\Exception\FileNotFoundException;
15
use WBW\Bundle\JQuery\DataTablesBundle\Api\DataTablesWrapperInterface;
16
use WBW\Bundle\JQuery\DataTablesBundle\Normalizer\DataTablesNormalizer;
17
use WBW\Bundle\JQuery\DataTablesBundle\WBWJQueryDataTablesBundle;
18
19
/**
20
 * DataTables wrapper helper.
21
 *
22
 * @author webeweb <https://github.com/webeweb>
23
 * @package WBW\Bundle\JQuery\DataTablesBundle\Helper
24
 */
25
class DataTablesWrapperHelper {
26
27
    /**
28
     * Get a language URL.
29
     *
30
     * @param string $language The language.
31
     * @return string Returns the language URL.
32
     * @throws FileNotFoundException Throws a file not found exception if the language file does not exist.
33
     */
34
    public static function getLanguageUrl(string $language): string {
35
36
        // Initialize the directory.
37
        $dir = (new WBWJQueryDataTablesBundle())->getPath();
38
        $dir .= "/Resources/public/datatables-i18n/%s.json";
39
40
        // Initialize the URI.
41
        $uri = "/bundles/wbwjquerydatatables/datatables-i18n/%s.json";
42
43
        // Initialize the URL.
44
        $url = sprintf($uri, $language);
45
46
        // Initialize and check the filename.
47
        $file = sprintf($dir, $language);
48
        if (false === file_exists($file)) {
49
            throw new FileNotFoundException(null, 500, null, $url);
50
        }
51
52
        return $url;
53
    }
54
55
    /**
56
     * Get a name.
57
     *
58
     * @param DataTablesWrapperInterface $dtWrapper The wrapper.
59
     * @return string Returns the name.
60
     */
61
    public static function getName(DataTablesWrapperInterface $dtWrapper): string {
62
        return "dt" . preg_replace("/[^A-Za-z0-9]/", "", $dtWrapper->getProvider()->getName());
63
    }
64
65
    /**
66
     * Get the options.
67
     *
68
     * @param DataTablesWrapperInterface $dtWrapper The wrapper.
69
     * @return array Returns the options.
70
     */
71
    public static function getOptions(DataTablesWrapperInterface $dtWrapper): array {
72
73
        $output = [];
74
75
        if (null !== $dtWrapper->getOptions()) {
76
            $output = $dtWrapper->getOptions()->getOptions();
77
        }
78
79
        $output["ajax"]["type"] = $dtWrapper->getMethod();
80
        $output["ajax"]["url"]  = $dtWrapper->getUrl();
81
        $output["columns"]      = [];
82
        $output["processing"]   = $dtWrapper->getProcessing();
83
        $output["serverSide"]   = $dtWrapper->getServerSide();
84
85
        foreach ($dtWrapper->getColumns() as $current) {
86
            $output["columns"][] = DataTablesNormalizer::normalizeColumn($current);
87
        }
88
89
        return $output;
90
    }
91
92
    /**
93
     * Determines if a wrapper contains a search.
94
     *
95
     * @param DataTablesWrapperInterface $dtWrapper The wrapper.
96
     * @return bool Returns true in case of success, false otherwise.
97
     */
98
    public static function hasSearch(DataTablesWrapperInterface $dtWrapper): bool {
99
        return null !== DataTablesRepositoryHelper::determineOperator($dtWrapper);
100
    }
101
}
102