-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers.php
186 lines (166 loc) · 5.23 KB
/
helpers.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
<?php
/**
* Lenevor Framework
*
* LICENSE
*
* This source file is subject to the new BSD license that is bundled
* with this package in the file license.md.
* It is also available through the world-wide-web at this URL:
* https://lenevor.com/license
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@Lenevor.com so we can send you a copy immediately.
*
* @package Lenevor
* @subpackage Base
* @link https://lenevor.com
* @copyright Copyright (c) 2019 - 2024 Alexander Campo <jalexcam@gmail.com>
* @license https://opensource.org/licenses/BSD-3-Clause New BSD license or see https://lenevor.com/license or see /license.md
*/
use Syscodes\Components\Support\Arr;
use Syscodes\Components\Support\Collection;
if ( ! function_exists('collect')) {
/**
* Create a collection from the given value.
*
* @param mixed $value
*
* @return \Syscodes\Components\Support\Collection
*/
function collect($value = null)
{
return new Collection($value);
}
}
if ( ! function_exists('data_get')) {
/**
* Get an item from an array or object using "dot" notation.
*
* @param mixed $target
* @param string|array|int|null $key
* @param mixed $default
*
* @return mixed
*/
function data_get($target, $key, mixed $default = null)
{
if (is_null($key)) return $target;
$key = is_array($key) ? $key : explode('.', $key);
while (($segment = array_shift($key)) !== null) {
if ($segment === '*') {
if ($target instanceof Collection) {
$target = $target->all();
} elseif (! is_array($target)) {
return value($default);
}
$result = Arr::pluck($target, $key);
return in_array('*', $key) ? Arr::collapse($result) : $result;
}
if (Arr::accessible($target) && Arr::exists($target, $segment)) {
$target = $target[$segment];
} elseif (is_object($target) && isset($target->{$segment})) {
$target = $target->{$segment};
} else {
return value($default);
}
}
return $target;
}
}
if( ! function_exists('data_set')) {
/**
* Set an item on an array or object using dot notation.
*
* @param mixed $target
* @param string|array $key
* @param mixed $value
* @param bool $overwrite
*
* @return mixed
*/
function data_set(&$target, string|array $key, mixed $value, bool $overwrite = true)
{
$segments = is_array($key) ? $key : explode('.', $key);
if (($segment = array_shift($segments)) === '*') {
if ( ! Arr::accessible($target)) {
$target = [];
}
if ($segments) {
foreach ($target as &$inner) {
data_set($inner, $segments, $value, $overwrite);
}
} elseif ($overwrite) {
foreach ($target as &$inner) {
$inner = $value;
}
}
} elseif (Arr::accessible($target)) {
if ($segments) {
if ( ! Arr::exists($target, $segment)) {
$target[$segment] = [];
}
data_set($target[$segment], $segments, $value, $overwrite);
} elseif ($overwrite || ! Arr::exists($target, $segment)) {
$target[$segment] = $value;
}
} elseif (is_object($target)) {
if ($segments) {
if ( ! isset($target->{$segment})) {
$target->{$segment} = [];
}
data_set($target->{$segment}, $segments, $value, $overwrite);
} elseif ($overwrite || ! isset($target->{$segment})) {
$target->{$segment} = $value;
}
} else {
$target = [];
if ($segments) {
data_set($target[$segment], $segments, $value, $overwrite);
} elseif ($overwrite) {
$target[$segment] = $value;
}
}
return $target;
}
}
if ( ! function_exists('headItem')) {
/**
* Get the actual element of an array. Useful for method chaining.
*
* @param array $array
* @param bool $bool
*
* @return mixed
*/
function headItem(array $array, bool $bool = false)
{
return $bool ? reset($array) : current($array);
}
}
if ( ! function_exists('lastItem')) {
/**
* Get the last element from an array.
*
* @param array $array
*
* @return mixed
*/
function lastItem(array $array)
{
return end($array);
}
}
if ( ! function_exists('value')) {
/**
* Return the default value of the given value.
*
* @param mixed $value
*
* @return mixed
*/
function value(mixed $value)
{
return $value instanceof \Closure ? $value() : $value;
}
}