1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* This file is part of the Scrawler package. |
4
|
|
|
* |
5
|
|
|
* (c) Pranjal Pandey <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
declare(strict_types=1); |
12
|
|
|
|
13
|
|
|
namespace Scrawler\Http; |
14
|
|
|
|
15
|
|
|
use Exception; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Request class adds magic to the Symfony request. |
19
|
|
|
*/ |
20
|
|
|
class Request extends \Symfony\Component\HttpFoundation\Request |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* Method to get varaiable sent with request |
24
|
|
|
* First checks in $request->request then in $request->query and then in $request->getContent(). |
25
|
|
|
*/ |
26
|
|
|
#[\Override] |
27
|
|
|
public function get(string $key, mixed $default = null): ?string |
28
|
|
|
{ |
29
|
|
|
$value = $this->request->get($key); |
30
|
|
|
if (is_null($value)) { |
31
|
|
|
$value = $this->query->get($key); |
32
|
|
|
} |
33
|
|
|
if (is_null($value)) { |
34
|
|
|
$value = $this->getContentValue($key); |
35
|
|
|
} |
36
|
|
|
if (is_null($value) || '' == $value) { |
37
|
|
|
return $default; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
return $value; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Get value from $request->getContent(). |
45
|
|
|
* |
46
|
|
|
* @return mixed|null |
47
|
|
|
*/ |
48
|
|
|
private function getContentValue(string $key): mixed |
49
|
|
|
{ |
50
|
|
|
if ($this->getContent()) { |
51
|
|
|
try{ |
52
|
|
|
return \Safe\json_decode($this->getContent())->$key; |
|
|
|
|
53
|
|
|
}catch(Exception $e){ |
54
|
|
|
return null; |
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
return null; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Magic method to get property of request. |
63
|
|
|
*/ |
64
|
|
|
public function __get(string $key): ?string |
65
|
|
|
{ |
66
|
|
|
return $this->get($key); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Get all property of request |
71
|
|
|
* returns array of all from $request->request, $request->query and $request->getContent(). |
72
|
|
|
* |
73
|
|
|
* @return array<string,mixed> |
74
|
|
|
*/ |
75
|
|
|
public function all(): array |
76
|
|
|
{ |
77
|
|
|
if ($this->getContent()) { |
78
|
|
|
try{ |
79
|
|
|
return array_merge($this->request->all(), $this->query->all(), \Safe\json_decode($this->getContent(), true)); |
|
|
|
|
80
|
|
|
}catch(Exception $e){ |
81
|
|
|
return array_merge($this->request->all(), $this->query->all()); |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
return array_merge($this->request->all(), $this->query->all()); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Check id request has key in $request->request, $request->query and $request->getContent(). |
90
|
|
|
*/ |
91
|
|
|
public function has(string $key): bool |
92
|
|
|
{ |
93
|
|
|
if ($this->getContent()) { |
94
|
|
|
try{ |
95
|
|
|
return isset(\Safe\json_decode($this->getContent())->$key) || $this->request->has($key) || $this->query->has($key); |
|
|
|
|
96
|
|
|
}catch(Exception $e){ |
97
|
|
|
return $this->request->has($key) || $this->query->has($key); |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
return $this->request->has($key) || $this->query->has($key); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Get request url or generate url from path. |
106
|
|
|
*/ |
107
|
|
|
public function url(?string $path = null): string |
108
|
|
|
{ |
109
|
|
|
if (is_null($path)) { |
110
|
|
|
return $this->getSchemeAndHttpHost().$this->getBaseUrl().$this->getPathInfo(); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
return $this->getSchemeAndHttpHost().$this->getBasePath().$path; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Check if current path is same as given path. |
118
|
|
|
*/ |
119
|
|
|
public function is(string $path): bool |
120
|
|
|
{ |
121
|
|
|
return $this->getPathInfo() === $path; |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|