1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Psi\Component\Description\Schema; |
6
|
|
|
|
7
|
|
|
use Psi\Component\Description\DescriptorInterface; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Description schema defines which descriptors are allowed to be |
11
|
|
|
* set on the description. |
12
|
|
|
*/ |
13
|
|
|
class Schema |
14
|
|
|
{ |
15
|
|
|
private $definitions; |
16
|
|
|
|
17
|
|
|
public function __construct(array $extensions) |
18
|
|
|
{ |
19
|
|
|
foreach ($extensions as $extension) { |
20
|
|
|
$this->register($extension); |
21
|
|
|
} |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Ensure that a given key is valid. |
26
|
|
|
* |
27
|
|
|
* This is called when the user attempts to access a descriptor. |
28
|
|
|
*/ |
29
|
|
View Code Duplication |
public function validateKey(string $key) |
|
|
|
|
30
|
|
|
{ |
31
|
|
|
if (false === isset($this->definitions[$key])) { |
32
|
|
|
throw new \InvalidArgumentException(sprintf( |
33
|
|
|
'Descriptor "%s" not a permitted descriptor key. Did you make a typo? Permitted descriptors: "%s"', |
34
|
|
|
$key, |
35
|
|
|
implode('", "', array_keys($this->definitions)) |
36
|
|
|
)); |
37
|
|
|
} |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Validate the descriptor key AND ensures that the given |
42
|
|
|
* descriptor is of the correct type according to the schema. |
43
|
|
|
* |
44
|
|
|
* This is called when a descriptor is set on the description. |
45
|
|
|
*/ |
46
|
|
|
public function validate(string $key, DescriptorInterface $descriptor) |
47
|
|
|
{ |
48
|
|
|
$this->validateKey($key); |
49
|
|
|
$definition = $this->definitions[$key]; |
50
|
|
|
|
51
|
|
|
if (get_class($descriptor) !== $definition->getClass()) { |
52
|
|
|
throw new \InvalidArgumentException(sprintf( |
53
|
|
|
'Descriptor with key "%s" must be of class "%s", got "%s"', |
54
|
|
|
$key, |
55
|
|
|
$definition->getClass(), |
56
|
|
|
get_class($descriptor) |
57
|
|
|
)); |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Return the schema definitions. |
63
|
|
|
* |
64
|
|
|
* @return Definition[] |
65
|
|
|
*/ |
66
|
|
|
public function getDefinitions(): array |
67
|
|
|
{ |
68
|
|
|
return $this->definitions; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public function getDefinition($key): Definition |
72
|
|
|
{ |
73
|
|
|
if (!isset($this->definitions[$key])) { |
74
|
|
|
throw new \InvalidArgumentException(sprintf( |
75
|
|
|
'Unknown definition "%s"', $key |
76
|
|
|
)); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
return $this->definitions[$key]; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
private function register(ExtensionInterface $extension) |
83
|
|
|
{ |
84
|
|
|
$builder = new Builder(get_class($extension)); |
85
|
|
|
$extension->buildSchema($builder); |
86
|
|
|
|
87
|
|
|
foreach ($builder->getDefinitions() as $definition) { |
88
|
|
|
$key = sprintf('%s.%s', $extension->getName(), $definition->getKey()); |
89
|
|
|
|
90
|
|
|
if (isset($this->definitions[$key])) { |
91
|
|
|
throw new \InvalidArgumentException(sprintf( |
92
|
|
|
'Descriptor key "%s" for was already registered by "%s" extension', |
93
|
|
|
$key, $this->definitions[$key]->getExtensionClass() |
94
|
|
|
)); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
$this->definitions[$key] = $definition; |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.