Passed
Push — master ( abee1f...edc958 )
by Kirill
03:08
created

DotTrait::dotGet()   A

Complexity

Conditions 6
Paths 8

Size

Total Lines 17
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 10
dl 0
loc 17
rs 9.2222
c 0
b 0
f 0
cc 6
nc 8
nop 2
1
<?php
2
3
/**
4
 * Spiral Framework.
5
 *
6
 * @license   MIT
7
 * @author    Anton Titov (Wolfy-J)
8
 */
9
10
declare(strict_types=1);
11
12
namespace Spiral\Config\Patch\Traits;
13
14
use Spiral\Config\Exception\DotNotFoundException;
15
16
trait DotTrait
17
{
18
    /**
19
     * @param array  $data Pointer.
20
     * @param string $name
21
     * @return array|mixed
22
     */
23
    private function &dotGet(array &$data, string $name)
24
    {
25
        //Generating path relative to a given name and prefix
26
        $path = (!empty($this->prefix) ? $this->prefix . '.' : '') . $name;
27
        if (empty($path)) {
28
            return $data;
29
        }
30
31
        $path = explode('.', rtrim($path, '.'));
32
        foreach ($path as $step) {
33
            if (!is_array($data) || !array_key_exists($step, $data)) {
34
                throw new DotNotFoundException("Unable to find config element '{$name}'.");
35
            }
36
            $data = &$data[$step];
37
        }
38
39
        return $data;
40
    }
41
}
42