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 |
29
|
|
|
* the new instance |
30
|
|
|
*/ |
31
|
7 |
|
public static function create() |
32
|
|
|
{ |
33
|
7 |
|
return new RulesBuilder(); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Constructor. |
38
|
|
|
*/ |
39
|
7 |
|
public function __construct() |
40
|
|
|
{ |
41
|
7 |
|
$this->rules = []; |
42
|
7 |
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Adds a rule for a field to the set. This function takes a variable amount |
46
|
|
|
* of parameters in order to cover the rule parameters. Example for a rule |
47
|
|
|
* without parameter: |
48
|
|
|
* field('myField', 'required') |
49
|
|
|
* Example for a rule with two parameters: |
50
|
|
|
* field('myField', 'between', 3, 7) |
51
|
|
|
* |
52
|
|
|
* @param string $field |
53
|
|
|
* the field for the rule |
54
|
|
|
* @param string $rule |
55
|
|
|
* the rule to add |
56
|
|
|
* |
57
|
|
|
* @return RulesBuilder |
58
|
|
|
* the instance of the called RulesBuilder in order to chain the rules |
59
|
|
|
* creation |
60
|
|
|
*/ |
61
|
5 |
|
public function field($field, $rule) |
62
|
|
|
{ |
63
|
5 |
|
if (!isset($this->rules[$field])) { |
64
|
5 |
|
$this->rules[$field] = []; |
65
|
|
|
} |
66
|
5 |
|
$newRule = [$rule]; |
67
|
5 |
|
$numArgs = func_num_args(); |
68
|
5 |
|
for ($i = 2; $i < $numArgs; ++$i) { |
69
|
3 |
|
$newRule[] = func_get_arg($i); |
70
|
|
|
} |
71
|
5 |
|
$this->rules[$field][] = $newRule; |
72
|
5 |
|
return $this; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Adds a rule to the set. This function takes a variable amount |
77
|
|
|
* of parameters in order to cover the rule parameters. Example for a rule |
78
|
|
|
* without parameter: |
79
|
|
|
* rule('required') |
80
|
|
|
* Example for a rule with two parameters: |
81
|
|
|
* rule('between', 3, 7) |
82
|
|
|
* |
83
|
|
|
* @param string $rule |
84
|
|
|
* the rule to add |
85
|
|
|
* |
86
|
|
|
* @return RulesBuilder |
87
|
|
|
* the instance of the called RulesBuilder in order to chain the rules |
88
|
|
|
* creation |
89
|
|
|
*/ |
90
|
2 |
|
public function rule($rule) |
91
|
|
|
{ |
92
|
2 |
|
$newRule = [$rule]; |
93
|
2 |
|
$numArgs = func_num_args(); |
94
|
2 |
|
for ($i = 1; $i < $numArgs; ++$i) { |
95
|
2 |
|
$newRule[] = func_get_arg($i); |
96
|
|
|
} |
97
|
2 |
|
$this->rules[] = $newRule; |
98
|
|
|
|
99
|
2 |
|
return $this; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Gets the created rules. Afterwards, the RulesBuilder is emptied and ready to be used again. |
104
|
|
|
* |
105
|
|
|
* @return array |
106
|
|
|
* the created rules |
107
|
|
|
*/ |
108
|
6 |
|
public function build() |
109
|
|
|
{ |
110
|
6 |
|
$rules = $this->rules; |
111
|
6 |
|
$this->rules = []; |
112
|
6 |
|
return $rules; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
} |
116
|
|
|
|