Passed
Push — master ( ff8ca7...001f87 )
by Julien
05:55 queued 01:01
created

Config::pathToArray()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 6
c 1
b 0
f 0
nc 3
nop 3
dl 0
loc 13
ccs 7
cts 7
cp 1
crap 3
rs 10
1
<?php
2
3
/**
4
 * This file is part of the Zemit Framework.
5
 *
6
 * (c) Zemit Team <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE.txt
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Zemit\Config;
13
14
use Phalcon\Config\ConfigInterface as PhalconConfigInterface;
15
16
class Config extends \Phalcon\Config\Config implements ConfigInterface
17
{
18
    /**
19
     * Return the element as an array
20
     */
21 24
    public function pathToArray(string $path, ?array $defaultValue = null, ?string $delimiter = null): ?array
22
    {
23 24
        $ret = $this->path($path, $defaultValue, $delimiter);
24
        
25 24
        if (is_null($ret)) {
26 2
            return null;
27
        }
28
        
29 24
        if ($ret instanceof PhalconConfigInterface) {
30 24
            return $ret->toArray();
31
        }
32
        
33 1
        return (array)$ret;
34
    }
35
    
36 2
    public function merge($toMerge, bool $append = false): PhalconConfigInterface
37
    {
38 2
        if (!$append) {
39 2
            return parent::merge($toMerge);
40
        }
41
        
42
        $source = $this->toArray();
43
        $this->clear();
44
        $toMerge = $toMerge instanceof PhalconConfigInterface ? $toMerge->toArray() : $toMerge;
45
        
46
        if (!is_array($toMerge)) {
47
            throw new \Exception('Invalid data type for merge.');
48
        }
49
        
50
        $result = $this->internalMergeAppend($source, $toMerge);
51
        $this->init($result);
52
        return $this;
53
    }
54
    
55 24
    final protected function internalMergeAppend(array $source, array $target): array
56
    {
57 24
        foreach ($target as $key => $value) {
58 24
            if (is_array($value) && isset($source[$key]) && is_array($source[$key])) {
59 24
                $source[$key] = $this->internalMergeAppend($source[$key], $value);
60
            }
61 24
            elseif (is_int($key)) {
62 24
                $source[] = $value;
63
            }
64
            else {
65 24
                $source[$key] = $value;
66
            }
67
        }
68
        
69 24
        return $source;
70
    }
71
    
72
    /**
73
     * Return the mapped model class name from $this->models->$class
74
     */
75 3
    public function getModelClass(string $class): string
76
    {
77 3
        return $this->get('models')->get($class) ?: $class;
78
    }
79
    
80
    /**
81
     * Map a new model class
82
     */
83 1
    public function setModelClass(string $class, string $expected): void
84
    {
85 1
        $this->get('models')->set($class, $expected);
86
    }
87
    
88
    /**
89
     * Map a new model class
90
     */
91 1
    public function resetModelClass(string $class): void
92
    {
93 1
        $this->get('models')->set($class, $class);
94
    }
95
}
96