1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @author Stefano Torresi (http://stefanotorresi.it) |
4
|
|
|
* @license See the file LICENSE.txt for copying permission. |
5
|
|
|
* ************************************************ |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace Thorr\Persistence\Validator; |
9
|
|
|
|
10
|
|
|
use Thorr\Persistence\DataMapper\Manager\DataMapperManagerInterface; |
11
|
|
|
use Zend\Validator\AbstractValidator; |
12
|
|
|
use Zend\Validator\Exception; |
13
|
|
|
|
14
|
|
|
abstract class AbstractEntityValidator extends AbstractValidator |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @var object |
18
|
|
|
*/ |
19
|
|
|
protected $finder; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var string |
23
|
|
|
*/ |
24
|
|
|
protected $findMethod; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var array |
28
|
|
|
*/ |
29
|
|
|
protected $excluded = []; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var DataMapperManagerInterface |
33
|
|
|
*/ |
34
|
|
|
protected $dataMapperManager; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @param array $options |
38
|
|
|
* @param DataMapperManagerInterface $dataMapperManager |
39
|
|
|
*/ |
40
|
14 |
|
public function __construct(array $options = null, DataMapperManagerInterface $dataMapperManager = null) |
41
|
|
|
{ |
42
|
14 |
|
$this->dataMapperManager = $dataMapperManager; |
43
|
|
|
|
44
|
14 |
|
if (! isset($options['finder']) && ! isset($options['entity_class'])) { |
45
|
1 |
|
throw new Exception\InvalidArgumentException('No finder nor entity class provided'); |
46
|
|
|
} |
47
|
|
|
|
48
|
13 |
|
if (isset($options['entity_class']) && $this->dataMapperManager) { |
49
|
2 |
|
$options['finder'] = $this->dataMapperManager->getDataMapperForEntity($options['entity_class']); |
50
|
2 |
|
} |
51
|
|
|
|
52
|
13 |
|
if (! is_object($options['finder']) && ! is_callable($options['finder'])) { |
53
|
1 |
|
throw new Exception\InvalidArgumentException('Finder must be an object or a callable'); |
54
|
|
|
} |
55
|
|
|
|
56
|
12 |
|
if (! isset($options['find_method'])) { |
57
|
10 |
|
$options['find_method'] = is_callable($options['finder']) ? '__invoke' : 'findByUuid'; |
58
|
10 |
|
} |
59
|
|
|
|
60
|
12 |
|
if (! method_exists($options['finder'], $options['find_method'])) { |
61
|
2 |
|
throw new Exception\InvalidArgumentException(sprintf( |
62
|
2 |
|
"'%s' method not found in '%s'", |
63
|
2 |
|
$options['find_method'], |
64
|
2 |
|
get_class($options['finder']) |
65
|
2 |
|
)); |
66
|
|
|
} |
67
|
|
|
|
68
|
10 |
|
$this->setFindMethod($options['find_method']); |
69
|
10 |
|
$this->setFinder($options['finder']); |
|
|
|
|
70
|
|
|
|
71
|
10 |
|
if (isset($options['excluded'])) { |
72
|
1 |
|
$this->setExcluded($options['excluded']); |
73
|
1 |
|
} |
74
|
|
|
|
75
|
10 |
|
parent::__construct($options); |
76
|
10 |
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @return mixed |
80
|
|
|
*/ |
81
|
1 |
|
public function getFindMethod() |
82
|
|
|
{ |
83
|
1 |
|
return $this->findMethod; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @param mixed $findMethod |
88
|
|
|
*/ |
89
|
10 |
|
public function setFindMethod($findMethod) |
90
|
|
|
{ |
91
|
10 |
|
$this->findMethod = $findMethod; |
92
|
10 |
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @return object |
96
|
|
|
*/ |
97
|
2 |
|
public function getFinder() |
98
|
|
|
{ |
99
|
2 |
|
return $this->finder; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @param object $finder |
104
|
|
|
*/ |
105
|
10 |
|
public function setFinder($finder) |
106
|
|
|
{ |
107
|
10 |
|
$this->finder = $finder; |
108
|
10 |
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @return array |
112
|
|
|
*/ |
113
|
1 |
|
public function getExcluded() |
114
|
|
|
{ |
115
|
1 |
|
return $this->excluded; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* @param mixed $excluded |
120
|
|
|
*/ |
121
|
1 |
|
public function setExcluded($excluded) |
122
|
|
|
{ |
123
|
1 |
|
$this->excluded = (array) $excluded; |
124
|
1 |
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* @param $value |
128
|
|
|
* |
129
|
|
|
* @return array |
130
|
|
|
*/ |
131
|
5 |
|
protected function findEntity($value) |
132
|
|
|
{ |
133
|
5 |
|
$result = $this->finder->{$this->findMethod}($value); |
134
|
|
|
|
135
|
5 |
|
if ($result === null) { |
136
|
1 |
|
return []; |
137
|
|
|
} |
138
|
|
|
|
139
|
5 |
|
return (array) $result; |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
|
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: