1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Enzyme\Freckle; |
4
|
|
|
|
5
|
|
|
class ArrayDotCollection implements DotCollectionInterface |
6
|
|
|
{ |
7
|
|
|
public function get($collection, $path) |
8
|
|
|
{ |
9
|
|
|
$parts = explode('.', $path); |
10
|
|
|
$top = count($parts) - 1; |
11
|
|
|
$index = 0; |
12
|
|
|
|
13
|
|
|
foreach ($collection as $key => $value) { |
14
|
|
|
// If we have only one path item left and this array key matches |
15
|
|
|
// it, let's immediately return its value. |
16
|
|
|
if ($key === $parts[0] && $index === $top) { |
17
|
|
|
return $value; |
18
|
|
|
} |
19
|
|
|
|
20
|
|
|
// If this array key matches the path item we're currently on, |
21
|
|
|
// let's dig in deeper and hope to find the full path. |
22
|
|
|
if ($key === $parts[0]) { |
23
|
|
|
// The next iteration will continue on the next available path |
24
|
|
|
// item chain. Eg: if we were on 'foo.bar.xzy', we'll be passing |
25
|
|
|
// in 'bar.xyz' to the recursive call. |
26
|
|
|
$new_path = array_slice($parts, 1); |
27
|
|
|
$new_path = implode('.', $new_path); |
28
|
|
|
|
29
|
|
|
return $this->get($value, $new_path); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
// If we're currently sitting on a numerical key, let's dig in |
33
|
|
|
// deeper in hopes to find some string keys to work with. |
34
|
|
|
if (true === is_int($key)) { |
35
|
|
|
// Keep our current path chain. |
36
|
|
|
$new_path = implode('.', $parts); |
37
|
|
|
$hit = $this->get($value, $new_path); |
38
|
|
|
|
39
|
|
|
// If we found a match futher down the rabbit hole, let's |
40
|
|
|
// return that value here, otherwise let's do another iteration. |
41
|
|
|
if (null !== $hit) { |
42
|
|
|
return $hit; |
43
|
|
|
} |
44
|
|
|
} |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
return null; |
48
|
|
|
} |
49
|
|
|
} |
50
|
|
|
|