|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of Respect/Validation. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Alexandre Gomes Gaigalas <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the "LICENSE.md" |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Respect\Validation\Rules; |
|
13
|
|
|
|
|
14
|
|
|
use ReflectionClass; |
|
15
|
|
|
use Respect\Validation\Exceptions\ComponentException; |
|
16
|
|
|
|
|
17
|
|
|
class Zend extends AbstractRule |
|
18
|
|
|
{ |
|
19
|
|
|
protected $messages = []; |
|
20
|
|
|
protected $zendValidator; |
|
21
|
|
|
|
|
22
|
9 |
|
public function __construct($validator, $params = []) |
|
23
|
|
|
{ |
|
24
|
9 |
|
if (is_object($validator)) { |
|
25
|
2 |
|
return $this->zendValidator = $validator; |
|
|
|
|
|
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
7 |
|
if (!is_string($validator)) { |
|
29
|
|
|
throw new ComponentException('Invalid Validator Construct'); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
7 |
|
if (false === stripos($validator, 'Zend')) { |
|
33
|
6 |
|
$validator = "Zend\\Validator\\{$validator}"; |
|
34
|
|
|
} else { |
|
35
|
1 |
|
$validator = "\\{$validator}"; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
7 |
|
$zendMirror = new ReflectionClass($validator); |
|
39
|
|
|
|
|
40
|
7 |
|
if ($zendMirror->hasMethod('__construct')) { |
|
41
|
7 |
|
$this->zendValidator = $zendMirror->newInstanceArgs($params); |
|
42
|
|
|
} else { |
|
43
|
|
|
$this->zendValidator = $zendMirror->newInstance(); |
|
44
|
|
|
} |
|
45
|
7 |
|
} |
|
46
|
|
|
|
|
47
|
3 |
|
public function assert($input) |
|
48
|
|
|
{ |
|
49
|
3 |
|
$validator = clone $this->zendValidator; |
|
50
|
|
|
|
|
51
|
3 |
|
if ($validator->isValid($input)) { |
|
52
|
1 |
|
return true; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
2 |
|
$exceptions = []; |
|
56
|
2 |
|
foreach ($validator->getMessages() as $m) { |
|
57
|
2 |
|
$exceptions[] = $this->reportError($m, get_object_vars($this)); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
2 |
|
throw $this->reportError($input)->setRelated($exceptions); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
3 |
|
public function validate($input) |
|
64
|
|
|
{ |
|
65
|
3 |
|
$validator = clone $this->zendValidator; |
|
66
|
|
|
|
|
67
|
3 |
|
return $validator->isValid($input); |
|
68
|
|
|
} |
|
69
|
|
|
} |
|
70
|
|
|
|