1
|
|
|
<?php |
2
|
|
|
namespace Boekkooi\Bundle\JqueryValidationBundle\Form\Rule\Mapping; |
3
|
|
|
|
4
|
|
|
use Boekkooi\Bundle\JqueryValidationBundle\Exception\LogicException; |
5
|
|
|
use Boekkooi\Bundle\JqueryValidationBundle\Form\Rule\ConstraintRule; |
6
|
|
|
use Boekkooi\Bundle\JqueryValidationBundle\Form\RuleCollection; |
7
|
|
|
use Boekkooi\Bundle\JqueryValidationBundle\Form\Rule\ConstraintMapperInterface; |
8
|
|
|
use Boekkooi\Bundle\JqueryValidationBundle\Form\RuleMessage; |
9
|
|
|
use Symfony\Component\Form\FormInterface; |
10
|
|
|
use Symfony\Component\Validator\Constraint; |
11
|
|
|
use Symfony\Component\Validator\Constraints\Ip; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* @author Warnar Boekkooi <[email protected]> |
15
|
|
|
*/ |
16
|
|
|
class IpRule implements ConstraintMapperInterface |
17
|
|
|
{ |
18
|
|
|
const RULE_NAME_V4 = 'ipv4'; |
19
|
|
|
const RULE_NAME_V6 = 'ipv6'; |
20
|
|
|
const RULE_NAME_OR = 'one_or_other'; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var bool |
24
|
|
|
*/ |
25
|
|
|
private $useIpv4; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var bool |
29
|
|
|
*/ |
30
|
|
|
private $useIpv6; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var bool |
34
|
|
|
*/ |
35
|
|
|
private $useOrRule; |
36
|
|
|
|
37
|
21 |
|
public function __construct($useIpv4, $useIpv6, $useOrRule) |
38
|
|
|
{ |
39
|
21 |
|
$this->useIpv4 = $useIpv4; |
40
|
21 |
|
$this->useIpv6 = $useIpv6; |
41
|
21 |
|
$this->useOrRule = $useOrRule; |
42
|
21 |
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* {@inheritdoc} |
46
|
|
|
*/ |
47
|
17 |
|
public function resolve(Constraint $constraint, FormInterface $form, RuleCollection $collection) |
48
|
|
|
{ |
49
|
17 |
|
if (!$this->supports($constraint, $form)) { |
50
|
2 |
|
throw new LogicException(); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** @var Ip $constraint */ |
54
|
15 |
|
$ruleOptions = true; |
55
|
15 |
|
switch ($constraint->version) { |
56
|
15 |
|
case Ip::V4: |
57
|
15 |
|
case Ip::V4_NO_PRIV: |
58
|
15 |
|
case Ip::V4_NO_RES: |
59
|
15 |
|
case Ip::V4_ONLY_PUBLIC: |
60
|
6 |
|
if (!$this->useIpv4) { |
61
|
1 |
|
return; |
62
|
|
|
} |
63
|
5 |
|
$ruleName = self::RULE_NAME_V4; |
64
|
5 |
|
break; |
65
|
|
|
|
66
|
9 |
|
case Ip::V6: |
67
|
9 |
|
case Ip::V6_NO_PRIV: |
68
|
9 |
|
case Ip::V6_NO_RES: |
69
|
9 |
|
case Ip::V6_ONLY_PUBLIC: |
70
|
5 |
|
if (!$this->useIpv6) { |
71
|
1 |
|
return; |
72
|
|
|
} |
73
|
4 |
|
$ruleName = self::RULE_NAME_V6; |
74
|
4 |
|
break; |
75
|
|
|
|
76
|
4 |
|
case Ip::ALL: |
77
|
4 |
|
case Ip::ALL_NO_PRIV: |
78
|
4 |
|
case Ip::ALL_NO_RES: |
79
|
4 |
|
case Ip::ALL_ONLY_PUBLIC: |
80
|
4 |
|
if (!$this->useOrRule || !$this->useIpv6 || !$this->useIpv4) { |
81
|
3 |
|
return; |
82
|
|
|
} |
83
|
1 |
|
$ruleName = self::RULE_NAME_OR; |
84
|
1 |
|
$ruleOptions = array(self::RULE_NAME_V4 => true, self::RULE_NAME_V6 => true); |
85
|
1 |
|
break; |
86
|
|
|
default: |
87
|
|
|
return; |
88
|
10 |
|
} |
89
|
|
|
|
90
|
10 |
|
$collection->set( |
91
|
10 |
|
'ip', |
92
|
10 |
|
new ConstraintRule( |
93
|
10 |
|
$ruleName, |
94
|
10 |
|
$ruleOptions, |
95
|
10 |
|
new RuleMessage($constraint->message), |
96
|
10 |
|
$constraint->groups |
97
|
10 |
|
) |
98
|
10 |
|
); |
99
|
10 |
|
} |
100
|
|
|
|
101
|
21 |
|
public function supports(Constraint $constraint, FormInterface $form) |
102
|
|
|
{ |
103
|
21 |
|
return get_class($constraint) === Ip::class && |
104
|
21 |
|
($this->useIpv6 || $this->useIpv4); |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|