1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Date: 11.11.18 |
4
|
|
|
* Time: 15:22 |
5
|
|
|
*/ |
6
|
|
|
|
7
|
|
|
namespace AlecRabbit\StringList; |
8
|
|
|
|
9
|
|
|
use AlecRabbit\StringList\Contracts\StringListInterface; |
10
|
|
|
|
11
|
|
|
class StringList implements StringListInterface |
12
|
|
|
{ |
13
|
|
|
/** @var array */ |
14
|
|
|
private $including = []; |
15
|
|
|
/** @var array */ |
16
|
|
|
private $excluding = []; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* StringList constructor. |
20
|
|
|
* @param null|array $including |
21
|
|
|
* @param null|array $excluding |
22
|
|
|
*/ |
23
|
62 |
|
public function __construct(?array $including = null, ?array $excluding = null) |
24
|
|
|
{ |
25
|
62 |
|
$this->include(...$including ?? []); |
26
|
62 |
|
$this->exclude(...$excluding ?? []); |
27
|
62 |
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* {@inheritdoc} |
31
|
|
|
*/ |
32
|
62 |
|
public function include(string ...$including): StringListInterface |
33
|
|
|
{ |
34
|
62 |
|
$this->including = $this->process($this->including, $including); |
35
|
62 |
|
$this->excluding = $this->diff($this->excluding, $this->including); |
36
|
|
|
|
37
|
62 |
|
return $this; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @param array $first |
42
|
|
|
* @param array $second |
43
|
|
|
* @return array |
44
|
|
|
*/ |
45
|
62 |
|
private function process(array $first, array $second): array |
46
|
|
|
{ |
47
|
|
|
return |
48
|
62 |
|
array_unique(array_merge($first, $second)); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @param array $first |
53
|
|
|
* @param array $second |
54
|
|
|
* @return array |
55
|
|
|
*/ |
56
|
62 |
|
private function diff(array $first, array $second): array |
57
|
|
|
{ |
58
|
|
|
return |
59
|
62 |
|
array_diff($first, $second); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* {@inheritdoc} |
64
|
|
|
*/ |
65
|
62 |
|
public function exclude(string ...$excluding): StringListInterface |
66
|
|
|
{ |
67
|
62 |
|
$this->excluding = $this->process($this->excluding, $excluding); |
68
|
62 |
|
$this->including = $this->diff($this->including, $this->excluding); |
69
|
|
|
|
70
|
62 |
|
return $this; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* {@inheritdoc} |
75
|
|
|
*/ |
76
|
62 |
|
public function has(string $element): bool |
77
|
|
|
{ |
78
|
|
|
return |
79
|
62 |
|
$this->includes($element) && $this->notExcludes($element); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @param string $element |
84
|
|
|
* @return bool |
85
|
|
|
*/ |
86
|
62 |
|
private function includes(string $element): bool |
87
|
|
|
{ |
88
|
|
|
return |
89
|
62 |
|
empty($this->including) || \in_array($element, $this->including, true); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @param string $element |
94
|
|
|
* @return bool |
95
|
|
|
*/ |
96
|
40 |
|
private function notExcludes(string $element): bool |
97
|
|
|
{ |
98
|
|
|
return |
99
|
40 |
|
empty($this->excluding) || !\in_array($element, $this->excluding, true); |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|