Total Complexity | 10 |
Total Lines | 48 |
Duplicated Lines | 0 % |
Coverage | 100% |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
1 | <?php |
||
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 |
|
58 | } |
||
59 | } |
||
60 |