Passed
Push — master ( 1ec89a...1818e9 )
by Vincent
06:54 queued 04:35
created

CanUseDotPath::retrieve()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3.0987

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 8
nc 3
nop 2
dl 0
loc 14
ccs 7
cts 9
cp 0.7778
crap 3.0987
rs 10
c 1
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace VGirol\JsonApiStructure;
6
7
use VGirol\JsonApiStructure\Exception\DotPathException;
8
9
trait CanUseDotPath
10
{
11 441
    protected function retrieve($path, $array)
12
    {
13 441
        $segments = explode('.', $path);
14 441
        $value = $array;
15 441
        while ($segment = \array_shift($segments)) {
16 441
            if (!\array_key_exists($segment, $value)) {
17
                throw new DotPathException(
18
                    sprintf('Path "%s" is not valid : segment "%s" does not exists.', $path, $segment)
19
                );
20
            }
21 441
            $value = $value[$segment];
22
        }
23
24 441
        return $value;
25
    }
26
}
27