Request::getMethod()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php 
2
3
namespace Fastpress\Http;
4
5
class Request implements \ArrayAccess {
6
    protected $get     = [];
7
    protected $post    = [];
8
    protected $server  = [];
9
    protected $cookies = [];
10
11
    public function __construct(
12
        array $_get, 
13
        array $_post, 
14
        array $_server, 
15
        array $_cookies
16
    ){
17
        $this->get      = $_get;
18
        $this->post     = $_post; 
19
        $this->server   = $_server;
20
        $this->cookies  = $_cookies;
21
    }
22
23
    public function isGet(): bool {
24
        return $this->getMethod() === 'GET'; 
25
    }
26
    
27
    public function isPost(): bool {
28
        return $this->getMethod() === 'POST'; 
29
    }
30
    
31
    public function isPut(): bool {
32
        return $this->getMethod() === 'PUT'; 
33
    }
34
    
35
    public function isDelete(): bool {
36
        return $this->getMethod() === 'DELETE'; 
37
    }
38
39
    public function get(string $var, $filter = null) 
40
    {
41
        return $this->filter($this->get, $var, $filter);
42
    }
43
44
    public function post(string $var, $filter = null) {
45
        return $this->filter($this->post, $var, $filter);
46
    }
47
48
    public function server(string $var, $filter = null) {
49
        return $this->filter($this->server, $var, $filter);
50
    }
51
    
52
    public function getUri(): string {
53
        return $this->filter($this->server, 'REQUEST_URI');
54
    }
55
    
56
    public function getReferer(): string {
57
        return $this->filter($this->server, 'HTTP_REFERER');
58
    }
59
60
    public function getMethod(): string {
61
        return $this->filter($this->server, 'REQUEST_METHOD');
62
    }
63
64
    public function isSecure(): bool{
65
       return (array_key_exists('HTTPS', $this->server)
66
            && $this->server['HTTPS'] !== 'off'
67
        );
68
    }
69
70
    public function isXhr(): bool {
71
        return $this->filter($this->server, 'HTTP_X_REQUESTED_WITH') === 'XMLHttpRequest';
72
    }
73
74
    protected function filter(array $input, string $var,  $filter = null){
75
        $value = $input[$var] ?? false; 
76
        
77
        if (!$filter) {
78
            return $value;  
79
        }
80
        
81
        return filter_var($value, $filter);
82
    }
83
    
84
    public function requestGlobals(): array {
85
        return [
86
            'get' => $this->get, 
87
            'post' => $this->post, 
88
            'server' => $this->server
89
        ];
90
    }
91
92
    public function build_url(){
93
        return  parse_url(
94
            $this->server('REQUEST_SCHEME') . '://' .
95
            $this->server('SERVER_NAME') . 
96
            $this->server('REQUEST_URI')
97
        );
98
    }
99
100
    public function offsetGet($offset){
101
        if(in_array($offset, ['isGet', 'isPut', 'isPost', 'isDelete', 'isXhr', 'isSecure'])){
102
            return $this->$offset(); 
103
        }
104
    }
105
106
    public function offsetSet($offset, $value){
107
        $this->set($offset, $value);
0 ignored issues
show
Bug introduced by
The method set() does not seem to exist on object<Fastpress\Http\Request>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
108
    }
109
    
110
    // \ArrayAccess methods, unused for now 
111
    public function offsetExists($offset){}
112
    public function offsetUnset($offset){}
113
}
114
115
116
117