1
|
|
|
<?php |
2
|
|
|
namespace Boekkooi\Bundle\JqueryValidationBundle\Form; |
3
|
|
|
|
4
|
|
|
use Boekkooi\Bundle\JqueryValidationBundle\Exception\InvalidArgumentException; |
5
|
|
|
|
6
|
|
|
/** |
7
|
|
|
* @author Warnar Boekkooi <[email protected]> |
8
|
|
|
*/ |
9
|
|
|
class FormRuleContext |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* @var RuleCollection[] { Key: form name, Value: RuleCollection[] } |
13
|
|
|
*/ |
14
|
|
|
protected $rules = array(); |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @var array { Key: form name, Value: array[] } |
18
|
|
|
*/ |
19
|
|
|
protected $groups = array(); |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var string[] { Value: button name } |
23
|
|
|
*/ |
24
|
|
|
protected $buttons = array(); |
25
|
|
|
|
26
|
|
|
public function __construct(array $rules, array $groups, array $buttons) |
27
|
|
|
{ |
28
|
|
|
foreach ($groups as $formGroups) { |
29
|
|
|
$validGroups = array_filter($formGroups, array($this, 'isValidGroup')); |
30
|
|
|
if (count($validGroups) !== count($formGroups)) { |
31
|
|
|
throw new InvalidArgumentException('Invalid groups given.'); |
32
|
|
|
} |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
$this->rules = $rules; |
36
|
|
|
$this->groups = $groups; |
37
|
|
|
$this->buttons = $buttons; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Gets a rule list by name. |
42
|
|
|
* |
43
|
|
|
* @param string $name The form full_name |
44
|
|
|
* @return RuleCollection|null A array of Rule instances or null when not found |
45
|
|
|
*/ |
46
|
|
|
public function get($name) |
47
|
|
|
{ |
48
|
|
|
return isset($this->rules[$name]) ? $this->rules[$name] : null; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Returns all rules in this collection. |
53
|
|
|
* |
54
|
|
|
* @return RuleCollection[] An array of rules |
55
|
|
|
*/ |
56
|
|
|
public function all() |
57
|
|
|
{ |
58
|
|
|
return $this->rules; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
public function getGroups() |
62
|
|
|
{ |
63
|
|
|
return $this->groups; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @param $name |
68
|
|
|
* @return null|array |
69
|
|
|
*/ |
70
|
|
|
public function getGroup($name) |
71
|
|
|
{ |
72
|
|
|
return isset($this->groups[$name]) ? $this->groups[$name] : null; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
public function getButtons() |
76
|
|
|
{ |
77
|
|
|
return $this->buttons; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
protected function isValidGroup($value) |
81
|
|
|
{ |
82
|
|
|
return |
83
|
|
|
// Callable |
84
|
|
|
!is_string($value) && is_callable($value) || |
85
|
|
|
// False is allowed to deactivate validation |
86
|
|
|
is_bool($value) && $value === false || |
87
|
|
|
// String |
88
|
|
|
is_string($value) || |
89
|
|
|
// Int |
90
|
|
|
is_int($value); |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|