|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Ttskch; |
|
4
|
|
|
|
|
5
|
|
|
class AccessRestrictor |
|
6
|
|
|
{ |
|
7
|
|
|
/** |
|
8
|
|
|
* @var array |
|
9
|
|
|
*/ |
|
10
|
|
|
private $publicCategories; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* @var array |
|
14
|
|
|
*/ |
|
15
|
|
|
private $publicTags; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* @var array |
|
19
|
|
|
*/ |
|
20
|
|
|
private $privateCategories; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @var array |
|
24
|
|
|
*/ |
|
25
|
|
|
private $privateTags; |
|
26
|
|
|
|
|
27
|
23 |
|
public function __construct(array $publicCategories = [], array $publicTags = [], array $privateCategories = [], array $privateTags = []) |
|
28
|
|
|
{ |
|
29
|
23 |
|
$this->publicCategories = $publicCategories; |
|
30
|
23 |
|
$this->publicTags = $publicTags; |
|
31
|
23 |
|
$this->privateCategories = $privateCategories; |
|
32
|
23 |
|
$this->privateTags = $privateTags; |
|
33
|
23 |
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @param string $category |
|
37
|
|
|
* @param array $tags |
|
38
|
|
|
* @return bool |
|
39
|
|
|
*/ |
|
40
|
10 |
|
public function isPublic($category, array $tags) |
|
41
|
|
|
{ |
|
42
|
10 |
|
if ($this->isWithheld($category, $tags)) { |
|
43
|
7 |
|
return false; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
4 |
|
return empty($this->publicCategories) || $this->isPublished($category, $tags); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @param string $category |
|
51
|
|
|
* @param array $tags |
|
52
|
|
|
* @return bool |
|
53
|
|
|
*/ |
|
54
|
4 |
|
public function isPublished($category, array $tags) |
|
55
|
|
|
{ |
|
56
|
4 |
|
return $this->categoryIsUnderOneOf($category, $this->publicCategories) || $this->atLeastOneTagIsIn($tags, $this->publicTags); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* @param string $category |
|
61
|
|
|
* @param array $tags |
|
62
|
|
|
* @return bool |
|
63
|
|
|
*/ |
|
64
|
10 |
|
public function isWithheld($category, array $tags) |
|
65
|
|
|
{ |
|
66
|
10 |
|
return $this->categoryIsUnderOneOf($category, $this->privateCategories) || $this->atLeastOneTagIsIn($tags, $this->privateTags); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* @param string $needle |
|
71
|
|
|
* @param array $haystacks |
|
72
|
|
|
* @return bool |
|
73
|
|
|
*/ |
|
74
|
17 |
|
public function categoryIsUnderOneOf($needle, array $haystacks) |
|
75
|
|
|
{ |
|
76
|
17 |
|
foreach ($haystacks as $haystack) { |
|
77
|
17 |
|
if (preg_match(sprintf('#^%s#', $haystack), $needle)) { |
|
78
|
9 |
|
return true; |
|
79
|
|
|
} |
|
80
|
11 |
|
} |
|
81
|
|
|
|
|
82
|
9 |
|
return false; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* @param array $needles |
|
87
|
|
|
* @param array $haystacks |
|
88
|
|
|
* @return bool |
|
89
|
|
|
*/ |
|
90
|
11 |
|
public function atLeastOneTagIsIn(array $needles, array $haystacks) |
|
91
|
|
|
{ |
|
92
|
11 |
|
foreach ($haystacks as $haystack) { |
|
93
|
11 |
|
foreach ($needles as $needle) { |
|
94
|
8 |
|
if ($needle === $haystack) { |
|
95
|
7 |
|
return true; |
|
96
|
|
|
} |
|
97
|
8 |
|
} |
|
98
|
7 |
|
} |
|
99
|
|
|
|
|
100
|
6 |
|
return false; |
|
101
|
|
|
} |
|
102
|
|
|
} |
|
103
|
|
|
|