Completed
Push — master ( c5de01...c9c19d )
by
unknown
07:34 queued 05:10
created

Request::hasQueryParameters()   A

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
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace Coduo\TuTu\Config\Element;
4
5
use Symfony\Component\OptionsResolver\OptionsResolver;
6
7
class Request
8
{
9
    /**
10
     * @var string
11
     */
12
    private $path;
13
14
    /**
15
     * @var array
16
     */
17
    private $allowedMethods;
18
19
    /**
20
     * @var array
21
     */
22
    private $request;
23
24
    /**
25
     * @var array
26
     */
27
    private $query;
28
29
    /**
30
     * @var array
31
     */
32
    private $headers;
33
34
    /**
35
     * @var string
36
     */
37
    private $body;
38
39
    /**
40
     * @param $path
41
     */
42
    public function __construct($path)
43
    {
44
        $this->validatePath($path);
45
        $this->path = $path;
46
        $this->allowedMethods = [];
47
        $this->request = [];
48
        $this->query = [];
49
        $this->headers = [];
50
        $this->body = '';
51
    }
52
53
    public static function fromArray(array $arrayConfig)
54
    {
55
        $configResolver = self::createArrayConfigResolver();
56
        $config = $configResolver->resolve($arrayConfig);
57
        $responseConfig = new Request($config['path']);
58
59
        $responseConfig->setAllowedMethods($config['methods']);
60
        $responseConfig->setBodyParameters($config['request']);
61
        $responseConfig->setQueryParameters($config['query']);
62
        $responseConfig->setHeaders($config['headers']);
63
        $responseConfig->setBody($config['body']);
64
65
        return $responseConfig;
66
    }
67
68
    /**
69
     * @param $methods
70
     */
71
    public function setAllowedMethods($methods)
72
    {
73
        $this->allowedMethods = array_map(function ($method) {
74
            return strtoupper($method);
75
        }, $methods);
76
    }
77
78
    /**
79
     * @return array
80
     */
81
    public function getAllowedMethods()
82
    {
83
        return $this->allowedMethods;
84
    }
85
86
    /**
87
     * @return string
88
     */
89
    public function getPath()
90
    {
91
        return '/' . ltrim($this->path, '/');
92
    }
93
94
    /**
95
     * @return bool
96
     */
97
    public function hasBodyParameters()
98
    {
99
        return (boolean) count($this->request);
100
    }
101
102
    /**
103
     * @param $parameters
104
     */
105
    public function setBodyParameters($parameters)
106
    {
107
        $this->request = $parameters;
108
    }
109
110
    /**
111
     * @return array
112
     */
113
    public function getBodyParameters()
114
    {
115
        return $this->request;
116
    }
117
118
    /**
119
     * @return bool
120
     */
121
    public function hasQueryParameters()
122
    {
123
        return (boolean) count($this->query);
124
    }
125
126
    /**
127
     * @param $parameters
128
     */
129
    public function setQueryParameters($parameters)
130
    {
131
        $this->query = $parameters;
132
    }
133
134
    /**
135
     * @return array
136
     */
137
    public function getQueryParameters()
138
    {
139
        return $this->query;
140
    }
141
142
    /**
143
     * @return boolean
144
     */
145
    public function hasHeaders()
146
    {
147
        return (boolean) count($this->headers);
148
    }
149
150
    /**
151
     * @param array $headers
152
     */
153
    public function setHeaders($headers)
154
    {
155
        $this->headers = $headers;
156
    }
157
158
    /**
159
     * @return array
160
     */
161
    public function getHeaders()
162
    {
163
        return $this->headers;
164
    }
165
166
    /**
167
     * @return bool
168
     */
169
    public function hasBody()
170
    {
171
        return !empty($this->body);
172
    }
173
174
    /**
175
     * @param string $body
176
     */
177
    public function setBody($body)
178
    {
179
        $this->body = $body;
180
    }
181
182
    /**
183
     * @return string
184
     */
185
    public function getBody()
186
    {
187
        return $this->body;
188
    }
189
190
    /**
191
     * @param $path
192
     * @throws \InvalidArgumentException
193
     */
194
    private function validatePath($path)
195
    {
196
        if (!is_string($path)) {
197
            throw new \InvalidArgumentException("Path must be a valid string.");
198
        }
199
200
        if (empty($path)) {
201
            throw new \InvalidArgumentException("Path can't be empty.");
202
        }
203
    }
204
205
    /**
206
     * @return OptionsResolver
207
     */
208
    private static function createArrayConfigResolver()
209
    {
210
        $configResolver = new OptionsResolver();
211
        $configResolver->setRequired(['path']);
212
        $configResolver->setDefaults([
213
            'methods' => [],
214
            'request' => [],
215
            'query' => [],
216
            'headers' => [],
217
            'body' => ''
218
        ]);
219
        $configResolver->setAllowedTypes('path', 'string');
220
        $configResolver->setAllowedTypes('methods', 'array');
221
        $configResolver->setAllowedTypes('request', 'array');
222
        $configResolver->setAllowedTypes('query', 'array');
223
        $configResolver->setAllowedTypes('headers', 'array');
224
        $configResolver->setAllowedTypes('body', 'string');
225
226
        return $configResolver;
227
    }
228
}
229