AbstractResult::findInDescendent()   A
last analyzed

Complexity

Conditions 6
Paths 6

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 6
eloc 8
c 1
b 0
f 0
nc 6
nop 2
dl 0
loc 13
ccs 9
cts 9
cp 1
crap 6
rs 9.2222
1
<?php
2
3
declare(strict_types=1);
4
5
namespace PhpCfdi\Finkok\Services;
6
7
use InvalidArgumentException;
8
use stdClass;
9
10
abstract class AbstractResult
11
{
12
    /** @var stdClass */
13
    protected $data;
14
15
    /** @var stdClass */
16
    protected $root;
17
18 134
    public function __construct(stdClass $data, string ...$meanLocation)
19
    {
20 134
        $this->data = $data;
21 134
        $root = $this->findInDescendent($data, ...$meanLocation);
22 134
        if (! $root instanceof stdClass) {
23 1
            throw new InvalidArgumentException(
24 1
                sprintf('Unable to find mean object at /%s', implode('/', $meanLocation))
25 1
            );
26
        }
27 134
        $this->root = $root;
28
    }
29
30 3
    public function rawData(): stdClass
31
    {
32 3
        return clone $this->data;
33
    }
34
35
    /**
36
     * @param stdClass|array|mixed $haystack
37
     * @param string ...$location
38
     * @return mixed
39
     */
40 134
    protected function findInDescendent($haystack, string ...$location)
41
    {
42 134
        if (0 === count($location)) {
43 134
            return $haystack;
44
        }
45 134
        $search = array_shift($location);
46 134
        if (is_array($haystack)) {
47 2
            return (isset($haystack[$search])) ? $this->findInDescendent($haystack[$search], ...$location) : null;
48
        }
49 134
        if ($haystack instanceof stdClass) {
50 134
            return (isset($haystack->{$search})) ? $this->findInDescendent($haystack->{$search}, ...$location) : null;
51
        }
52 1
        throw new InvalidArgumentException('Cannot find descendent on non-array non-object haystack');
53
    }
54
55 106
    protected function get(string $keyword): string
56
    {
57 106
        return strval($this->root->{$keyword} ?? '');
58
    }
59
}
60