1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
|
<?php
namespace Jasny\Controller;
use Psr\Http\Message\ServerRequestInterface;
/**
* Methods for a controller to read from the request
*/
trait Input
{
/**
* Get request, set for controller
*
* @return ServerRequestInterface
*/
abstract public function getRequest();
/**
* Get the request query parameters.
*
* <code>
* // Get all parameters
* $params = $this->getQueryParams();
*
* // Get specific parameters, specifying defaults for 'bar' and 'zoo'
* list($foo, $bar, $zoo) = $this->getQueryParams(['foo', 'bar' => 10, 'zoo' => 'monkey']);
* </code>
*
* @param array $list
* @return array
*/
public function getQueryParams(array $list = null)
{
return isset($list)
? $this->listQueryParams($list)
: (array)$this->getRequest()->getQueryParams();
}
/**
* Apply list to query params
*
* @param array $list
* @return array
*/
protected function listQueryParams(array $list)
{
$result = [];
$params = $this->getRequest()->getQueryParams();
foreach ($list as $key => $value) {
if (is_int($key)) {
$key = $value;
$value = null;
}
$result[] = isset($params[$key]) ? $params[$key] : $value;
}
return $result;
}
/**
* Check if the request has a query parameter
*
* @param array $param
* @return boolean
*/
public function hasQueryParam($param)
{
$params = $this->getQueryParams();
return isset($params[$param]);
}
/**
* Get a query parameter.
*
* Optionally apply filtering to the value.
* @link http://php.net/manual/en/filter.filters.php
*
* @param array $param
* @param string $default
* @param int $filter
* @param mixed $filterOptions
* @return mixed
*/
public function getQueryParam($param, $default = null, $filter = null, $filterOptions = null)
{
$params = $this->getQueryParams();
$value = isset($params[$param]) ? $params[$param] : $default;
if (isset($filter) && isset($value)) {
$value = filter_var($value, $filter, $filterOptions);
}
return $value;
}
/**
* Get parsed body and uploaded files as input
*
* @return array|mixed
*/
public function getInput()
{
$data = $this->getRequest()->getParsedBody();
if (is_array($data)) {
$files = $this->getRequest()->getUploadedFiles();
$data = array_replace_recursive($data, (array)$files);
}
return $data;
}
}
|