-
Notifications
You must be signed in to change notification settings - Fork 55
Expand file tree
/
Copy pathConditionalLogicRegistry.php
More file actions
109 lines (93 loc) · 3.41 KB
/
Copy pathConditionalLogicRegistry.php
File metadata and controls
109 lines (93 loc) · 3.41 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
<?php
/*
* This source file is available under two different licenses:
* - GNU General Public License version 3 (GPLv3)
* - DACHCOM Commercial License (DCL)
* Full copyright and license information is available in
* LICENSE.md which is distributed with this source code.
*
* @copyright Copyright (c) DACHCOM.DIGITAL AG (https://www.dachcom-digital.com)
* @license GPLv3 and DCL
*/
namespace FormBuilderBundle\Registry;
use FormBuilderBundle\Validation\ConditionalLogic\Rule\Action\ActionInterface;
use FormBuilderBundle\Validation\ConditionalLogic\Rule\Condition\ConditionInterface;
class ConditionalLogicRegistry
{
protected array $services = [
'action' => [],
'condition' => []
];
protected array $serviceConfiguration = [
'action' => [],
'condition' => []
];
private string $actionInterface;
private string $conditionInterface;
public function __construct(string $actionInterface, string $conditionInterface)
{
$this->actionInterface = $actionInterface;
$this->conditionInterface = $conditionInterface;
}
public function register(string $identifier, mixed $service, ?string $type = null, array $configuration = []): void
{
$allowedTypes = ['action', 'condition'];
if (!is_null($type) && !in_array($type, $allowedTypes)) {
throw new \InvalidArgumentException(
sprintf('%s must be a type of %s, "%s" given.', $identifier, implode(' or ', $allowedTypes), $type)
);
}
if (!is_null($service)) {
$interfaceReference = $type . 'Interface';
/* @phpstan-ignore-next-line */
if (!in_array($this->{$interfaceReference}, class_implements($service), true)) {
throw new \InvalidArgumentException(
/* @phpstan-ignore-next-line */
sprintf('%s needs to implement "%s", "%s" given.', get_class($service), $this->{$interfaceReference}, implode(', ', class_implements($service)))
);
}
}
$this->services[$type][$identifier] = $service;
$this->serviceConfiguration[$type][$identifier] = $configuration;
}
public function has(string $identifier, string $type): bool
{
return isset($this->services[$type], $this->services[$type][$identifier]);
}
/**
* @throws \Exception
*/
public function get(string $identifier, string $type): ConditionInterface|ActionInterface
{
if (!$this->has($identifier, $type)) {
throw new \Exception('"' . $identifier . '" validation service of type "' . $type . '" does not exist.');
}
return $this->services[$type][$identifier];
}
public function hasCondition(string $identifier): bool
{
return $this->has($identifier, 'condition');
}
/**
* @throws \Exception
*/
public function getCondition(string $identifier): ConditionInterface
{
return $this->get($identifier, 'condition');
}
public function hasAction(string $identifier): bool
{
return $this->has($identifier, 'action');
}
/**
* @throws \Exception
*/
public function getAction(string $identifier): ActionInterface
{
return $this->get($identifier, 'action');
}
public function getAllConfiguration(string $type): array
{
return $this->serviceConfiguration[$type];
}
}