Skip to content

Commit 9332c96

Browse files
committed
Add helper methods for getting individual request params
1 parent b708bc9 commit 9332c96

File tree

2 files changed

+61
-2
lines changed

2 files changed

+61
-2
lines changed

src/Controller.php

+59
Original file line numberDiff line numberDiff line change
@@ -123,4 +123,63 @@ public function __invoke(ServerRequestInterface $request, ResponseInterface $res
123123
throw new ActionForMethodNotFound($request->getMethod());
124124
}
125125
}
126+
127+
/**
128+
* Return a param from a parsed body.
129+
*
130+
* This method is NULL or object safe - it will check for body type, and do it's best to return a value without
131+
* breaking or throwing a warning.
132+
*
133+
* @param ServerRequestInterface $request
134+
* @param $param_name
135+
* @param null $default
136+
* @return mixed|null
137+
*/
138+
protected function getParsedBodyParam(ServerRequestInterface $request, $param_name, $default = null)
139+
{
140+
$parsed_body = $request->getParsedBody();
141+
142+
if ($parsed_body) {
143+
if (is_array($parsed_body) && array_key_exists($param_name, $parsed_body)) {
144+
return $parsed_body[$param_name];
145+
} elseif (is_object($parsed_body) && property_exists($parsed_body, $param_name)) {
146+
return $parsed_body->$param_name;
147+
}
148+
}
149+
150+
return $default;
151+
}
152+
153+
/**
154+
* @param ServerRequestInterface $request
155+
* @param string $param_name
156+
* @param mixed $default
157+
* @return mixed
158+
*/
159+
protected function getCookieParam(ServerRequestInterface $request, $param_name, $default = null)
160+
{
161+
return array_key_exists($param_name, $request->getCookieParams()) ? $request->getCookieParams()[$param_name] : $default;
162+
}
163+
164+
/**
165+
* @param ServerRequestInterface $request
166+
* @param string $param_name
167+
* @param mixed $default
168+
* @return mixed
169+
*/
170+
protected function getQueryParam(ServerRequestInterface $request, $param_name, $default = null)
171+
{
172+
return array_key_exists($param_name, $request->getQueryParams()) ? $request->getQueryParams()[$param_name] : $default;
173+
}
174+
175+
/**
176+
* @param ServerRequestInterface $request
177+
* @param string $param_name
178+
* @param mixed $default
179+
* @return mixed
180+
*/
181+
protected function getServerParam(ServerRequestInterface $request, $param_name, $default = null)
182+
{
183+
return array_key_exists($param_name, $request->getServerParams()) ? $request->getServerParams()[$param_name] : $default;
184+
}
126185
}

src/ResultEncoder/ResultEncoder.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -118,12 +118,12 @@ protected function encodeArray(array $action_result, ResponseInterface $response
118118
/**
119119
* Encode scalar value, with status 200.
120120
*
121-
* @param array $action_result
121+
* @param mixed $action_result
122122
* @param ResponseInterface $response
123123
* @param int $status
124124
* @return ResponseInterface
125125
*/
126-
protected function encodeScalar(array $action_result, ResponseInterface $response, $status = 200)
126+
protected function encodeScalar($action_result, ResponseInterface $response, $status = 200)
127127
{
128128
return $response->write(json_encode($action_result))->withStatus($status);
129129
}

0 commit comments

Comments
 (0)