-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAbstractContainerType.php
More file actions
79 lines (69 loc) · 2.83 KB
/
AbstractContainerType.php
File metadata and controls
79 lines (69 loc) · 2.83 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
<?php
/*
* This file is part of the enhavo package.
*
* (c) WE ARE INDEED GmbH
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Enhavo\Component\Type;
use Symfony\Component\OptionsResolver\Exception\InvalidOptionsException;
use Symfony\Component\OptionsResolver\Exception\MissingOptionsException;
use Symfony\Component\OptionsResolver\Exception\UndefinedOptionsException;
use Symfony\Component\OptionsResolver\OptionsResolver;
abstract class AbstractContainerType
{
protected TypeInterface $type;
/** @var TypeInterface[] */
protected array $parents;
protected array $options;
protected ?string $key;
/** @var TypeExtensionInterface[] */
protected array $extensions;
/**
* AbstractContainerType constructor.
*
* @param TypeInterface[] $parents
*/
public function __construct(TypeInterface $type, array $parents, array $options, ?string $key = null, array $extensions = [])
{
$this->type = $type;
$this->parents = $parents;
$this->key = $key;
$this->extensions = $extensions;
$resolver = new OptionsResolver();
foreach ($this->parents as $parent) {
$parent->configureOptions($resolver);
foreach ($this->extensions as $extension) {
if ($this->isExtendable($parent, $extension)) {
$extension->configureOptions($resolver);
}
}
}
$this->type->configureOptions($resolver);
foreach ($this->extensions as $extension) {
if ($this->isExtendable($this->type, $extension)) {
$extension->configureOptions($resolver);
}
}
try {
$this->options = $resolver->resolve($options);
} catch (MissingOptionsException $exception) {
throw new MissingOptionsException(sprintf('%s (%s): %s', get_class($type), $this->key, $exception->getMessage()), $exception->getCode(), $exception);
} catch (InvalidOptionsException $exception) {
throw new InvalidOptionsException(sprintf('%s (%s): %s', get_class($type), $this->key, $exception->getMessage()), $exception->getCode(), $exception);
} catch (UndefinedOptionsException $exception) {
throw new UndefinedOptionsException(sprintf('%s (%s): %s', get_class($type), $this->key, $exception->getMessage()), $exception->getCode(), $exception);
}
}
protected function isExtendable(TypeInterface $type, TypeExtensionInterface $extension): bool
{
foreach ($extension::getExtendedTypes() as $extendedType) {
if ($type::class === $extendedType || ($type::getName() && $type::getName() === $extendedType)) {
return true;
}
}
return false;
}
}