1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the PHALCON-EXT package. |
5
|
|
|
* |
6
|
|
|
* (c) Jitendra Adhikari <[email protected]> |
7
|
|
|
* <https://github.com/adhocore> |
8
|
|
|
* |
9
|
|
|
* Licensed under MIT license. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace PhalconExt\Validation; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Validation rules helper. |
16
|
|
|
* |
17
|
|
|
* @author Jitendra Adhikari <[email protected]> |
18
|
|
|
* @license MIT |
19
|
|
|
* |
20
|
|
|
* @link https://github.com/adhocore/phalcon-ext |
21
|
|
|
*/ |
22
|
|
|
class Rules |
23
|
|
|
{ |
24
|
|
|
/** @var Validation */ |
25
|
|
|
protected $validation; |
26
|
|
|
|
27
|
|
|
public function __construct(Validation $validation) |
28
|
|
|
{ |
29
|
|
|
$this->validation = $validation; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Normalize rules if needed. |
34
|
|
|
* |
35
|
|
|
* @param mixed $rules |
36
|
|
|
* |
37
|
|
|
* @return array |
38
|
|
|
*/ |
39
|
|
|
public function normalizeRules($rules): array |
40
|
|
|
{ |
41
|
|
|
if (\is_string($rules)) { |
42
|
|
|
$rules = $this->parseRules($rules); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
if (!\is_array($rules)) { |
46
|
|
|
throw new \UnexpectedValueException('The rules should be an array or string'); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
return $this->cancelOnFail($rules); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Parse string representation of the rules and make it array. |
54
|
|
|
* |
55
|
|
|
* Rule Format: `rule1:key1:value11,value12;key2:value22|rule2:key21:value21|rule3` |
56
|
|
|
* |
57
|
|
|
* @param string $rules Example: 'required|length:min:1;max:2;|in:domain:1,12,30' |
58
|
|
|
* |
59
|
|
|
* @return array |
60
|
|
|
*/ |
61
|
|
|
protected function parseRules(string $rules): array |
62
|
|
|
{ |
63
|
|
|
$parsed = []; |
64
|
|
|
|
65
|
|
|
foreach (\explode('|', $rules) as $rule) { |
66
|
|
|
if (false === \strpos($rule, ':')) { |
67
|
|
|
$parsed[$rule] = []; |
68
|
|
|
continue; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
list($name, $options) = \explode(':', $rule, 2); |
72
|
|
|
$parsed[$name] = $this->parseOptions($options); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
return $parsed; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Parse rule options. |
80
|
|
|
* |
81
|
|
|
* @param string $options |
82
|
|
|
* |
83
|
|
|
* @return array |
84
|
|
|
*/ |
85
|
|
|
protected function parseOptions(string $options): array |
86
|
|
|
{ |
87
|
|
|
$parsed = []; |
88
|
|
|
|
89
|
|
|
foreach (\explode(';', $options) as $parts) { |
90
|
|
|
list($key, $value) = \explode(':', $parts) + ['', '']; |
91
|
|
|
if (\strpos($value, ',')) { |
92
|
|
|
$value = \explode(',', $value); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
$parsed[$key] = $value; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
return $parsed; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Make the validator cancel on fail i.e bail on first ever invalid field. |
103
|
|
|
* |
104
|
|
|
* @param array $rules |
105
|
|
|
* |
106
|
|
|
* @return array |
107
|
|
|
*/ |
108
|
|
|
protected function cancelOnFail(array $rules): array |
109
|
|
|
{ |
110
|
|
|
if (!isset($rules['abort'])) { |
111
|
|
|
return $rules; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
unset($rules['abort']); |
115
|
|
|
foreach ($rules as &$rule) { |
116
|
|
|
$rule = (array) $rule + ['cancelOnFail' => true]; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
return $rules; |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|