1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
5
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
6
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
7
|
|
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
8
|
|
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
9
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
10
|
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
11
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
12
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
13
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
14
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
15
|
|
|
* |
16
|
|
|
* This software consists of voluntary contributions made by many individuals |
17
|
|
|
* and is licensed under the MIT license. For more information, see |
18
|
|
|
* <http://www.doctrine-project.org>. |
19
|
|
|
*/ |
20
|
|
|
|
21
|
|
|
namespace DoctrineModule\Validator\Service; |
22
|
|
|
|
23
|
|
|
use Zend\ServiceManager\FactoryInterface; |
24
|
|
|
use Interop\Container\ContainerInterface; |
25
|
|
|
use Zend\ServiceManager\ServiceLocatorInterface; |
26
|
|
|
use Zend\ServiceManager\ServiceLocatorAwareInterface; |
27
|
|
|
use DoctrineModule\Validator\Service\Exception\ServiceCreationException; |
28
|
|
|
use Zend\Stdlib\ArrayUtils; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Factory for creating NoObjectExists instances |
32
|
|
|
* |
33
|
|
|
* @license MIT |
34
|
|
|
* @link http://www.doctrine-project.org/ |
35
|
|
|
* @since 1.3.0 |
36
|
|
|
* @author Fabian Grutschus <[email protected]> |
37
|
|
|
*/ |
38
|
|
|
abstract class AbstractValidatorFactory implements FactoryInterface |
|
|
|
|
39
|
|
|
{ |
40
|
|
|
const DEFAULT_OBJECTMANAGER_KEY = 'doctrine.entitymanager.orm_default'; |
41
|
|
|
|
42
|
|
|
protected $creationOptions = []; |
43
|
|
|
|
44
|
|
|
protected $validatorClass; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @param ContainerInterface $container |
48
|
|
|
* @param array $options |
49
|
|
|
* @return \Doctrine\Common\Persistence\ObjectRepository |
50
|
|
|
* @throws ServiceCreationException |
51
|
|
|
*/ |
52
|
3 |
|
protected function getRepository(ContainerInterface $container, array $options) |
53
|
|
|
{ |
54
|
3 |
|
if (empty($options['target_class'])) { |
55
|
1 |
|
throw new ServiceCreationException(sprintf( |
56
|
1 |
|
"Option 'target_class' is missing when creating validator %s", |
57
|
|
|
__CLASS__ |
58
|
1 |
|
)); |
59
|
|
|
} |
60
|
|
|
|
61
|
2 |
|
$objectManager = $this->getObjectManager($container, $options); |
62
|
2 |
|
$targetClassName = $options['target_class']; |
63
|
2 |
|
$objectRepository = $objectManager->getRepository($targetClassName); |
64
|
|
|
|
65
|
2 |
|
return $objectRepository; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @param ContainerInterface $container |
70
|
|
|
* @param array $options |
71
|
|
|
* @return \Doctrine\Common\Persistence\ObjectManager |
72
|
|
|
*/ |
73
|
2 |
|
protected function getObjectManager(ContainerInterface $container, array $options) |
74
|
|
|
{ |
75
|
2 |
|
$objectManager = isset($options['object_manager']) ? $options['object_manager'] : self::DEFAULT_OBJECTMANAGER_KEY; |
76
|
2 |
|
if (is_string($objectManager)) { |
77
|
1 |
|
$objectManager = $container->get($objectManager); |
78
|
1 |
|
} |
79
|
|
|
|
80
|
2 |
|
return $objectManager; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @param array $options |
85
|
|
|
* @return array |
86
|
|
|
*/ |
87
|
2 |
|
protected function getFields(array $options) |
88
|
|
|
{ |
89
|
2 |
|
if (isset($options['fields'])) { |
90
|
2 |
|
return (array) $options['fields']; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
return []; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Helper to merge options array passed to `__invoke` |
98
|
|
|
* together with the options array created based on the above |
99
|
|
|
* helper methods. |
100
|
|
|
* |
101
|
|
|
* @param array $previousOptions |
102
|
|
|
* @param array $newOptions |
103
|
|
|
* @return array |
104
|
|
|
*/ |
105
|
1 |
|
protected function merge($previousOptions, $newOptions) |
106
|
|
|
{ |
107
|
1 |
|
return ArrayUtils::merge($previousOptions, $newOptions, true); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Helper method for ZF2 compatiblity. |
112
|
|
|
* |
113
|
|
|
* In ZF2 the plugin manager instance if passed to `createService` |
114
|
|
|
* instead of the global service manager instance (as in ZF3). |
115
|
|
|
* |
116
|
|
|
* @param ContainerInterface $container |
117
|
|
|
* @return ContainerInterface |
118
|
|
|
*/ |
119
|
2 |
|
protected function container(ContainerInterface $container) |
120
|
|
|
{ |
121
|
2 |
|
if ($container instanceof ServiceLocatorAwareInterface) { |
|
|
|
|
122
|
|
|
$container = $container->getServiceLocator(); |
123
|
|
|
} |
124
|
|
|
|
125
|
2 |
|
return $container; |
126
|
|
|
} |
127
|
|
|
|
128
|
1 |
|
public function createService(ServiceLocatorInterface $serviceLocator) |
129
|
|
|
{ |
130
|
1 |
|
return $this($serviceLocator, $this->validatorClass, $this->creationOptions); |
131
|
|
|
} |
132
|
|
|
|
133
|
1 |
|
public function setCreationOptions(array $options) |
134
|
|
|
{ |
135
|
1 |
|
$this->creationOptions = $options; |
136
|
1 |
|
} |
137
|
|
|
} |
138
|
|
|
|
This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.