Completed
Push — master ( 8de48f...51cd42 )
by Saulius
11s queued 10s
created

OptionsResolver::resolve()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 4
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 7
ccs 5
cts 5
cp 1
crap 1
rs 10
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 Exception;
16
use Sauls\Component\Collection\ArrayCollection;
17
use Symfony\Component\OptionsResolver\Exception\AccessException;
18
use Symfony\Component\OptionsResolver\OptionsResolver as SymfonyOptionsResolver;
19
20
use function Sauls\Component\Helper\array_key_childs_exist;
21
use function Sauls\Component\Helper\array_keys_with_value;
22
use function Sauls\Component\Helper\array_remove_key;
23
24
class OptionsResolver extends SymfonyOptionsResolver
25
{
26
    /**
27
     * @throws AccessException
28
     * @throws Exception
29
     */
30 7
    public function setDefaults(array $defaults)
31
    {
32 7
        return parent::setDefaults(array_keys_with_value($defaults));
33
    }
34
35
    /**
36
     * @throws Exception
37
     */
38 9
    public function resolve(array $options = []): array
39
    {
40 9
        return (new ArrayCollection(
41 9
            $this->processDotNotatedValues(
42 9
                parent::resolve(array_keys_with_value($options))
43
            )
44 4
        ))->all();
45
    }
46
47 4
    private function processDotNotatedValues(array $resolvedValues): array
48
    {
49 4
        foreach ($resolvedValues as $key => $value) {
50 4
            if (array_key_childs_exist($key, $resolvedValues)) {
51 1
                array_remove_key($resolvedValues, $key);
52
            }
53
        }
54
55 4
        return $resolvedValues;
56
    }
57
}
58