|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Jaeger |
|
4
|
|
|
* |
|
5
|
|
|
* @copyright Copyright (c) 2015-2016, mithra62 |
|
6
|
|
|
* @link http://jaeger-app.com |
|
7
|
|
|
* @version 1.0 |
|
8
|
|
|
* @filesource ./Validate.php |
|
9
|
|
|
*/ |
|
10
|
|
|
namespace JaegerApp; |
|
11
|
|
|
|
|
12
|
|
|
use Valitron\Validator; |
|
13
|
|
|
use JaegerApp\Exceptions\ValidateException; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* mithra62 - Validation Object |
|
17
|
|
|
* |
|
18
|
|
|
* Object to manage system settings |
|
19
|
|
|
* |
|
20
|
|
|
* @package Validate |
|
21
|
|
|
* @author Eric Lamb <[email protected]> |
|
22
|
|
|
*/ |
|
23
|
|
|
class Validate extends Validator |
|
24
|
|
|
{ |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* The regex engine |
|
28
|
|
|
* |
|
29
|
|
|
* @var \mithra62\Regex |
|
30
|
|
|
*/ |
|
31
|
|
|
protected $regex = null; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Overrides the initial validation constructor |
|
35
|
|
|
* |
|
36
|
|
|
* @throws \InvalidArgumentException |
|
37
|
|
|
*/ |
|
38
|
|
|
public function __construct() |
|
39
|
|
|
{ |
|
40
|
|
|
$langDir = static::langDir(); |
|
41
|
|
|
|
|
42
|
|
|
$lang = static::lang(); |
|
43
|
|
|
// Load language file in directory |
|
44
|
|
|
$langFile = rtrim($langDir, '/') . '/' . $lang . '.php'; |
|
45
|
|
|
if (stream_resolve_include_path($langFile)) { |
|
46
|
|
|
$langMessages = include $langFile; |
|
47
|
|
|
static::$_ruleMessages = array_merge(static::$_ruleMessages, $langMessages); |
|
48
|
|
|
} else { |
|
49
|
|
|
throw new \InvalidArgumentException("fail to load language file '$langFile'"); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
$this->loadRules(); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Adds all the custom rules |
|
57
|
|
|
* |
|
58
|
|
|
* @throws ValidateException |
|
59
|
|
|
*/ |
|
60
|
|
|
protected function loadRules() |
|
61
|
|
|
{ |
|
62
|
|
|
$old_cwd = getcwd(); |
|
63
|
|
|
chdir(__DIR__); |
|
64
|
|
|
$path = 'Validate' . DIRECTORY_SEPARATOR . 'Rules'; |
|
65
|
|
|
if (! is_dir($path)) { |
|
66
|
|
|
throw new ValidateException("Rules Directory " . $path . " isn't a directory..."); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
$files = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($path), \RecursiveIteratorIterator::LEAVES_ONLY); |
|
70
|
|
|
|
|
71
|
|
|
foreach ($files as $file) { |
|
72
|
|
|
if (! $file->isDir()) { |
|
73
|
|
|
$name = '\\JaegerApp\\' . str_replace('/', '\\', str_replace('.php', '', $file->getPathname())); |
|
74
|
|
|
$class = $name; |
|
75
|
|
|
if (class_exists($class)) { |
|
76
|
|
|
$rule = new $class(); |
|
77
|
|
|
if ($rule instanceof Validate\RuleInterface) { |
|
78
|
|
|
self::addRule($rule->getName(), array( |
|
79
|
|
|
$rule, |
|
80
|
|
|
'validate' |
|
81
|
|
|
), $rule->getErrorMessage()); |
|
|
|
|
|
|
82
|
|
|
} |
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
chdir($old_cwd); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* Processes the validation group |
|
92
|
|
|
* |
|
93
|
|
|
* @param array $data |
|
94
|
|
|
* @return \Valitron\boolean |
|
95
|
|
|
*/ |
|
96
|
|
|
public function val(array $data) |
|
97
|
|
|
{ |
|
98
|
|
|
$this->_fields = $data; |
|
99
|
|
|
$labels = array(); |
|
100
|
|
|
foreach ($data as $key => $value) { |
|
101
|
|
|
$labels[$key] = ucwords(str_replace('_', ' ', $key)); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
$this->labels($labels); |
|
105
|
|
|
|
|
106
|
|
|
return parent::validate(); |
|
|
|
|
|
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* Checks if there are errors and returns a boolean |
|
111
|
|
|
* |
|
112
|
|
|
* @return boolean |
|
113
|
|
|
*/ |
|
114
|
|
|
public function hasErrors() |
|
115
|
|
|
{ |
|
116
|
|
|
if (count($this->errors()) != '0') { |
|
117
|
|
|
return true; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
return false; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* Returns the error messages |
|
125
|
|
|
* |
|
126
|
|
|
* @return Ambigous <\Valitron\array, \Valitron\bool> |
|
127
|
|
|
*/ |
|
128
|
|
|
public function getErrorMessages() |
|
129
|
|
|
{ |
|
130
|
|
|
return $this->errors(); |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* Returns an array of all the custom rules |
|
135
|
|
|
* |
|
136
|
|
|
* @return array |
|
137
|
|
|
*/ |
|
138
|
|
|
public function getCustomRules() |
|
139
|
|
|
{ |
|
140
|
|
|
return static::$_rules; |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
/** |
|
144
|
|
|
* Sets the regular expression engine |
|
145
|
|
|
* |
|
146
|
|
|
* @param \mithra62\Regex $regex |
|
147
|
|
|
* @return \mithra62\Validate |
|
148
|
|
|
*/ |
|
149
|
|
|
public function setRegex(\JaegerApp\Regex $regex) |
|
150
|
|
|
{ |
|
151
|
|
|
$this->regex = $regex; |
|
|
|
|
|
|
152
|
|
|
return $this; |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
public function getRegex() |
|
156
|
|
|
{ |
|
157
|
|
|
return $this->regex; |
|
158
|
|
|
} |
|
159
|
|
|
} |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: