Failed Conditions
Push — master ( c04808...f5c6c8 )
by Arnold
04:54
created

Input::getQueryParams()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

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