Passed
Pull Request — master (#1)
by Saulius
02:19
created

OptionsResolver   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 32
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 32
ccs 12
cts 12
cp 1
rs 10
c 0
b 0
f 0
wmc 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A resolve() 0 7 1
A setDefaults() 0 3 1
A processDotNotatedValues() 0 9 3
1
<?php
2
/**
3
 * This file is part of the sauls/options-resolver package.
4
 *
5
 * @author    Saulius Vaičeliūnas <[email protected]>
6
 * @link      http://saulius.vaiceliunas.lt
7
 * @copyright 2018
8
 *
9
 * For the full copyright and license information, please view the LICENSE
10
 * file that was distributed with this source code.
11
 */
12
13
namespace Sauls\Component\OptionsResolver;
14
15
use function Sauls\Component\Helper\array_key_childs_exist;
16
use function Sauls\Component\Helper\array_keys_with_value;
17
use function Sauls\Component\Helper\array_remove_key;
18
use Symfony\Component\OptionsResolver\OptionsResolver as SymfonyOptionsResolver;
19
use Sauls\Component\Collection\ArrayCollection;
20
21
class OptionsResolver extends SymfonyOptionsResolver
22
{
23
    /**
24
     * @throws \Symfony\Component\OptionsResolver\Exception\AccessException
25
     * @throws \Exception
26
     */
27 7
    public function setDefaults(array $defaults)
28
    {
29 7
        return parent::setDefaults(array_keys_with_value($defaults));
30
    }
31
32
    /**
33
     * @throws \Exception
34
     */
35 9
    public function resolve(array $options = []): array
36
    {
37 9
        return (new ArrayCollection(
38 9
            $this->processDotNotatedValues(
39 9
                parent::resolve(array_keys_with_value($options))
40
            )
41 4
        ))->all();
42
    }
43
44 4
    private function processDotNotatedValues(array $resolvedValues): array
45
    {
46 4
        foreach ($resolvedValues as $key => $value) {
47 4
            if (array_key_childs_exist($key, $resolvedValues)) {
48 4
                array_remove_key($resolvedValues, $key);
49
            }
50
        }
51
52 4
        return $resolvedValues;
53
    }
54
55
}
56