1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of the theroadbunch/bouncer package. |
4
|
|
|
* |
5
|
|
|
* (c) Dan McAdams <[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
|
|
|
|
11
|
|
|
namespace RoadBunch\Bouncer; |
12
|
|
|
|
13
|
|
|
|
14
|
|
|
use RoadBunch\String\NamedStringCollectionInterface; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Class Bouncer |
18
|
|
|
* |
19
|
|
|
* @author Dan McAdams |
20
|
|
|
* @package RoadBunch\Bouncer |
21
|
|
|
*/ |
22
|
|
|
class Bouncer |
23
|
|
|
{ |
24
|
|
|
const BLACKLIST = 'blacklist'; |
25
|
|
|
const WHITELIST = 'whitelist'; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var NamedStringCollectionInterface |
29
|
|
|
*/ |
30
|
|
|
protected $filterList; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* BlacklistValidator constructor. |
34
|
|
|
* |
35
|
|
|
* @param NamedStringCollectionInterface $filterList |
36
|
|
|
* |
37
|
|
|
* @throws InvalidCollectionNameException |
38
|
|
|
*/ |
39
|
8 |
|
public function __construct(NamedStringCollectionInterface $filterList = null) |
40
|
|
|
{ |
41
|
8 |
|
if ($filterList !== null) { |
42
|
7 |
|
$this->validateListType($filterList); |
43
|
|
|
} |
44
|
7 |
|
$this->filterList = $filterList ?? new Blacklist(); |
45
|
7 |
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Checks the string against a blacklist or a white list |
49
|
|
|
* |
50
|
|
|
* @param string $element |
51
|
|
|
* |
52
|
|
|
* @return bool Returns true if the element is not found in the whitelist and true if found in the blacklist |
53
|
|
|
*/ |
54
|
7 |
|
public function isBlacklisted(string $element): bool |
55
|
|
|
{ |
56
|
7 |
|
if ($this->hasBlacklist()) { |
57
|
4 |
|
return $this->filterList->has($element); |
58
|
|
|
} |
59
|
3 |
|
return !$this->filterList->has($element); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Returns the opposite of isBlacklisted |
64
|
|
|
* |
65
|
|
|
* @param string $element |
66
|
|
|
* |
67
|
|
|
* @return bool |
68
|
|
|
*/ |
69
|
5 |
|
public function isAllowed(string $element): bool |
70
|
|
|
{ |
71
|
5 |
|
return !$this->isBlacklisted($element); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @param string $element |
76
|
|
|
*/ |
77
|
2 |
|
public function addToWhitelist(string $element): void |
78
|
|
|
{ |
79
|
2 |
|
if ($this->hasBlacklist()) { |
80
|
1 |
|
$this->filterList->remove($element); |
81
|
1 |
|
return; |
82
|
|
|
} |
83
|
1 |
|
$this->filterList->add($element); |
84
|
1 |
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @param string $element |
88
|
|
|
*/ |
89
|
3 |
|
public function addToBlacklist(string $element): void |
90
|
|
|
{ |
91
|
3 |
|
if ($this->hasBlacklist()) { |
92
|
2 |
|
$this->filterList->add($element); |
93
|
2 |
|
return; |
94
|
|
|
} |
95
|
1 |
|
$this->filterList->remove($element); |
96
|
1 |
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @return bool |
100
|
|
|
*/ |
101
|
7 |
|
private function hasBlacklist(): bool |
102
|
|
|
{ |
103
|
7 |
|
return $this->filterList->name() === self::BLACKLIST; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @param NamedStringCollectionInterface $filterList |
108
|
|
|
* |
109
|
|
|
* @throws InvalidCollectionNameException |
110
|
|
|
*/ |
111
|
7 |
|
private function validateListType(NamedStringCollectionInterface $filterList): void |
112
|
|
|
{ |
113
|
7 |
|
$validListTypes = [self::BLACKLIST, self::WHITELIST]; |
114
|
|
|
|
115
|
7 |
|
if (!in_array($filterList->name(), $validListTypes)) { |
116
|
1 |
|
throw new InvalidCollectionNameException("The provided FilterList must be of type 'whitelist' or 'blacklist'"); |
117
|
|
|
} |
118
|
6 |
|
} |
119
|
|
|
} |
120
|
|
|
|