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