Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
75.68% |
28 / 37 |
|
20.00% |
1 / 5 |
CRAP | |
0.00% |
0 / 1 |
Request | |
75.68% |
28 / 37 |
|
20.00% |
1 / 5 |
28.97 | |
0.00% |
0 / 1 |
getParam | |
80.00% |
8 / 10 |
|
0.00% |
0 / 1 |
6.29 | |||
getParams | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
6 | |||
getCookieParam | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
2 | |||
getBasePath | |
88.89% |
8 / 9 |
|
0.00% |
0 / 1 |
7.07 | |||
getBaseUrl | |
87.50% |
7 / 8 |
|
0.00% |
0 / 1 |
5.05 |
1 | <?php |
2 | |
3 | /** |
4 | * @copyright BerlinOnline Stadtportal GmbH & Co. KG |
5 | **/ |
6 | |
7 | declare(strict_types=1); |
8 | |
9 | namespace BO\Slim; |
10 | |
11 | class Request extends \Slim\Psr7\Request |
12 | { |
13 | /** |
14 | * Fetch request parameter value from body or query string (in that order). |
15 | * |
16 | * Note: This method is not part of the PSR-7 standard. |
17 | * |
18 | * @param string $key The parameter key. |
19 | * @param mixed $default The default value. |
20 | * |
21 | * @return mixed |
22 | */ |
23 | public function getParam(string $key, $default = null) |
24 | { |
25 | $postParams = $this->getParsedBody(); |
26 | $getParams = $this->getQueryParams(); |
27 | $result = $default; |
28 | if (is_array($postParams) && isset($postParams[$key])) { |
29 | $result = $postParams[$key]; |
30 | } elseif (is_object($postParams) && property_exists($postParams, $key)) { |
31 | $result = $postParams->$key; |
32 | } elseif (isset($getParams[$key])) { |
33 | $result = $getParams[$key]; |
34 | } |
35 | |
36 | return $result; |
37 | } |
38 | |
39 | /** |
40 | * @deprecated |
41 | * please use explicitly the query params or parsed request body params |
42 | * |
43 | * @return mixed[] |
44 | */ |
45 | public function getParams() |
46 | { |
47 | $params = $this->getQueryParams(); |
48 | $postParams = $this->getParsedBody(); |
49 | if ($postParams) { |
50 | $params = array_replace($params, (array)$postParams); |
51 | } |
52 | |
53 | return $params; |
54 | } |
55 | |
56 | /** |
57 | * Fetch cookie value from cookies sent by the client to the server. |
58 | * |
59 | * Note: This method is not part of the PSR-7 standard. |
60 | * |
61 | * @param string $key The attribute name. |
62 | * @param mixed $default Default value to return if the attribute does not exist. |
63 | * |
64 | * @return mixed |
65 | */ |
66 | public function getCookieParam($key, $default = null) |
67 | { |
68 | $cookies = $this->getCookieParams(); |
69 | $result = $default; |
70 | if (isset($cookies[$key])) { |
71 | $result = $cookies[$key]; |
72 | } |
73 | |
74 | return $result; |
75 | } |
76 | |
77 | /** |
78 | * @deprecated (use SlimApp::getBasePath() or resolve a named route instead) |
79 | * @return string |
80 | */ |
81 | public function getBasePath(): string |
82 | { |
83 | $basePath = getenv('ZMS_MODULE_BASEPATH') !== false ? getenv('ZMS_MODULE_BASEPATH') : ''; |
84 | if (empty($basePath)) { |
85 | $serverParams = $this->getServerParams(); |
86 | |
87 | if (!isset($serverParams['REQUEST_URI']) || !isset($serverParams['SCRIPT_NAME'])) { |
88 | return $basePath; |
89 | } |
90 | |
91 | while ( |
92 | min(strlen($serverParams['REQUEST_URI']), strlen($serverParams['SCRIPT_NAME'])) > strlen($basePath) |
93 | && strncmp($serverParams['REQUEST_URI'], $serverParams['SCRIPT_NAME'], strlen($basePath) + 1) === 0 |
94 | ) { |
95 | $basePath = substr($serverParams['REQUEST_URI'], 0, strlen($basePath) + 1); |
96 | } |
97 | } |
98 | |
99 | return rtrim($basePath, '/'); |
100 | } |
101 | |
102 | /** |
103 | * Return the fully qualified base URL. |
104 | * |
105 | * Note that this method never includes a trailing / |
106 | * |
107 | * This method is not part of PSR-7. |
108 | * |
109 | * @deprecated |
110 | * @return string |
111 | */ |
112 | public function getBaseUrl() |
113 | { |
114 | $scheme = $this->getUri()->getScheme(); |
115 | $authority = $this->getUri()->getAuthority(); |
116 | $basePath = $this->getBasePath(); |
117 | |
118 | if ($authority !== '' && substr($basePath, 0, 1) !== '/') { |
119 | $basePath = $basePath . '/' . $basePath; |
120 | } |
121 | |
122 | return ($scheme !== '' ? $scheme . ':' : '') |
123 | . ($authority ? '//' . $authority : '') |
124 | . rtrim($basePath, '/'); |
125 | } |
126 | } |