Passed
Pull Request — master (#355)
by Arman
02:49
created

Server::acceptedLang()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 0
dl 0
loc 11
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Quantum PHP Framework
5
 *
6
 * An open source software development framework for PHP
7
 *
8
 * @package Quantum
9
 * @author Arman Ag. <[email protected]>
10
 * @copyright Copyright (c) 2018 Softberg LLC (https://softberg.org)
11
 * @link http://quantum.softberg.org/
12
 * @since 2.9.9
13
 */
14
15
namespace Quantum\Environment;
16
17
/**
18
 * Class Server
19
 * @package Quantum\Environment
20
 */
21
class Server
22
{
23
24
    /**
25
     * @var array
26
     */
27
    private $server;
28
29
30
    private static $instance = null;
31
32
    /**
33
     * Server constructor.
34
     */
35
    private function __construct()
36
    {
37
        $this->server = $_SERVER ?? [];
38
    }
39
40
    /**
41
     * Get Instance
42
     * @return Server
43
     */
44
    public static function getInstance(): Server
45
    {
46
        if (self::$instance === null) {
47
            self::$instance = new self();
48
        }
49
50
        return self::$instance;
51
    }
52
53
    /**
54
     * Flushes the server params
55
     */
56
    public function flush()
57
    {
58
        $this->server = [];
59
    }
60
61
    /**
62
     * @return array
63
     */
64
    public function all(): array
65
    {
66
        return $this->server;
67
    }
68
69
    /**
70
     * @param $key
71
     * @return mixed|null
72
     */
73
    public function get($key)
74
    {
75
        return $this->server[$key] ?? null;
76
    }
77
78
    /**
79
     * @param $key
80
     * @return bool
81
     */
82
    public function has($key): bool
83
    {
84
        return array_key_exists($key, $this->server);
85
    }
86
87
    /**
88
     * @param $key
89
     * @param $value
90
     */
91
    public function set($key, $value)
92
    {
93
        $this->server[$key] = $value;
94
    }
95
96
    /**
97
     * @return string|null
98
     */
99
    public function uri(): ?string
100
    {
101
        return $this->get('REQUEST_URI');
102
    }
103
104
    /**
105
     * @return string|null
106
     */
107
    public function query(): ?string
108
    {
109
        return $this->get('QUERY_STRING');
110
    }
111
112
    /**
113
     * @return string|null
114
     */
115
    public function method(): ?string
116
    {
117
        return $this->get('REQUEST_METHOD');
118
    }
119
120
    /**
121
     * @return string|null
122
     */
123
    public function protocol(): ?string
124
    {
125
        $https = $this->get('HTTPS');
126
        $port = $this->get('SERVER_PORT');
127
128
        return (!empty($https) && strtolower($https) !== 'off') || $port == 443 ? 'https' : 'http';
129
    }
130
131
    /**
132
     * @return string|null
133
     */
134
    public function host(): ?string
135
    {
136
        return $this->get('SERVER_NAME');
137
    }
138
139
    /**
140
     * @return string|null
141
     */
142
    public function port(): ?string
143
    {
144
        return $this->get('SERVER_PORT');
145
    }
146
147
    /**
148
     * @param bool $exact
149
     * @return string|null
150
     */
151
    public function contentType(bool $exact = false): ?string
152
    {
153
        $contentType = $this->get('CONTENT_TYPE');
154
155
        if ($exact && $contentType && strpos($contentType, ';') !== false) {
156
            return trim(explode(';', $contentType, 2)[0]);
157
        }
158
159
        return $contentType;
160
    }
161
162
    /**
163
     * @return string|null
164
     */
165
    public function referrer(): ?string
166
    {
167
        return $this->get('HTTP_REFERER');
168
    }
169
170
    /**
171
     * @return bool
172
     */
173
    public function ajax(): bool
174
    {
175
        return strtolower($this->get('HTTP_X_REQUESTED_WITH') ?? '') === 'xmlhttprequest';
176
    }
177
178
    /**
179
     * @return string|null
180
     */
181
    public function ip(): ?string
182
    {
183
        return $this->get('HTTP_CLIENT_IP')
184
            ?? $this->get('HTTP_X_FORWARDED_FOR')
185
            ?? $this->get('REMOTE_ADDR');
186
    }
187
188
    /**
189
     * @return array
190
     */
191
    function getAllHeaders(): array
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
192
    {
193
        $data = $this->all();
194
195
        if (empty($data)) {
196
            return [];
197
        }
198
199
        return array_reduce(array_keys($data), function ($headers, $key) use ($data) {
200
            if (strpos($key, 'HTTP_') === 0) {
201
                $formattedKey = strtolower(str_replace('_', '-', substr($key, 5)));
202
                $headers[$formattedKey] = $data[$key];
203
            }
204
            return $headers;
205
        }, []);
206
    }
207
208
    /**
209
     * @return string|null
210
     */
211
    public function acceptedLang(): ?string
212
    {
213
        $accept = $this->get('HTTP_ACCEPT_LANGUAGE');
214
215
        if (!$accept) {
216
            return null;
217
        }
218
219
        $first = explode(',', $accept)[0];
220
221
        return strtolower(substr(trim($first), 0, 2));
222
    }
223
}