1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace HSkrasek\OpenAPI; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Get an item from an array or object using "dot" notation. |
7
|
|
|
* |
8
|
|
|
* @param mixed $target |
9
|
|
|
* @param string|array $key |
10
|
|
|
* @param mixed $default |
11
|
|
|
* |
12
|
|
|
* @return mixed |
13
|
|
|
* |
14
|
|
|
* @source Laravel (Thanks Taylor!) |
15
|
|
|
*/ |
16
|
|
|
function data_get($target, $key, $default = null) |
17
|
|
|
{ |
18
|
138 |
|
if (null === $key) { |
19
|
|
|
return $target; |
20
|
|
|
} |
21
|
|
|
|
22
|
138 |
|
$key = \is_array($key) ? $key : explode('.', $key); |
23
|
|
|
|
24
|
138 |
|
while (null !== $segment = array_shift($key)) { |
25
|
138 |
|
if (\is_array($target) && array_key_exists($segment, $target)) { |
26
|
|
|
$target = $target[$segment]; |
27
|
138 |
|
} elseif (\is_object($target) && isset($target->{$segment})) { |
28
|
138 |
|
$target = $target->{$segment}; |
29
|
|
|
} else { |
30
|
138 |
|
return $default; |
31
|
|
|
} |
32
|
|
|
} |
33
|
|
|
|
34
|
138 |
|
return $target; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Set an item on an array or object using dot notation. |
39
|
|
|
* |
40
|
|
|
* @param mixed $target |
41
|
|
|
* @param string|array $key |
42
|
|
|
* @param mixed $value |
43
|
|
|
* @param bool $overwrite |
44
|
|
|
* |
45
|
|
|
* @return mixed |
46
|
|
|
* |
47
|
|
|
* @source Laravel (Thanks Taylor!) |
48
|
|
|
*/ |
49
|
|
|
function data_set(&$target, $key, $value, $overwrite = true) |
50
|
|
|
{ |
51
|
138 |
|
$segments = \is_array($key) ? $key : explode('.', $key); |
52
|
|
|
|
53
|
138 |
|
$segment = array_shift($segments); |
54
|
|
|
|
55
|
138 |
|
if (\is_array($target)) { |
56
|
|
|
if ($segments) { |
|
|
|
|
57
|
|
|
if (!array_key_exists($segment, $target)) { |
58
|
|
|
$target[$segment] = []; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
data_set($target[$segment], $segments, $value, $overwrite); |
62
|
|
|
} elseif ($overwrite || !array_key_exists($segment, $target)) { |
63
|
|
|
$target[$segment] = $value; |
64
|
|
|
} |
65
|
138 |
|
} elseif (\is_object($target)) { |
66
|
138 |
|
if ($segments) { |
|
|
|
|
67
|
|
|
if (!isset($target->{$segment})) { |
68
|
|
|
$target->{$segment} = []; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
data_set($target->{$segment}, $segments, $value, $overwrite); |
72
|
138 |
|
} elseif ($overwrite || !isset($target->{$segment})) { |
73
|
138 |
|
$target->{$segment} = $value; |
74
|
|
|
} |
75
|
|
|
} else { |
76
|
|
|
$target = []; |
77
|
|
|
|
78
|
|
|
if ($segments) { |
|
|
|
|
79
|
|
|
data_set($target[$segment], $segments, $value, $overwrite); |
80
|
|
|
} elseif ($overwrite) { |
81
|
|
|
$target[$segment] = $value; |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|
85
|
138 |
|
return $target; |
86
|
|
|
} |
87
|
|
|
|
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)
or! empty(...)
instead.