DataTablesExportHelper   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
wmc 5
eloc 12
c 2
b 1
f 0
dl 0
loc 40
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A convert() 0 9 3
A isWindows() 0 13 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\Helper;
13
14
use DeviceDetector\DeviceDetector;
15
use DeviceDetector\Yaml\Symfony as SYP;
16
use Symfony\Component\HttpFoundation\Request;
17
use Throwable;
18
19
/**
20
 * DataTables export helper.
21
 *
22
 * @author webeweb <https://github.com/webeweb>
23
 * @package WBW\Bundle\JQuery\DataTablesBundle\Helper
24
 */
25
class DataTablesExportHelper {
26
27
    /**
28
     * Convert.
29
     *
30
     * @param string[] $values The values.
31
     * @param bool $windows Windows ?
32
     * @return string[] Returns the converted values.
33
     */
34
    public static function convert(array $values, bool $windows = false): array {
35
36
        if (true === $windows) {
37
            for ($i = count($values) - 1; 0 <= $i; --$i) {
38
                $values[$i] = utf8_decode($values[$i]);
39
            }
40
        }
41
42
        return $values;
43
    }
44
45
    /**
46
     * Determine if the operating system is windows.
47
     *
48
     * @param Request $request The request.
49
     * @return bool Returns true in case of success, false otherwise.
50
     * @throws Throwable Throws an exception if an error occurs.
51
     */
52
    public static function isWindows(Request $request): bool {
53
54
        if (false === $request->headers->has("user-agent")) {
55
            return false;
56
        }
57
58
        $dd = new DeviceDetector($request->headers->get("user-agent"));
59
        $dd->setYamlParser(new SYP());
60
        $dd->parse();
61
62
        $os = $dd->getOs("name");
63
64
        return 1 === preg_match("/Windows/", $os);
65
    }
66
}
67