Passed
Pull Request — master (#2)
by
unknown
15:51
created

BaseHelper::detectClientShortLanguage()   A

Complexity

Conditions 6
Paths 7

Size

Total Lines 22
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 11
nc 7
nop 0
dl 0
loc 22
c 0
b 0
f 0
cc 6
rs 9.2222
1
<?php
2
3
namespace app\helpers;
4
5
use Yii;
6
use yii\web\Request;
7
use yii\web\BadRequestHttpException;
8
use Itstructure\AdminModule\models\Language;
9
10
/**
11
 * Class BaseHelper
12
 *
13
 * @package app\helpers
14
 */
15
class BaseHelper
16
{
17
    /**
18
     * @param string $date
19
     * @param string $format
20
     *
21
     * @return string
22
     */
23
    public static function getDateAt(string $date, string $format = 'd.m.Y H:i:s'): string
24
    {
25
        $inDateTime = new \DateTime($date);
26
27
        return date($format, $inDateTime->getTimestamp());
28
    }
29
30
    /**
31
     * @param string $newShortLanguage
32
     * @param Request $request
33
     *
34
     * @return string
35
     */
36
    public static function getSwitchLanguageLink(string $newShortLanguage, Request $request): string
37
    {
38
        $url = trim($request->url, '/');
39
40
        $urlArray = explode('/', $url);
41
42
        $urlArray[0] = $newShortLanguage;
43
44
        return '/' . implode('/', $urlArray);
45
    }
46
47
    /**
48
     * @param string|null $shortLanguage
49
     *
50
     * @throws BadRequestHttpException
51
     */
52
    public static function setAppLanguage(string $shortLanguage = null)
53
    {
54
        if (empty($shortLanguage)) {
55
            throw new BadRequestHttpException('Parameter "shortLanguage" is not defined.');
56
        }
57
58
        $langModel = Language::find()->where([
59
            'shortName' => $shortLanguage
60
        ])->one();
61
62
        if (null === $langModel) {
63
            throw new BadRequestHttpException('There are not any data for language: '.$shortLanguage);
64
        }
65
66
        Yii::$app->language = $langModel->locale;
67
    }
68
69
    /**
70
     * Detect client (browser) short language.
71
     *
72
     * @return mixed
73
     */
74
    public static function detectClientShortLanguage()
75
    {
76
        if (!isset($_SERVER["HTTP_ACCEPT_LANGUAGE"]) || empty($_SERVER["HTTP_ACCEPT_LANGUAGE"])) {
77
            return null;
78
        }
79
80
        preg_match_all('/([a-z]{1,8}(?:-[a-z]{1,8})?)(?:;q=([0-9.]+))?/',
81
            strtolower($_SERVER["HTTP_ACCEPT_LANGUAGE"]),
82
            $matches
83
        );
84
85
        $langs = array_combine($matches[1], $matches[2]);
86
87
        foreach ($langs as $n => $v) {
88
            $langs[$n] = $v ? $v : 1; // If no q, then set q = 1
89
        }
90
91
        arsort($langs); // Sort descending q
92
93
        $language = key($langs);
94
95
        return strlen($language) > 2 ? substr($language, 0, 2) : $language;
96
    }
97
98
    /**
99
     * @return string
100
     */
101
    public static function ip_address()
102
    {
103
        $fields = array('HTTP_CLIENT_IP', 'HTTP_X_REAL_IP', 'HTTP_X_FORWARDED_FOR', 'REMOTE_ADDR');
104
105
        $ip = "0.0.0.0";
106
        foreach($fields as $k)
107
        {
108
            if(!empty($_SERVER[$k]) && ip2long($_SERVER[$k]) != false)
0 ignored issues
show
Bug Best Practice introduced by
It seems like you are loosely comparing ip2long($_SERVER[$k]) of type integer to the boolean false. If you are specifically checking for non-zero, consider using something more explicit like > 0 or !== 0 instead.
Loading history...
109
            {
110
                $ip = $_SERVER[$k];
111
            }
112
        }
113
114
        return $ip;
115
    }
116
}
117