1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
use Nip\Utility\Arr; |
4
|
|
|
|
5
|
|
|
if (!function_exists('env')) { |
6
|
|
|
/** |
7
|
|
|
* Gets the value of an environment variable. |
8
|
|
|
* |
9
|
|
|
* @param string $key |
10
|
|
|
* @param mixed $default |
11
|
|
|
* @return mixed |
12
|
|
|
*/ |
13
|
|
|
function env($key, $default = null) |
14
|
|
|
{ |
15
|
|
|
$value = getenv($key); |
16
|
|
|
if ($value === false) { |
17
|
|
|
return value($default); |
18
|
|
|
} |
19
|
|
|
switch (strtolower($value)) { |
20
|
|
|
case 'true': |
21
|
|
|
case '(true)': |
22
|
|
|
return true; |
23
|
|
|
case 'false': |
24
|
|
|
case '(false)': |
25
|
|
|
return false; |
26
|
|
|
case 'empty': |
27
|
|
|
case '(empty)': |
28
|
|
|
return ''; |
29
|
|
|
case 'null': |
30
|
|
|
case '(null)': |
31
|
|
|
return; |
32
|
|
|
} |
33
|
|
|
// if (strlen($value) > 1 && Str::startsWith($value, '"') && Str::endsWith($value, '"')) { |
34
|
|
|
// return substr($value, 1, -1); |
35
|
|
|
// } |
36
|
|
|
return $value; |
37
|
|
|
} |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
if (! function_exists('data_get')) { |
41
|
|
|
/** |
42
|
|
|
* Get an item from an array or object using "dot" notation. |
43
|
|
|
* |
44
|
|
|
* @param mixed $target |
45
|
|
|
* @param string|array|int $key |
46
|
|
|
* @param mixed $default |
47
|
|
|
* @return mixed |
48
|
|
|
*/ |
49
|
1 |
|
function data_get($target, $key, $default = null) |
50
|
|
|
{ |
51
|
|
|
if (is_null($key)) { |
|
|
|
|
52
|
|
|
return $target; |
53
|
|
|
} |
54
|
|
|
$key = is_array($key) ? $key : explode('.', $key); |
55
|
|
|
|
56
|
|
|
while (! is_null($segment = array_shift($key))) { |
57
|
|
|
if ($segment === '*') { |
58
|
|
|
if (is_object($target) && method_exists($target, 'all')) { |
59
|
|
|
$target = $target->all(); |
60
|
|
|
} elseif (! is_array($target)) { |
61
|
|
|
return value($default); |
62
|
|
|
} |
63
|
|
|
$result = []; |
64
|
1 |
|
foreach ($target as $item) { |
65
|
|
|
$result[] = data_get($item, $key); |
66
|
|
|
} |
67
|
1 |
|
return in_array('*', $key) ? Arr::collapse($result) : $result; |
68
|
|
|
} |
69
|
1 |
|
if (Arr::accessible($target) && Arr::exists($target, $segment)) { |
70
|
1 |
|
$target = $target[$segment]; |
71
|
|
|
} elseif (is_object($target) && isset($target->{$segment})) { |
72
|
|
|
$target = $target->{$segment}; |
73
|
|
|
} else { |
74
|
|
|
return value($default); |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
return $target; |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
if (! function_exists('head')) { |
81
|
|
|
/** |
82
|
1 |
|
* Get the first element of an array. Useful for method chaining. |
83
|
1 |
|
* |
84
|
1 |
|
* @param array $array |
85
|
|
|
* @return mixed |
86
|
|
|
*/ |
87
|
1 |
|
function head($array) |
88
|
|
|
{ |
89
|
|
|
return reset($array); |
90
|
1 |
|
} |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
if (! function_exists('last')) { |
94
|
|
|
/** |
95
|
|
|
* Get the last element from an array. |
96
|
|
|
* |
97
|
|
|
* @param array $array |
98
|
|
|
* @return mixed |
99
|
|
|
*/ |
100
|
|
|
function last($array) |
101
|
|
|
{ |
102
|
|
|
return end($array); |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
if (!function_exists('value')) { |
107
|
|
|
/** |
108
|
|
|
* Return the default value of the given value. |
109
|
|
|
* |
110
|
|
|
* @param mixed $value |
111
|
|
|
* @return mixed |
112
|
|
|
*/ |
113
|
|
|
function value($value) |
114
|
|
|
{ |
115
|
|
|
return $value instanceof Closure ? $value() : $value; |
116
|
|
|
} |
117
|
|
|
} |