|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Arp\LaminasDoctrine\Factory\Validator; |
|
6
|
|
|
|
|
7
|
|
|
use Arp\LaminasDoctrine\Repository\RepositoryManager; |
|
8
|
|
|
use Arp\LaminasDoctrine\Validator\AbstractEntityValidator; |
|
9
|
|
|
use Arp\LaminasFactory\AbstractFactory; |
|
10
|
|
|
use Laminas\ServiceManager\Exception\InvalidServiceException; |
|
11
|
|
|
use Laminas\ServiceManager\Exception\ServiceNotCreatedException; |
|
12
|
|
|
use Laminas\ServiceManager\Exception\ServiceNotFoundException; |
|
13
|
|
|
use Psr\Container\ContainerExceptionInterface; |
|
14
|
|
|
use Psr\Container\ContainerInterface; |
|
15
|
|
|
|
|
16
|
|
|
abstract class AbstractEntityValidatorFactory extends AbstractFactory |
|
17
|
|
|
{ |
|
18
|
|
|
/** |
|
19
|
|
|
* @return class-string<AbstractEntityValidator> |
|
|
|
|
|
|
20
|
|
|
*/ |
|
21
|
|
|
abstract protected function getDefaultClassName(): string; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @throws ServiceNotCreatedException |
|
25
|
|
|
* @throws InvalidServiceException |
|
26
|
|
|
* @throws ServiceNotFoundException |
|
27
|
|
|
* @throws ContainerExceptionInterface |
|
28
|
|
|
*/ |
|
29
|
|
|
public function __invoke( |
|
30
|
|
|
ContainerInterface $container, |
|
31
|
|
|
string $requestedName, |
|
32
|
|
|
array $options = null |
|
33
|
|
|
): AbstractEntityValidator { |
|
34
|
|
|
$options = $options ?? $this->getServiceOptions($container, $requestedName, 'validators'); |
|
35
|
|
|
|
|
36
|
|
|
$entityName = $options['entity_name'] ?? null; |
|
37
|
|
|
if (null === $entityName) { |
|
38
|
|
|
throw new ServiceNotCreatedException( |
|
39
|
|
|
sprintf( |
|
40
|
|
|
'The required \'entity_name\' configuration option is missing for service \'%s\'', |
|
41
|
|
|
$requestedName |
|
42
|
|
|
) |
|
43
|
|
|
); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
$fieldNames = $options['field_names'] ?? []; |
|
47
|
|
|
if (empty($fieldNames)) { |
|
48
|
|
|
throw new ServiceNotCreatedException( |
|
49
|
|
|
sprintf( |
|
50
|
|
|
'The required \'field_names\' configuration option is missing for service \'%s\'', |
|
51
|
|
|
$requestedName |
|
52
|
|
|
) |
|
53
|
|
|
); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** @var RepositoryManager $repositoryManager */ |
|
57
|
|
|
$repositoryManager = $this->getService($container, RepositoryManager::class, $requestedName); |
|
58
|
|
|
|
|
59
|
|
|
/** @var class-string<AbstractEntityValidator> $className */ |
|
60
|
|
|
$className = $options['class_name'] ?? $this->getDefaultClassName(); |
|
61
|
|
|
return new $className( |
|
62
|
|
|
$repositoryManager->get($entityName), |
|
63
|
|
|
$fieldNames, |
|
64
|
|
|
$options |
|
65
|
|
|
); |
|
66
|
|
|
} |
|
67
|
|
|
} |
|
68
|
|
|
|