Completed
Pull Request — master (#5)
by Benjamin
04:57
created

ConfigResolver::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 4
rs 10
1
<?php
2
3
namespace Obblm\Core\Helper\Rule\Config;
4
5
use Symfony\Component\OptionsResolver\OptionsResolver;
6
7
class ConfigResolver
8
{
9
    /** @var OptionsResolver */
10
    private $resolver;
11
    /** @var ConfigResolver[] */
12
    private $children = [];
13
    public function __construct(ConfigInterface $configuration)
14
    {
15
        $this->resolver = new OptionsResolver();
16
        $this->configure($configuration);
17
    }
18
    private function configure($configuration)
19
    {
20
        $configuration->configureOptions($this->resolver);
21
        foreach ($configuration::getChildren() as $key => $childrenClass) {
22
            $children = new $childrenClass();
23
            if (!$children instanceof ConfigInterface) {
24
                $message = sprintf("The children must implements interface %s", ConfigInterface::class);
25
                throw new \Exception($message);
26
            }
27
            if ($children instanceof ConfigTreeInterface) {
28
                $this->children[$key] = new ConfigTreeResolver($children);
29
            } else {
30
                $this->children[$key] = new ConfigResolver($children);
31
            }
32
        }
33
    }
34
    public function resolve($data)
35
    {
36
        $resolved = $this->resolver->resolve($data);
37
        foreach ($this->children as $key => $children) {
38
            if ($children instanceof ConfigTreeResolver) {
39
                foreach ($resolved[$key] as $subKey => $subData) {
40
                    $resolved[$key][$subKey] = $children->resolve($subData);
41
                }
42
            } else {
43
                $resolved[$key] = $children->resolve($resolved[$key]);
44
            }
45
        }
46
        return $resolved;
47
    }
48
}
49