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 = array(); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Adds a rule 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
|
|
|
* addRule('myField', 'required') |
46
|
|
|
* Example for a rule with two parameters: |
47
|
|
|
* addRule('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 addRule($field, $rule) { |
59
|
|
|
if (!isset($this->rules[$field])) { |
60
|
|
|
$this->rules[$field] = array(); |
61
|
|
|
} |
62
|
|
|
$newRule = array($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
|
|
|
* Gets the created rules. |
73
|
|
|
* |
74
|
|
|
* @return array |
75
|
|
|
* the created rules |
76
|
|
|
*/ |
77
|
|
|
public function getRules() { |
78
|
|
|
return $this->rules; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
} |
82
|
|
|
|