1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Linna Filter |
5
|
|
|
* |
6
|
|
|
* @author Sebastian Rapetti <[email protected]> |
7
|
|
|
* @copyright (c) 2018, Sebastian Rapetti |
8
|
|
|
* @license http://opensource.org/licenses/MIT MIT License |
9
|
|
|
*/ |
10
|
|
|
declare(strict_types=1); |
11
|
|
|
|
12
|
|
|
namespace Linna\Filter; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Build filter rules from files in rule directory. |
16
|
|
|
*/ |
17
|
|
|
class RuleBuilder |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* Build rules from classes files. |
21
|
|
|
* |
22
|
|
|
* @return array |
23
|
|
|
*/ |
24
|
90 |
|
public static function build(): array |
25
|
|
|
{ |
26
|
90 |
|
$alias = []; |
27
|
90 |
|
$rules = []; |
28
|
90 |
|
$files = glob(__DIR__.'/Rules/*.php', GLOB_ERR); |
29
|
|
|
|
30
|
90 |
|
foreach ($files as $entry) { |
31
|
|
|
//get file name |
32
|
90 |
|
$class = basename(str_replace('.php', '', $entry)); |
33
|
90 |
|
$keyword = strtolower($class); |
34
|
|
|
|
35
|
|
|
//exclude from rules array because |
36
|
|
|
//custom ruless will be built separately |
37
|
90 |
|
if ($keyword === 'customrule') { |
38
|
90 |
|
continue; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
//get full class name |
42
|
90 |
|
$class = __NAMESPACE__."\Rules\\{$class}"; |
43
|
|
|
|
44
|
90 |
|
self::makeAlias($rules, $alias, $keyword, $class::$config); |
|
|
|
|
45
|
|
|
} |
46
|
|
|
|
47
|
90 |
|
return [$rules, $alias]; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Build custom rules passed to Filter with . |
52
|
|
|
* |
53
|
|
|
* @param array $customRules |
54
|
|
|
* |
55
|
|
|
* @return array |
56
|
|
|
*/ |
57
|
|
|
public static function buildCustom(array $customRules): array |
58
|
|
|
{ |
59
|
|
|
$alias = []; |
60
|
|
|
$rules = []; |
61
|
|
|
|
62
|
|
|
foreach ($customRules as $rule) { |
63
|
|
|
$config = $rule->config; |
64
|
|
|
$config['instance'] = $rule; |
65
|
|
|
|
66
|
|
|
self::makeAlias($rules, $alias, $config['alias'][0], $config); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
return [$rules, $alias]; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Make alias array for Filter class. |
74
|
|
|
* |
75
|
|
|
* @param array $rules Array containing rules |
76
|
|
|
* @param array $alias Array containing rule aliases |
77
|
|
|
* @param string $keyword Name of the rule class or rule alias for custom rules |
78
|
|
|
* @param array $config Rule configuration |
79
|
|
|
*/ |
80
|
90 |
|
private static function makeAlias(array &$rules, array &$alias, string $keyword, array $config): void |
81
|
|
|
{ |
82
|
|
|
//get config for the rule |
83
|
90 |
|
$rules[$keyword] = $config; |
84
|
|
|
//fill array with alias |
85
|
90 |
|
$keys = $rules[$keyword]['alias']; |
86
|
|
|
//fill array of values with name of the class |
87
|
90 |
|
$values = array_fill(0, count($keys), $keyword); |
88
|
|
|
//combine keys and values |
89
|
90 |
|
$array = array_combine($keys, $values); |
90
|
|
|
//add all to alias array |
91
|
90 |
|
$alias = array_merge($alias, $array); |
92
|
|
|
//free memory |
93
|
90 |
|
unset($rules[$keyword]['alias']); |
94
|
90 |
|
} |
95
|
|
|
} |
96
|
|
|
|