|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Polder Knowledge / entityservice-zend-validator (https://polderknowledge.com) |
|
4
|
|
|
* |
|
5
|
|
|
* @link https://github.com/polderknowledge/entityservice-zend-validator for the canonical source repository |
|
6
|
|
|
* @copyright Copyright (c) 2016 Polder Knowledge (https://polderknowledge.com) |
|
7
|
|
|
* @license https://github.com/polderknowledge/entityservice-zend-validator/blob/master/LICENSE.md MIT |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
namespace PolderKnowledge\EntityService\Validator; |
|
11
|
|
|
|
|
12
|
|
|
use PolderKnowledge\EntityService\EntityServiceInterface; |
|
13
|
|
|
use Zend\Validator\AbstractValidator; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Base class for validators using an EntityServiceInterface. |
|
17
|
|
|
*/ |
|
18
|
|
|
abstract class AbstractEntityValidator extends AbstractValidator |
|
19
|
|
|
{ |
|
20
|
|
|
/** |
|
21
|
|
|
* EntityService instance used for validation |
|
22
|
|
|
* |
|
23
|
|
|
* @var EntityServiceInterface |
|
24
|
|
|
*/ |
|
25
|
|
|
protected $entityService; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* method name that will be called on validation |
|
29
|
|
|
* |
|
30
|
|
|
* @var string Method to be called |
|
31
|
|
|
*/ |
|
32
|
|
|
protected $method = 'findBy'; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @var string Name of the field to search |
|
36
|
|
|
*/ |
|
37
|
|
|
protected $field = 'id'; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Set entity service used for validation |
|
41
|
|
|
* |
|
42
|
|
|
* @param EntityServiceInterface $entityService |
|
43
|
|
|
*/ |
|
44
|
15 |
|
public function setEntityService(EntityServiceInterface $entityService) |
|
45
|
|
|
{ |
|
46
|
15 |
|
$this->entityService = $entityService; |
|
47
|
15 |
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Sets the method option. |
|
51
|
|
|
* |
|
52
|
|
|
* @param string $method The name of the method to set. |
|
53
|
|
|
*/ |
|
54
|
9 |
|
public function setMethod($method) |
|
55
|
|
|
{ |
|
56
|
9 |
|
$this->method = $method; |
|
57
|
9 |
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Sets the field option. |
|
61
|
|
|
* |
|
62
|
|
|
* @param string $field The name of the field to set. |
|
63
|
|
|
*/ |
|
64
|
3 |
|
public function setField($field) |
|
65
|
|
|
{ |
|
66
|
3 |
|
$this->field = $field; |
|
67
|
3 |
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Will call the configured method on self::$entityService using the value as criteria |
|
71
|
|
|
* |
|
72
|
|
|
* @param mixed $value |
|
73
|
|
|
* @return object |
|
74
|
|
|
*/ |
|
75
|
21 |
|
protected function fetchResult($value) |
|
76
|
|
|
{ |
|
77
|
21 |
|
return call_user_func_array( |
|
78
|
|
|
array( |
|
79
|
21 |
|
$this->entityService, |
|
80
|
21 |
|
$this->method |
|
81
|
21 |
|
), |
|
82
|
|
|
array( |
|
83
|
21 |
|
'criteria' => array($this->field => $value) |
|
84
|
21 |
|
) |
|
85
|
21 |
|
); |
|
86
|
|
|
} |
|
87
|
|
|
} |
|
88
|
|
|
|