Passed
Pull Request — master (#303)
by Arman
02:44
created

Params   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 96
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 25
dl 0
loc 96
rs 10
c 1
b 0
f 0
wmc 16

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getParams() 0 7 3
A urlEncodedParams() 0 12 3
A getRawInput() 0 3 1
A getRawInputParams() 0 10 3
A postParams() 0 7 2
A jsonPayloadParams() 0 10 4
1
<?php
2
3
/**
4
 * Quantum PHP Framework
5
 *
6
 * An open source software development framework for PHP
7
 *
8
 * @package Quantum
9
 * @author Arman Ag. <[email protected]>
10
 * @copyright Copyright (c) 2018 Softberg LLC (https://softberg.org)
11
 * @link http://quantum.softberg.org/
12
 * @since 2.9.8
13
 */
14
15
namespace Quantum\Http\Traits\Request;
16
17
18
use Quantum\Config\Exceptions\ConfigException;
19
use Quantum\App\Exceptions\BaseException;
20
use Quantum\Http\Constants\ContentType;
21
use Quantum\Di\Exceptions\DiException;
22
use ReflectionException;
23
24
/**
25
 * Trait Params
26
 * @package Quantum\Http\Request
27
 */
28
trait Params
29
{
30
31
    /**
32
     * Request content type
33
     * @var string|null
34
     */
35
    private static $__contentType = null;
36
37
    /**
38
     * Gets the GET params.
39
     * @return array
40
     */
41
    private static function getParams(): array
42
    {
43
        if (empty($_GET)) {
44
            return [];
45
        }
46
47
        return filter_input_array(INPUT_GET) ?: [];
48
    }
49
50
    /**
51
     * Gets the POST params.
52
     * @return array
53
     */
54
    private static function postParams(): array
55
    {
56
        if (empty($_POST)) {
57
            return [];
58
        }
59
60
        return filter_input_array(INPUT_POST) ?? [];
61
    }
62
63
    /**
64
     * Parses and returns JSON payload parameters.
65
     * @return array
66
     */
67
    private static function jsonPayloadParams(): array
68
    {
69
        if (
70
            !in_array(self::$__method, ['PUT', 'PATCH', 'POST'], true) ||
71
            self::$__contentType !== ContentType::JSON
72
        ) {
73
            return [];
74
        }
75
76
        return json_decode(self::getRawInput(), true) ?: [];
77
    }
78
79
    /**
80
     * Parses and returns URL-encoded parameters.
81
     * @return array
82
     */
83
    private static function urlEncodedParams(): array
84
    {
85
        if (
86
            !in_array(self::$__method, ['PUT', 'PATCH', 'POST'], true) ||
87
            self::$__contentType !== ContentType::URL_ENCODED
88
        ) {
89
            return [];
90
        }
91
92
        parse_str(urldecode(self::getRawInput()), $result);
93
94
        return $result;
95
    }
96
97
    /**
98
     * Parses and returns multipart form data parameters.
99
     * @return array[]
100
     * @throws BaseException
101
     * @throws ConfigException
102
     * @throws DiException
103
     * @throws ReflectionException
104
     */
105
    private static function getRawInputParams(): array
106
    {
107
        if (
108
            !in_array(self::$__method, ['PUT', 'PATCH', 'POST'], true) ||
109
            self::$__contentType !== ContentType::FORM_DATA
110
        ) {
111
            return ['params' => [], 'files' => []];
112
        }
113
114
        return self::parse(self::getRawInput());
115
    }
116
117
    /**
118
     * Retrieves the raw HTTP request body as a string.
119
     * @return string
120
     */
121
    private static function getRawInput(): string
122
    {
123
        return file_get_contents('php://input');
124
    }
125
}