RequestParser::processArrayParameter()   A
last analyzed

Complexity

Conditions 5
Paths 5

Size

Total Lines 26
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
eloc 12
dl 0
loc 26
ccs 0
cts 16
cp 0
rs 9.5555
c 0
b 0
f 0
cc 5
nc 5
nop 2
crap 30
1
<?php
2
3
/**
4
 * AppserverIo\RestApi\Parsers\OA2\RequestParser
5
 *
6
 * NOTICE OF LICENSE
7
 *
8
 * This source file is subject to the Open Software License (OSL 3.0)
9
 * that is available through the world-wide-web at this URL:
10
 * http://opensource.org/licenses/osl-3.0.php
11
 *
12
 * PHP version 5
13
 *
14
 * @author    Tim Wagner <[email protected]>
15
 * @copyright 2018 TechDivision GmbH <[email protected]>
16
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
17
 * @link      https://github.com/appserver-io/restapi
18
 * @link      http://www.appserver.io
19
 */
20
21
namespace AppserverIo\RestApi\Parsers\OA2;
22
23
use AppserverIo\Psr\Servlet\Http\HttpServletRequestInterface;
24
use AppserverIo\RestApi\Parsers\RequestParserInterface;
25
use AppserverIo\RestApi\Wrappers\OperationWrapperInterface;
26
use AppserverIo\RestApi\Wrappers\ParameterWrapperInterface;
27
28
/**
29
 * OpenApi 2.0 compatible request parser.
30
 *
31
 * @author    Tim Wagner <[email protected]>
32
 * @copyright 2018 TechDivision GmbH <[email protected]>
33
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
34
 * @link      https://github.com/appserver-io/restapi
35
 * @link      http://www.appserver.io
36
 */
37
class RequestParser implements RequestParserInterface
38
{
39
40
    /**
41
     * Returns the integer value for the passed parameter from the request.
42
     *
43
     * @param \AppserverIo\Psr\Servlet\Http\HttpServletRequestInterface $servletRequest The HTTP servlet request instance
44
     * @param \AppserverIo\RestApi\Wrappers\ParameterWrapperInterface   $parameter      The parameter instance
45
     *
46
     * @return integer|null The parsed value
47
     * @throws \Exception Is thrown, if the specified collection format invalid
48
     */
49
    protected function processArrayParameter(HttpServletRequestInterface $servletRequest, ParameterWrapperInterface $parameter)
50
    {
51
52
        // query the collection format specified for the parameter
53
        if ($parameter->hasCollectionFormat('csv')) {
54
            // load the value from the request
55
            if ($param = $servletRequest->getParameter($parameter->getName())) {
56
                // initialize the array for the values
57
                $parameters = array();
58
59
                // extract the values by parsing them as CSV string
60
                foreach (str_getcsv(urldecode($param)) as $value) {
61
                    list ($key, $val) = explode('=', urldecode($value));
62
                    $parameters[$key] = $val;
63
                }
64
65
                // return the parameters
66
                return $parameters;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $parameters returns the type array which is incompatible with the documented return type null|integer.
Loading history...
67
            }
68
69
            // return an empty array
70
            return array();
0 ignored issues
show
Bug Best Practice introduced by
The expression return array() returns the type array which is incompatible with the documented return type null|integer.
Loading history...
71
        } elseif ($parameter->hasCollectionFormat('multi')) {
72
            throw new \Exception('Collection format "multi" is not yet supported');
73
        } else {
74
            throw new \Exception(sprintf('Unknown collection format "%s" is not supported yet', $parameter->getCollectionFormat()));
75
        }
76
    }
77
78
    /**
79
     * Returns the string value for the passed parameter from the request.
80
     *
81
     * @param \AppserverIo\Psr\Servlet\Http\HttpServletRequestInterface $servletRequest The HTTP servlet request instance
82
     * @param \AppserverIo\RestApi\Wrappers\ParameterWrapperInterface   $parameter      The parameter instance
83
     *
84
     * @return string|null The parsed value
85
     */
86
    protected function processStringParameter(HttpServletRequestInterface $servletRequest, ParameterWrapperInterface $parameter)
87
    {
88
        return $servletRequest->getParameter($parameter->getName());
89
    }
90
91
    /**
92
     * Returns the integer value for the passed parameter from the request.
93
     *
94
     * @param \AppserverIo\Psr\Servlet\Http\HttpServletRequestInterface $servletRequest The HTTP servlet request instance
95
     * @param \AppserverIo\RestApi\Wrappers\ParameterWrapperInterface   $parameter      The parameter instance
96
     *
97
     * @return integer|null The parsed value
98
     */
99
    protected function processIntegerParameter(HttpServletRequestInterface $servletRequest, ParameterWrapperInterface $parameter)
100
    {
101
        if ($value = $servletRequest->getParameter($parameter->getName(), FILTER_VALIDATE_INT)) {
102
            return (integer) $value;
103
        }
104
    }
105
106
    /**
107
     * Returns the internal method name to load the value from the request parameters.
108
     *
109
     * @param \AppserverIo\RestApi\Wrappers\ParameterWrapperInterface $parameter The parameter to create prepare the method name for
110
     *
111
     * @return string The prepared method name
112
     */
113
    protected function prepareMethodName(ParameterWrapperInterface $parameter)
114
    {
115
        return sprintf('process%sParameter', ucfirst($parameter->getType()));
116
    }
117
118
    /**
119
     * Parses the HTTP request for request parameters defined by the also passed operation.
120
     *
121
     * @param \AppserverIo\Psr\Servlet\Http\HttpServletRequestInterface $servletRequest   The HTTP servlet request instance
122
     * @param \AppserverIo\RestApi\Wrappers\OperationWrapperInterface   $operationWrapper The operation wrapper
123
     *
124
     * @return array The array with the operations values pasrsed from the request
125
     * @throws \Exception is thrown, the context of the operation's parameter is invalid
126
     */
127
    public function parse(HttpServletRequestInterface $servletRequest, OperationWrapperInterface $operationWrapper)
128
    {
129
130
        // initialize the array for the parsed values
131
        $parameters = array();
132
133
        // iterate over the operations parameters and try load the value from the request
134
        foreach ($operationWrapper->getParameters() as $parameter) {
135
            // query the parameter context
136
            if ($parameter->isIn('path')) {
137
                // extract the value from the path info
138
                $value = $operationWrapper->getMatch($parameter);
139
140
                // cast the value depending on the specified type
141
                if ($parameter->getType() === 'integer') {
142
                    $parameters[] = (integer) $value;
143
                } elseif ($parameter->getType() === 'float') {
144
                    $parameters[] = (float) $value;
145
                } else {
146
                    $parameters[] = $value;
147
                }
148
            } elseif ($parameter->isIn('query')) {
149
                // try to load the value from the request parameters
150
                $parameters[] = call_user_func(array($this, $this->prepareMethodName($parameter)), $servletRequest, $parameter);
151
            } else {
152
                throw new \Exception(sprintf('Query parameter context "%s" is not supported yed', $parameter->getIn()));
153
            }
154
        }
155
156
        // return the array with parsed values
157
        return $parameters;
158
    }
159
}
160