1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Valdi package. |
5
|
|
|
* |
6
|
|
|
* (c) Philip Lehmann-Böhm <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Valdi; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* To ease the building of the rules array. |
16
|
|
|
*/ |
17
|
|
|
class RulesBuilder |
18
|
|
|
{ |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Holds the rules under construction. |
22
|
|
|
*/ |
23
|
|
|
protected $rules; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Creates and returns an instance. |
27
|
|
|
* |
28
|
|
|
* @return RulesBuilder - the new instance |
29
|
|
|
*/ |
30
|
8 |
|
public static function create() : RulesBuilder |
31
|
|
|
{ |
32
|
8 |
|
return new RulesBuilder(); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Constructor. |
37
|
|
|
*/ |
38
|
8 |
|
public function __construct() |
39
|
|
|
{ |
40
|
8 |
|
$this->rules = []; |
41
|
8 |
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Adds a rule for a field to the set. This function takes a variable amount |
45
|
|
|
* of parameters in order to cover the rule parameters. Example for a rule |
46
|
|
|
* without parameter: |
47
|
|
|
* field('myField', 'required') |
48
|
|
|
* Example for a rule with two parameters: |
49
|
|
|
* field('myField', 'between', 3, 7) |
50
|
|
|
* |
51
|
|
|
* @param string $field the field for the rule |
52
|
|
|
* @param string $rule the rule to add |
53
|
|
|
* |
54
|
|
|
* @return RulesBuilder - the instance of the called RulesBuilder in order to chain the rules creation |
55
|
|
|
*/ |
56
|
6 |
|
public function field(string $field, string $rule) : RulesBuilder |
57
|
|
|
{ |
58
|
6 |
|
if (!isset($this->rules[$field])) { |
59
|
6 |
|
$this->rules[$field] = []; |
60
|
|
|
} |
61
|
6 |
|
$newRule = [$rule]; |
62
|
6 |
|
$numArgs = func_num_args(); |
63
|
6 |
|
for ($i = 2; $i < $numArgs; ++$i) { |
64
|
4 |
|
$newRule[] = func_get_arg($i); |
65
|
|
|
} |
66
|
6 |
|
$this->rules[$field][] = $newRule; |
67
|
6 |
|
return $this; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Adds a rule to the set. This function takes a variable amount |
72
|
|
|
* of parameters in order to cover the rule parameters. Example for a rule |
73
|
|
|
* without parameter: |
74
|
|
|
* rule('required') |
75
|
|
|
* Example for a rule with two parameters: |
76
|
|
|
* rule('between', 3, 7) |
77
|
|
|
* |
78
|
|
|
* @param string $rule the rule to add |
79
|
|
|
* |
80
|
|
|
* @return RulesBuilder - the instance of the called RulesBuilder in order to chain the rules creation |
81
|
|
|
*/ |
82
|
3 |
|
public function rule(string $rule) : RulesBuilder |
83
|
|
|
{ |
84
|
3 |
|
$newRule = [$rule]; |
85
|
3 |
|
$numArgs = func_num_args(); |
86
|
3 |
|
for ($i = 1; $i < $numArgs; ++$i) { |
87
|
3 |
|
$newRule[] = func_get_arg($i); |
88
|
|
|
} |
89
|
3 |
|
$this->rules[] = $newRule; |
90
|
|
|
|
91
|
3 |
|
return $this; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Gets the created rules. Afterwards, the RulesBuilder is emptied and ready to be used again. |
96
|
|
|
* |
97
|
|
|
* @return array - the created rules |
98
|
|
|
*/ |
99
|
7 |
|
public function build() : array |
100
|
|
|
{ |
101
|
7 |
|
$rules = $this->rules; |
102
|
7 |
|
$this->rules = []; |
103
|
7 |
|
return $rules; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
} |
107
|
|
|
|