CanUseDotPath   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 23
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 8
dl 0
loc 23
ccs 8
cts 8
cp 1
rs 10
c 1
b 0
f 0
wmc 3

1 Method

Rating   Name   Duplication   Size   Complexity  
A retrieve() 0 12 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace VGirol\JsonApiStructure;
6
7
use VGirol\JsonApiStructure\Exception\DotPathException;
8
9
/**
10
 * This trait add the ability to retrieve values in arrays using dot notation.
11
 */
12
trait CanUseDotPath
13
{
14
    /**
15
     * Retrieve a value in an array using dot notation
16
     *
17
     * @param string $path
18
     * @param array $array
19
     *
20
     * @return mixed
21
     * @throws \VGirol\JsonApiStructure\Exception\DotPathException
22
     */
23 450
    public function retrieve(string $path, array $array)
24
    {
25 450
        $segments = explode('.', $path);
26 450
        $value = $array;
27 450
        while ($segment = \array_shift($segments)) {
28 450
            if (!\array_key_exists($segment, $value)) {
29 3
                throw new DotPathException($path, $segment);
30
            }
31 450
            $value = $value[$segment];
32
        }
33
34 447
        return $value;
35
    }
36
}
37