1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @author: Abdul Qureshi. <[email protected]> |
5
|
|
|
* |
6
|
|
|
* This file has been modified from the original source. |
7
|
|
|
* See original here: |
8
|
|
|
* |
9
|
|
|
* @link: https://github.com/progsmile/request-validator |
10
|
|
|
*/ |
11
|
|
|
namespace TheSupportGroup\Common\Validator\Rules; |
12
|
|
|
|
13
|
|
|
use TheSupportGroup\Common\ValidationInterop\ValidationProviderInterface; |
14
|
|
|
|
15
|
|
|
abstract class BaseRule |
16
|
|
|
{ |
17
|
|
|
const CONFIG_ALL = 'all'; |
18
|
|
|
const CONFIG_DATA = 'data'; |
19
|
|
|
const CONFIG_FIELD_RULES = 'fieldRules'; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var array $config |
23
|
|
|
*/ |
24
|
|
|
private $config; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var array $params |
28
|
|
|
*/ |
29
|
|
|
private $params; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var ValidationProviderInterface $validationProvider |
33
|
|
|
*/ |
34
|
|
|
private $validationProvider; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* BaseRule constructor. |
38
|
|
|
* |
39
|
|
|
* @param $config |
40
|
|
|
*/ |
41
|
|
|
public function __construct( |
42
|
|
|
$config, |
43
|
|
|
ValidationProviderInterface $validationProvider |
44
|
|
|
) { |
45
|
|
|
$this->config = $config; |
46
|
|
|
$this->validationProvider = $validationProvider; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Validator calls captured and remapped here. |
51
|
|
|
* |
52
|
|
|
* @return BaseRule Validator rule object. |
53
|
|
|
*/ |
54
|
|
|
public function __call($method, array $params = array()) |
55
|
|
|
{ |
56
|
|
|
// Get the mapping out of the mapper file. |
57
|
|
|
$validatorMethod = $this->validationProvider->getMappedMethod($method); |
58
|
|
|
|
59
|
|
|
// Try running rule against params. |
60
|
|
|
$this->buildRule($validatorMethod, $params); |
61
|
|
|
|
62
|
|
|
// Return self for next call to be made via the base Rule. |
63
|
|
|
return $this; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Run rule against validator. |
68
|
|
|
*/ |
69
|
|
|
private function buildRule($ruleName, $arguments = []) |
|
|
|
|
70
|
|
|
{ |
71
|
|
|
return $this->validationProvider->rule($ruleName, $arguments); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Validate rule against value provided. |
76
|
|
|
* We can control which method is called on the external library. |
77
|
|
|
*/ |
78
|
|
|
public function validate($value) |
|
|
|
|
79
|
|
|
{ |
80
|
|
|
return $this->validationProvider->validate($value); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Returns all config array, or specific one. |
85
|
|
|
* |
86
|
|
|
* @param string $type |
87
|
|
|
* |
88
|
|
|
* @return string |
89
|
|
|
*/ |
90
|
|
|
public function getConfig($type = self::CONFIG_ALL) |
91
|
|
|
{ |
92
|
|
|
if ($type == self::CONFIG_ALL) { |
93
|
|
|
return $this->config; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
return isset($this->config[$type]) ? $this->config[$type] : []; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* If field has specific rule. |
101
|
|
|
* |
102
|
|
|
* @param string $rule |
103
|
|
|
* |
104
|
|
|
* @return bool |
105
|
|
|
*/ |
106
|
|
|
public function hasRule($rule) |
107
|
|
|
{ |
108
|
|
|
return strpos($this->getConfig(self::CONFIG_FIELD_RULES), $rule) !== false; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Check if variable is not required - to prevent error messages from another validators. |
113
|
|
|
* |
114
|
|
|
* @param string $type | 'var' or 'file' |
115
|
|
|
* |
116
|
|
|
* @return bool |
117
|
|
|
*/ |
118
|
|
|
protected function isNotRequiredAndEmpty($type = 'var') |
|
|
|
|
119
|
|
|
{ |
120
|
|
|
$condition = false; |
121
|
|
|
|
122
|
|
|
if ($type == 'var') { |
123
|
|
|
$condition = strlen($this->params[1]) == 0; |
124
|
|
|
} elseif ($type == 'file') { |
125
|
|
|
$fieldsName = $this->params[0]; |
126
|
|
|
|
127
|
|
|
//when file field is not required and empty |
128
|
|
|
$condition = isset($_FILES[$fieldsName]['name']) && $_FILES[$fieldsName]['name'] == ''; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
return !$this->hasRule('required') && $condition; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Set params to validator. |
136
|
|
|
* |
137
|
|
|
* @param $params |
138
|
|
|
* |
139
|
|
|
* @return $this |
140
|
|
|
*/ |
141
|
|
|
public function setParams($params) |
142
|
|
|
{ |
143
|
|
|
$this->params = $params; |
144
|
|
|
|
145
|
|
|
return $this; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* Returns params. |
150
|
|
|
* |
151
|
|
|
* @return mixed |
152
|
|
|
*/ |
153
|
|
|
public function getParams() |
154
|
|
|
{ |
155
|
|
|
return $this->params; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* Returns pure class name. |
160
|
|
|
* |
161
|
|
|
* @return string |
162
|
|
|
*/ |
163
|
|
|
public function getRuleName() |
164
|
|
|
{ |
165
|
|
|
$classPath = explode('\\', get_class($this)); |
166
|
|
|
|
167
|
|
|
return array_pop($classPath); |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* Returns error message from rule. |
172
|
|
|
* |
173
|
|
|
* @return string |
174
|
|
|
*/ |
175
|
|
|
abstract public function getMessage(); |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* The main function that validates the inputted value against |
179
|
|
|
* an existing one or similar. |
180
|
|
|
* |
181
|
|
|
* @return bool Return a if values were valid/matched or not |
182
|
|
|
*/ |
183
|
|
|
abstract public function isValid(); |
184
|
|
|
} |
185
|
|
|
|
Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a
@return
annotation as described here.