Test Failed
Push — master ( 39f870...3191d9 )
by Julien
13:20 queued 08:37
created

Config   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 78
Duplicated Lines 0 %

Test Coverage

Coverage 40.63%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 17
eloc 28
c 1
b 0
f 0
dl 0
loc 78
ccs 13
cts 32
cp 0.4063
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A internalMergeAppend() 0 15 6
A merge() 0 17 4
A resetModelClass() 0 3 1
A getModelClass() 0 3 2
A setModelClass() 0 3 1
A pathToArray() 0 13 3
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