-
-
Notifications
You must be signed in to change notification settings - Fork 37
/
CoreMappingPlugin.php
78 lines (64 loc) · 2.58 KB
/
CoreMappingPlugin.php
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
<?php declare(strict_types = 1);
namespace Apitte\Core\DI\Plugin;
use Apitte\Core\Decorator\IDecorator;
use Apitte\Core\Decorator\RequestEntityDecorator;
use Apitte\Core\Decorator\RequestParametersDecorator;
use Apitte\Core\DI\ApiExtension;
use Apitte\Core\Mapping\Parameter\BooleanTypeMapper;
use Apitte\Core\Mapping\Parameter\DateTimeTypeMapper;
use Apitte\Core\Mapping\Parameter\FloatTypeMapper;
use Apitte\Core\Mapping\Parameter\IntegerTypeMapper;
use Apitte\Core\Mapping\Parameter\ScalarTypeMapper;
use Apitte\Core\Mapping\Parameter\StringTypeMapper;
use Apitte\Core\Mapping\RequestEntityMapping;
use Apitte\Core\Mapping\RequestParameterMapping;
use Apitte\Core\Mapping\Validator\IEntityValidator;
use Apitte\Core\Mapping\Validator\NullValidator;
class CoreMappingPlugin extends AbstractPlugin
{
public const PLUGIN_NAME = 'mapping';
/** @var mixed[] */
protected $defaults = [
'types' => [
'scalar' => ScalarTypeMapper::class,
'string' => StringTypeMapper::class,
'int' => IntegerTypeMapper::class,
'float' => FloatTypeMapper::class,
'bool' => BooleanTypeMapper::class,
'datetime' => DateTimeTypeMapper::class,
],
'request' => [
'validator' => NullValidator::class,
],
];
public function __construct(PluginCompiler $compiler)
{
parent::__construct($compiler);
$this->name = self::PLUGIN_NAME;
}
/**
* Register services
*/
public function loadPluginConfiguration(): void
{
$builder = $this->getContainerBuilder();
$config = $this->setupConfig($this->defaults, $this->getConfig());
$builder->addDefinition($this->prefix('request.parameters.decorator'))
->setFactory(RequestParametersDecorator::class)
->addTag(ApiExtension::CORE_DECORATOR_TAG, ['priority' => 100, 'type' => IDecorator::ON_HANDLER_BEFORE]);
$builder->addDefinition($this->prefix('request.entity.decorator'))
->setFactory(RequestEntityDecorator::class)
->addTag(ApiExtension::CORE_DECORATOR_TAG, ['priority' => 101, 'type' => IDecorator::ON_HANDLER_BEFORE]);
$parametersMapping = $builder->addDefinition($this->prefix('request.parameters.mapping'))
->setFactory(RequestParameterMapping::class);
foreach ($config['types'] as $type => $mapper) {
$parametersMapping->addSetup('addMapper', [$type, $mapper]);
}
$builder->addDefinition($this->prefix('request.entity.mapping.validator'))
->setType(IEntityValidator::class)
->setFactory($config['request']['validator']);
$builder->addDefinition($this->prefix('request.entity.mapping'))
->setFactory(RequestEntityMapping::class)
->addSetup('setValidator', ['@' . $this->prefix('request.entity.mapping.validator')]);
}
}