Completed
Pull Request — master (#447)
by Alexandru
10:50 queued 02:32
created

QueryParameters::all()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace BeyondCode\LaravelWebSockets\Server;
4
5
use Psr\Http\Message\RequestInterface;
6
7
class QueryParameters
8
{
9
    /**
10
     * The Request object.
11
     *
12
     * @var \Psr\Http\Message\RequestInterface
13
     */
14
    protected $request;
15
16
    public static function create(RequestInterface $request)
17
    {
18
        return new static($request);
19
    }
20
21
    /**
22
     * Initialize the class.
23
     *
24
     * @param  \Psr\Http\Message\RequestInterface  $request
25
     * @return void
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
26
     */
27
    public function __construct(RequestInterface $request)
28
    {
29
        $this->request = $request;
30
    }
31
32
    /**
33
     * Get all query parameters.
34
     *
35
     * @return array
36
     */
37
    public function all(): array
38
    {
39
        $queryParameters = [];
40
41
        parse_str($this->request->getUri()->getQuery(), $queryParameters);
42
43
        return $queryParameters;
44
    }
45
46
    /**
47
     * Get a specific query parameter.
48
     *
49
     * @param  string  $name
50
     * @return string
51
     */
52
    public function get(string $name): string
53
    {
54
        return $this->all()[$name] ?? '';
55
    }
56
}
57