Test Failed
Push — master ( c9926a...5520e1 )
by Julien
04:42
created

Config::pathToArray()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3.2098

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 6
c 1
b 0
f 0
dl 0
loc 13
ccs 5
cts 7
cp 0.7143
rs 10
cc 3
nc 3
nop 3
crap 3.2098
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 1
    public function pathToArray(string $path, ?array $defaultValue = null, ?string $delimiter = null): ?array
22
    {
23 1
        $ret = $this->path($path, $defaultValue, $delimiter);
24
        
25 1
        if (is_null($ret)) {
26
            return null;
27
        }
28
        
29 1
        if ($ret instanceof PhalconConfigInterface) {
30 1
            return $ret->toArray();
31
        }
32
        
33
        return (array)$ret;
34
    }
35
    
36
    public function merge($toMerge, bool $append = false): PhalconConfigInterface
37
    {
38
        if (!$append) {
39
            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 1
    final protected function internalMergeAppend(array $source, array $target): array
56
    {
57 1
        foreach ($target as $key => $value) {
58 1
            if (is_array($value) && isset($source[$key]) && is_array($source[$key])) {
59 1
                $source[$key] = $this->internalMergeAppend($source[$key], $value);
60
            }
61 1
            elseif (is_int($key)) {
62 1
                $source[] = $value;
63
            }
64
            else {
65 1
                $source[$key] = $value;
66
            }
67
        }
68
        
69 1
        return $source;
70
    }
71
    
72
    /**
73
     * Return the mapped model class name from $this->models->$class
74
     */
75
    public function getModelClass(string $class): string
76
    {
77
        return $this->get('models')->get($class) ?: $class;
78
    }
79
    
80
    /**
81
     * Map a new model class
82
     */
83
    public function setModelClass(string $class, string $expected): void
84
    {
85
        $this->get('models')->set($class, $expected);
86
    }
87
    
88
    /**
89
     * Map a new model class
90
     */
91
    public function resetModelClass(string $class): void
92
    {
93
        $this->get('models')->set($class, $class);
94
    }
95
}
96