|
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
|
|
|
declare(strict_types=1); |
|
13
|
|
|
|
|
14
|
|
|
namespace Respect\Validation\Rules; |
|
15
|
|
|
|
|
16
|
|
|
use ReflectionClass; |
|
17
|
|
|
use Respect\Validation\Exceptions\ComponentException; |
|
18
|
|
|
use Respect\Validation\Exceptions\ZendException; |
|
19
|
|
|
use function get_object_vars; |
|
20
|
|
|
use function is_object; |
|
21
|
|
|
use function is_string; |
|
22
|
|
|
use function mb_stripos; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Use Zend validators inside Respect\Validation flow. Messages are preserved. |
|
26
|
|
|
* |
|
27
|
|
|
* @author Alexandre Gomes Gaigalas <[email protected]> |
|
28
|
|
|
* @author Danilo Correa <[email protected]> |
|
29
|
|
|
* @author Henrique Moody <[email protected]> |
|
30
|
|
|
* @author Hugo Hamon <[email protected]> |
|
31
|
|
|
*/ |
|
32
|
|
|
final class Zend extends AbstractRule |
|
33
|
|
|
{ |
|
34
|
|
|
/** |
|
35
|
|
|
* @var mixed |
|
36
|
|
|
*/ |
|
37
|
|
|
private $zendValidator; |
|
38
|
|
|
|
|
39
|
1 |
|
public function __construct($validator, $params = []) |
|
40
|
|
|
{ |
|
41
|
1 |
|
if (is_object($validator)) { |
|
42
|
|
|
$this->zendValidator = $validator; |
|
43
|
|
|
return; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
1 |
|
if (!is_string($validator)) { |
|
47
|
|
|
throw new ComponentException('Invalid Validator Construct'); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
1 |
|
if (false === mb_stripos($validator, 'Zend')) { |
|
51
|
1 |
|
$validator = "Zend\\Validator\\{$validator}"; |
|
52
|
|
|
} else { |
|
53
|
|
|
$validator = "\\{$validator}"; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
1 |
|
$zendMirror = new ReflectionClass($validator); |
|
57
|
|
|
|
|
58
|
1 |
|
if ($zendMirror->hasMethod('__construct')) { |
|
59
|
1 |
|
$this->zendValidator = $zendMirror->newInstanceArgs($params); |
|
60
|
|
|
} else { |
|
61
|
|
|
$this->zendValidator = $zendMirror->newInstance(); |
|
62
|
|
|
} |
|
63
|
1 |
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* {@inheritdoc} |
|
67
|
|
|
*/ |
|
68
|
1 |
|
public function assert($input): void |
|
69
|
|
|
{ |
|
70
|
1 |
|
$validator = clone $this->zendValidator; |
|
71
|
|
|
|
|
72
|
1 |
|
if ($validator->isValid($input)) { |
|
73
|
|
|
return; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
$exceptions = array_map(function ($message) { |
|
77
|
1 |
|
return $this->reportError($message, get_object_vars($this)); |
|
78
|
1 |
|
}, $validator->getMessages()); |
|
79
|
|
|
|
|
80
|
|
|
/** @var ZendException $zendException */ |
|
81
|
1 |
|
$zendException = $this->reportError($input); |
|
82
|
1 |
|
$zendException->addChildren($exceptions); |
|
83
|
|
|
|
|
84
|
1 |
|
throw $zendException; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* {@inheritdoc} |
|
89
|
|
|
*/ |
|
90
|
6 |
|
public function validate($input): bool |
|
91
|
|
|
{ |
|
92
|
6 |
|
$validator = clone $this->zendValidator; |
|
93
|
6 |
|
return $validator->isValid($input); |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
|