3 use WPML\Collect\Support\Arr;
4 use WPML\Collect\Support\Collection;
6 if (! function_exists('wpml_collect')) {
8 * Create a collection from the given value.
11 * @return \WPML\Collect\Support\Collection
13 function wpml_collect($value = null)
15 return new Collection($value);
19 if (! function_exists('value')) {
21 * Return the default value of the given value.
26 function value($value)
28 return $value instanceof Closure ? $value() : $value;
32 if (! function_exists('data_get')) {
34 * Get an item from an array or object using "dot" notation.
36 * @param mixed $target
37 * @param string|array $key
38 * @param mixed $default
41 function data_get($target, $key, $default = null)
47 $key = is_array($key) ? $key : explode('.', $key);
49 while (($segment = array_shift($key)) !== null) {
50 if ($segment === '*') {
51 if ($target instanceof Collection) {
52 $target = $target->all();
53 } elseif (! is_array($target)) {
54 return value($default);
57 $result = Arr::pluck($target, $key);
59 return in_array('*', $key) ? Arr::collapse($result) : $result;
62 if (Arr::accessible($target) && Arr::exists($target, $segment)) {
63 $target = $target[$segment];
64 } elseif (is_object($target) && isset($target->{$segment})) {
65 $target = $target->{$segment};
67 return value($default);
75 if (! function_exists('with')) {
77 * Return the given object. Useful for chaining.
79 * @param mixed $object
82 function with($object)