-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathManager.php
More file actions
127 lines (107 loc) · 3.88 KB
/
Manager.php
File metadata and controls
127 lines (107 loc) · 3.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
<?php
declare(strict_types=1);
namespace PayNL\Sdk\Mapper;
use PayNL\Sdk\{
Config\Config,
Exception\MapperSourceServiceNotFoundException,
Exception\MapperTargetServiceNotFoundException,
Service\AbstractPluginManager,
Service\Manager as ServiceManager,
Exception\ServiceNotFoundException,
Exception\ServiceNotCreatedException,
Util\Misc
};
/**
* Class Manager
*
* @package PayNL\Sdk\Mapper
*/
class Manager extends AbstractPluginManager
{
/**
* @var string
*
* @see AbstractPluginManager::$instanceOf
*/
protected $instanceOf = MapperInterface::class;
/**
* @var array
*/
protected $mapping = [];
/**
* @param array $config
* @return ServiceManager
* @throws \Psr\Container\ContainerExceptionInterface
* @throws \Psr\Container\NotFoundExceptionInterface
*/
public function configure(array $config): ServiceManager
{
parent::configure($config);
$currentMapping = new Config($this->mapping);
$mappingConfig = [];
if (false === empty($config['mapping'])) {
$mappingConfig = $config['mapping'];
foreach ($mappingConfig as $mapper => $map) {
// check the mapping keys, see if the class exists
$mapperAlias = $mapper;
$mapper = $this->resolvedAliases[$mapperAlias] ?? $mapperAlias;
if (false === class_exists($mapper)) {
throw new ServiceNotCreatedException(
sprintf(
'Can not initiate mapper object, class "%s" does not exist',
$mapper
)
);
}
$mappingConfig[$mapper] = $map;
unset($mappingConfig[$mapperAlias]);
# Determine the necessary managers
preg_match_all('/((?:^|[A-Z])[a-z]+)/', Misc::getClassNameByFQN($mapper), $matches);
if (2 > count($matches[1])) {
throw new ServiceNotFoundException(
sprintf(
'Mapper name "%s" is not valid',
$mapper
)
);
}
# Determine the name of the source and target
$sourceManagerName = lcfirst(current($matches[1]));
next($matches[1]);
$targetManagerName = lcfirst(current($matches[1]));
$sourceManager = $this->creationContext->get("{$sourceManagerName}Manager");
$targetManager = $this->creationContext->get("{$targetManagerName}Manager");
# Check the map
foreach ($map as $source => $target) {
if ($sourceManager->has($source) === false) {
throw new MapperSourceServiceNotFoundException(sprintf('Mapping source service with name "%s" not found in %s', $source, $sourceManagerName));
}
if ($targetManager->has($target) === false) {
throw new MapperTargetServiceNotFoundException(sprintf('Mapping target service with name "%s" not found in %s', $target, $targetManagerName));
}
}
$mappingConfig[$mapper] = $map;
}
}
$this->mapping = $currentMapping->merge(new Config($mappingConfig))->toArray();
return $this;
}
/**
* @param array $config
* @return void
*/
protected function validateOverrides(array $config): void
{
if (true === isset($config['mapping'])) {
$this->validateOverrideSet(array_keys($config['mapping']), 'mapping');
}
parent::validateOverrides($config);
}
/**
* @return array
*/
public function getMapping(): array
{
return $this->mapping;
}
}