Passed
Pull Request — master (#182)
by Arman
05:01 queued 02:11
created

Server::query()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
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.5
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|null
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
     * @return array
55
     */
56
    public function all(): array
57
    {
58
        return $this->server;
59
    }
60
61
    /**
62
     * @param $key
63
     * @return mixed|null
64
     */
65
    public function get($key)
66
    {
67
        return $this->server[$key] ?? null;
68
    }
69
70
    /**
71
     * @param $key
72
     * @param $value
73
     * @return void
74
     */
75
    public function set($key, $value): void
76
    {
77
        $this->server[$key] = $value;
78
    }
79
80
    /**
81
     * @return string|null
82
     */
83
    public function uri(): ?string
84
    {
85
        return $this->get('REQUEST_URI');
86
    }
87
88
    /**
89
     * @return string|null
90
     */
91
    public function query(): ?string
92
    {
93
        return $this->get('QUERY_STRING');
94
    }
95
96
    /**
97
     * @return string|null
98
     */
99
    public function method(): ?string
100
    {
101
        return $this->get('REQUEST_METHOD');
102
    }
103
104
    /**
105
     * @return string|null
106
     */
107
    public function protocol(): ?string
108
    {
109
        $https = $this->get('HTTPS');
110
        $port = $this->get('SERVER_PORT');
111
112
        return (!empty($https) && strtolower($https) !== 'off') || $port == 443 ? 'https' : 'http';
113
    }
114
115
    /**
116
     * @return string|null
117
     */
118
    public function host(): ?string
119
    {
120
        return $this->get('SERVER_NAME');
121
    }
122
123
    /**
124
     * @return string|null
125
     */
126
    public function port(): ?string
127
    {
128
        return $this->get('SERVER_PORT');
129
    }
130
131
    /**
132
     * @param bool $exact
133
     * @return string|null
134
     */
135
    public function contentType(bool $exact = false): ?string
136
    {
137
        $contentType = $this->get('CONTENT_TYPE');
138
        if ($exact && $contentType && strpos($contentType, ';') !== false) {
139
            return trim(explode(';', $contentType, 2)[0]);
140
        }
141
        return $contentType;
142
    }
143
144
    /**
145
     * @return string|null
146
     */
147
    public function referrer(): ?string
148
    {
149
        return $this->get('HTTP_REFERER');
150
    }
151
152
    /**
153
     * @return bool
154
     */
155
    public function ajax(): bool
156
    {
157
        return strtolower($this->get('HTTP_X_REQUESTED_WITH') ?? '') === 'xmlhttprequest';
158
    }
159
160
}
161