Passed
Push — 2.x ( 9cccdb...7ea17f )
by Terry
01:58
created

DeniedTrait::hasDeniedItem()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
1
<?php
2
/**
3
 * This file is part of the Shieldon package.
4
 *
5
 * (c) Terry L. <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 * 
10
 * php version 7.1.0
11
 * 
12
 * @category  Web-security
13
 * @package   Shieldon
14
 * @author    Terry Lin <[email protected]>
15
 * @copyright 2019 terrylinooo
16
 * @license   https://github.com/terrylinooo/shieldon/blob/2.x/LICENSE MIT
17
 * @link      https://github.com/terrylinooo/shieldon
18
 * @see       https://shieldon.io
19
 */
20
21
declare(strict_types=1);
22
23
namespace Shieldon\Firewall\Component;
24
25
use function array_keys;
26
use function array_push;
27
use function in_array;
28
use function strpos;
29
30
/*
31
 * @since 2.0.0
32
 */
33
trait DeniedTrait
34
{
35
    /**
36
     * Data pool for hard whitelist.
37
     *
38
     * @var array
39
     */
40
    protected $deniedList = [];
41
42
    /**
43
     * Add items to the whitelist pool.
44
     *
45
     * @param array $itemList String list.
46
     *
47
     * @return void
48
     */
49
    public function setDeniedItems(array $itemList): void
50
    {
51
        $this->deniedList = $itemList;
52
    }
53
54
    /**
55
     * Add an item to the whitelist pool.
56
     *
57
     * @param string|array $value The value of the data.
58
     * @param string       $key   The key of the data.
59
     *
60
     * @return void
61
     */
62
    public function setDeniedItem($value, string $key = ''): void
63
    {
64
        if (!empty($key)) {
65
            $this->deniedList[$key] = $value;
66
67
        } elseif (!in_array($value, $this->deniedList)) {
68
            array_push($this->deniedList, $value);
69
        }
70
    }
71
72
    /**
73
     * Get items from the whitelist pool.
74
     *
75
     * @return array
76
     */
77
    public function getDeniedItems(): array
78
    {
79
        return $this->deniedList;
80
    }
81
82
    /**
83
     * Get an item from the whitelist pool.
84
     *
85
     * @return string|array
86
     */
87
    public function getDeniedItem(string $key)
88
    {
89
        return $this->deniedList[$key] ?? '';
90
    }
91
92
    /**
93
     * Return the denied item if exists.
94
     *
95
     * @param string $key The key of the data.
96
     *
97
     * @return string
98
     */
99
    public function removeDeniedItem(string $key): void
100
    {
101
        unset($this->deniedList[$key]);
102
    }
103
104
    /**
105
     * Remove all items.
106
     *
107
     * @return void
108
     */
109
    public function removeDeniedItems(): void
110
    {
111
        $this->deniedList = [];
112
    }
113
114
    /**
115
     * Check if a denied item exists.
116
     *
117
     * @param string $key The key of the data.
118
     *
119
     * @return bool
120
     */
121
    function hasDeniedItem(string $key): bool
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
122
    {
123
        return isset($this->deniedList[$key]);
124
    }
125
126
    /**
127
     * Check if a denied item exists have the same prefix.
128
     *
129
     * @param string $key The key of the data.
130
     *
131
     * @return array
132
     */
133
    function getDeniedItemsWithPrefix(string $key): array
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
134
    {
135
        $temp = [];
136
        foreach ($this->deniedList as $keyName => $value) {
137
            if (strpos($keyName, $key) === 0) {
138
                $temp[$keyName] = $value;
139
            }
140
        }
141
        return $temp;
142
    }
143
144
    /**
145
     * Remove denied items with the same prefix.
146
     *
147
     * @param string $key The key of the data.
148
     *
149
     * @return void
150
     */
151
    public function removeDeniedItemsWithPrefix(string $key): void
152
    {
153
        foreach (array_keys($this->deniedList) as $keyName) {
154
            if (strpos($keyName, $key) === 0) {
155
                unset($this->deniedList[$keyName]);
156
            }
157
        }
158
    }
159
160
    /**
161
     * Is denied?
162
     * This method should adjust in extended class if need.
163
     *
164
     * @return bool
165
     */
166
    public function isDenied(): bool
167
    {
168
        return false;
169
    }
170
}
171