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

Body::delete()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 2
nc 2
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
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
use Quantum\App\Constants\ReservedKeys;
18
use InvalidArgumentException;
19
20
/**
21
 * Trait Body
22
 * @package Quantum\Http\Request
23
 */
24
trait Body
25
{
26
27
    /**
28
     * Request body
29
     * @var array
30
     */
31
    private static $__request = [];
32
33
    /**
34
     * Checks if request contains a data by given key
35
     * @param string $key
36
     * @return bool
37
     */
38
    public static function has(string $key): bool
39
    {
40
        return isset(self::$__request[$key]);
41
    }
42
43
    /**
44
     * Retrieves data from request by given key
45
     * @param string $key
46
     * @param string|null $default
47
     * @param bool $raw
48
     * @return mixed
49
     */
50
    public static function get(string $key, string $default = null, bool $raw = false)
51
    {
52
        if(!self::has($key)) {
53
            return $default;
54
        }
55
56
        $value = self::$__request[$key];
57
58
        if ($raw) {
59
            return $value;
60
        }
61
62
        return is_array($value)
63
            ? array_map('strip_tags', $value)
64
            : strip_tags($value);
65
    }
66
67
    /**
68
     * Sets new key/value pair into request
69
     * @param string $key
70
     * @param mixed $value
71
     */
72
    public static function set(string $key, $value)
73
    {
74
        if ($key === ReservedKeys::RENDERED_VIEW) {
75
            throw new InvalidArgumentException("Cannot set reserved key: `$key`");
76
        }
77
78
        self::$__request[$key] = $value;
79
    }
80
81
    /**
82
     * Gets all request parameters
83
     * @return array
84
     */
85
    public static function all(): array
86
    {
87
        return array_merge(self::$__request, self::$__files);
88
    }
89
90
    /**
91
     * Deletes the element from request by given key
92
     * @param string $key
93
     */
94
    public static function delete(string $key)
95
    {
96
        if (self::has($key)) {
97
            unset(self::$__request[$key]);
98
        }
99
    }
100
}