SplashRequestContext::addUrlParameter()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
rs 10
cc 1
eloc 2
nc 1
nop 2
1
<?php
2
3
namespace Mouf\Mvc\Splash\Services;
4
5
use Mouf\Mvc\Splash\Exception\SplashMissingParameterException;
6
use Psr\Http\Message\ServerRequestInterface;
7
8
/**
9
 * This class represents the context of a request so far (so basically, it contains the
10
 * HTTP request and any additional parameters from the URL analysis).
11
 *
12
 * @author David Negrier
13
 */
14
class SplashRequestContext
15
{
16
    private $urlParameters = array();
17
    private $request;
18
19
    public function __construct(ServerRequestInterface $request)
20
    {
21
        $this->request = $request;
22
    }
23
24
    /**
25
     * Add a new parameter.
26
     *
27
     * @param string $key
28
     * @param string $value
29
     */
30
    public function addUrlParameter($key, $value)
31
    {
32
        $this->urlParameters[$key] = $value;
33
    }
34
35
    /**
36
     * Sets all parameters at once.
37
     *
38
     * @param array $urlParameters
39
     */
40
    public function setUrlParameters(array $urlParameters)
41
    {
42
        $this->urlParameters = $urlParameters;
43
    }
44
45
    /**
46
     * Returns the list of parameters seen while analysing the URL.
47
     *
48
     * @return array<string, string>
0 ignored issues
show
Documentation introduced by
The doc-type array<string, could not be parsed: Expected ">" at position 5, but found "end of type". (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
49
     */
50
    public function getUrlParameters()
51
    {
52
        return $this->urlParameters;
53
    }
54
55
    /**
56
     * Returns the request.
57
     *
58
     * @return ServerRequestInterface
59
     */
60
    public function getRequest()
61
    {
62
        return $this->request;
63
    }
64
65
    public function hasParameter($key) : bool
66
    {
67
        if (isset($this->urlParameters[$key])) {
68
            return true;
69
        } elseif (isset($this->request->getParsedBody()[$key])) {
70
            return true;
71
        } elseif (isset($this->request->getQueryParams()[$key])) {
72
            return true;
73
        } elseif (isset($this->request->getUploadedFiles()[$key])) {
74
            return true;
75
        }
76
77
        return false;
78
    }
79
80
    /**
81
     * Scan the URL parameters and the request parameters and return the given parameter (or a default value).
82
     *
83
     * @param string $key
84
     *
85
     * @return mixed
86
     *
87
     * @throws SplashMissingParameterException
88
     */
89
    public function getParameter(string $key, bool $compulsory, $default = null)
90
    {
91
        if (isset($this->urlParameters[$key])) {
92
            $value = $this->urlParameters[$key];
93
        } else {
94
            $postVals = $this->request->getParsedBody();
95
            $value = null;
96
            if (isset($postVals[$key])) {
97
                $value = $postVals[$key];
98
            } else {
99
                $getVals = $this->request->getQueryParams();
100
                if (isset($getVals[$key])) {
101
                    $value = $getVals[$key];
102
                } else {
103
                    $uploadedFiles = $this->request->getUploadedFiles();
104
                    if (isset($uploadedFiles[$key])) {
105
                        $value = $uploadedFiles[$key];
106
                    }
107
                }
108
            }
109
        }
110
        if ($value !== null) {
111
            return $value;
112
        } elseif (!$compulsory) {
113
            return $default;
114
        } else {
115
            throw SplashMissingParameterException::create($key);
116
        }
117
    }
118
}
119