1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SixtyNine\Cloud\Builder; |
4
|
|
|
|
5
|
|
|
use SixtyNine\Cloud\Filters\ChangeCase; |
6
|
|
|
use SixtyNine\Cloud\Filters\Filters; |
7
|
|
|
use SixtyNine\Cloud\Filters\RemoveByLength; |
8
|
|
|
use SixtyNine\Cloud\Filters\RemoveCharacters; |
9
|
|
|
use SixtyNine\Cloud\Filters\RemoveNumbers; |
10
|
|
|
use SixtyNine\Cloud\Filters\RemoveTrailingCharacters; |
11
|
|
|
|
12
|
|
|
class FiltersBuilder |
13
|
|
|
{ |
14
|
|
|
/** @var array */ |
15
|
|
|
protected $allowedCase; |
16
|
|
|
/** @var string|null */ |
17
|
|
|
protected $case; |
18
|
|
|
/** @var bool */ |
19
|
|
|
protected $removeNumbers = true; |
20
|
|
|
/** @var bool */ |
21
|
|
|
protected $removeUnwanted = true; |
22
|
|
|
/** @var bool */ |
23
|
|
|
protected $removeTrailing = true; |
24
|
|
|
/** @var int|null */ |
25
|
|
|
protected $minLength; |
26
|
|
|
/** @var int|null */ |
27
|
|
|
protected $maxLength; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Disallow direct instantiation |
31
|
|
|
*/ |
32
|
13 |
|
protected function __construct() |
33
|
|
|
{ |
34
|
13 |
|
$this->allowedCase = array( |
35
|
|
|
ChangeCase::LOWERCASE, |
36
|
|
|
ChangeCase::UPPERCASE, |
37
|
|
|
ChangeCase::UCFIRST, |
38
|
|
|
); |
39
|
13 |
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @return FiltersBuilder |
43
|
|
|
*/ |
44
|
13 |
|
public static function create() |
45
|
|
|
{ |
46
|
13 |
|
return new self(); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @param string $case |
51
|
|
|
* @return FiltersBuilder |
52
|
|
|
*/ |
53
|
1 |
|
public function setCase($case) |
54
|
|
|
{ |
55
|
1 |
|
$case = strtolower($case); |
56
|
1 |
|
if (in_array($case, $this->allowedCase)) { |
57
|
1 |
|
$this->case = $case; |
58
|
|
|
} |
59
|
1 |
|
return $this; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @param boolean $enabled |
64
|
|
|
* @return FiltersBuilder |
65
|
|
|
*/ |
66
|
6 |
|
public function setRemoveNumbers($enabled) |
67
|
|
|
{ |
68
|
6 |
|
$this->removeNumbers = (bool)$enabled; |
69
|
6 |
|
return $this; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @param boolean $enabled |
74
|
|
|
* @return FiltersBuilder |
75
|
|
|
*/ |
76
|
6 |
|
public function setRemoveUnwanted($enabled) |
77
|
|
|
{ |
78
|
6 |
|
$this->removeUnwanted = (bool)$enabled; |
79
|
6 |
|
return $this; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @param boolean $enabled |
84
|
|
|
* @return FiltersBuilder |
85
|
|
|
*/ |
86
|
6 |
|
public function setRemoveTrailing($enabled) |
87
|
|
|
{ |
88
|
6 |
|
$this->removeTrailing = (bool)$enabled; |
89
|
6 |
|
return $this; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @param int $maxLength |
94
|
|
|
* @return FiltersBuilder |
95
|
|
|
*/ |
96
|
3 |
|
public function setMaxLength($maxLength) |
97
|
|
|
{ |
98
|
3 |
|
$this->maxLength = $maxLength; |
99
|
3 |
|
return $this; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @param int $minLength |
104
|
|
|
* @return FiltersBuilder |
105
|
|
|
*/ |
106
|
3 |
|
public function setMinLength($minLength) |
107
|
|
|
{ |
108
|
3 |
|
$this->minLength = $minLength; |
109
|
3 |
|
return $this; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @return string[] |
114
|
|
|
*/ |
115
|
|
|
public function getAllowedCase() |
116
|
|
|
{ |
117
|
|
|
return $this->allowedCase; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @return Filters |
122
|
|
|
*/ |
123
|
13 |
|
public function build() |
124
|
|
|
{ |
125
|
13 |
|
$filters = new Filters(); |
126
|
|
|
|
127
|
13 |
|
if (null !== $this->case) { |
128
|
1 |
|
$filters->addFilter(new ChangeCase($this->case)); |
129
|
|
|
} |
130
|
|
|
|
131
|
13 |
|
if ($this->removeNumbers) { |
132
|
8 |
|
$filters->addFilter(new RemoveNumbers()); |
133
|
|
|
} |
134
|
|
|
|
135
|
13 |
|
if ($this->removeUnwanted) { |
136
|
8 |
|
$filters->addFilter(new RemoveCharacters()); |
137
|
|
|
} |
138
|
|
|
|
139
|
13 |
|
if ($this->removeTrailing) { |
140
|
8 |
|
$filters->addFilter(new RemoveTrailingCharacters()); |
141
|
|
|
} |
142
|
|
|
|
143
|
13 |
|
if (null !== $this->minLength || null !== $this->maxLength) { |
144
|
3 |
|
$filters->addFilter(new RemoveByLength($this->minLength, $this->maxLength)); |
145
|
|
|
} |
146
|
|
|
|
147
|
13 |
|
return $filters; |
148
|
|
|
} |
149
|
|
|
} |