Completed
Push — master ( e43f1a...57db71 )
by Arman
26s queued 12s
created

Body   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 70
Duplicated Lines 0 %

Importance

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

5 Methods

Rating   Name   Duplication   Size   Complexity  
A has() 0 3 1
A set() 0 3 1
A delete() 0 4 2
A get() 0 15 4
A all() 0 3 1
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.4.0
13
 */
14
15
namespace Quantum\Http\Request;
16
17
/**
18
 * Trait Body
19
 * @package Quantum\Http\Request
20
 */
21
trait Body
22
{
23
24
    /**
25
     * Request body
26
     * @var array
27
     */
28
    private static $__request = [];
29
30
    /**
31
     * Checks if request contains a data by given key
32
     * @param string $key
33
     * @return bool
34
     */
35
    public static function has(string $key): bool
36
    {
37
        return isset(self::$__request[$key]);
38
    }
39
40
    /**
41
     * Retrieves data from request by given key
42
     * @param string $key
43
     * @param string|null $default
44
     * @param bool $raw
45
     * @return mixed
46
     */
47
    public static function get(string $key, string $default = null, bool $raw = false)
48
    {
49
        $data = $default;
50
51
        if (self::has($key)) {
52
            if ($raw) {
53
                $data = self::$__request[$key];
54
            } else {
55
                $data = is_array(self::$__request[$key]) ?
56
                    filter_var_array(self::$__request[$key], FILTER_SANITIZE_STRING) :
57
                    filter_var(self::$__request[$key], FILTER_SANITIZE_STRING);
58
            }
59
        }
60
61
        return $data;
62
    }
63
64
    /**
65
     * Sets new key/value pair into request
66
     * @param string $key
67
     * @param mixed $value
68
     */
69
    public static function set(string $key, $value)
70
    {
71
        self::$__request[$key] = $value;
72
    }
73
74
    /**
75
     * Gets all request parameters
76
     * @return array
77
     */
78
    public static function all(): array
79
    {
80
        return array_merge(self::$__request, self::$__files);
81
    }
82
83
    /**
84
     * Deletes the element from request by given key
85
     * @param string $key
86
     */
87
    public static function delete(string $key)
88
    {
89
        if (self::has($key)) {
90
            unset(self::$__request[$key]);
91
        }
92
    }
93
94
}