|
1
|
|
|
<?php |
|
2
|
|
|
namespace Respect\Validation\Rules; |
|
3
|
|
|
|
|
4
|
|
|
use ReflectionClass; |
|
5
|
|
|
use Respect\Validation\Exceptions\ComponentException; |
|
6
|
|
|
|
|
7
|
|
|
class Zend extends AbstractRule |
|
8
|
|
|
{ |
|
9
|
|
|
protected $messages = array(); |
|
10
|
|
|
protected $zendValidator; |
|
11
|
|
|
|
|
12
|
9 |
|
public function __construct($validator, $params = array()) |
|
13
|
|
|
{ |
|
14
|
9 |
|
if (is_object($validator)) { |
|
15
|
2 |
|
return $this->zendValidator = $validator; |
|
|
|
|
|
|
16
|
|
|
} |
|
17
|
|
|
|
|
18
|
7 |
|
if (!is_string($validator)) { |
|
19
|
|
|
throw new ComponentException('Invalid Validator Construct'); |
|
20
|
|
|
} |
|
21
|
|
|
|
|
22
|
7 |
|
if (false === stripos($validator, 'Zend')) { |
|
23
|
6 |
|
$validator = "Zend\\Validator\\{$validator}"; |
|
24
|
6 |
|
} else { |
|
25
|
1 |
|
$validator = "\\{$validator}"; |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
7 |
|
$zendMirror = new ReflectionClass($validator); |
|
29
|
|
|
|
|
30
|
7 |
|
if ($zendMirror->hasMethod('__construct')) { |
|
31
|
7 |
|
$this->zendValidator = $zendMirror->newInstanceArgs($params); |
|
32
|
7 |
|
} else { |
|
33
|
|
|
$this->zendValidator = $zendMirror->newInstance(); |
|
34
|
|
|
} |
|
35
|
7 |
|
} |
|
36
|
|
|
|
|
37
|
3 |
|
public function assert($input) |
|
38
|
|
|
{ |
|
39
|
3 |
|
$validator = clone $this->zendValidator; |
|
40
|
|
|
|
|
41
|
3 |
|
if ($validator->isValid($input)) { |
|
42
|
1 |
|
return true; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
2 |
|
$exceptions = array(); |
|
46
|
2 |
|
foreach ($validator->getMessages() as $m) { |
|
47
|
2 |
|
$exceptions[] = $this->reportError($m, get_object_vars($this)); |
|
48
|
2 |
|
} |
|
49
|
|
|
|
|
50
|
2 |
|
throw $this->reportError($input)->setRelated($exceptions); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
3 |
|
public function validate($input) |
|
54
|
|
|
{ |
|
55
|
3 |
|
$validator = clone $this->zendValidator; |
|
56
|
|
|
|
|
57
|
3 |
|
return $validator->isValid($input); |
|
58
|
|
|
} |
|
59
|
|
|
} |
|
60
|
|
|
|