/
helpers.php
109 lines (97 loc) · 2.06 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
<?php
/**
* Laravel Helpers Package
* Extracted by Anthony Rappa
* rappa819@gmail.com
*/
if (!function_exists('dd')) {
/**
* Dump the passed variables and end the script.
*
* @param mixed
* @return void
*/
function dd()
{
array_map(function ($x) {
var_dump($x);
}, func_get_args());
die(1);
}
}
if (!function_exists('e')) {
/**
* Escape HTML entities in a string.
*
* @param string $value
*
* @return string
*/
function e(string $value): string
{
return htmlentities($value, ENT_QUOTES, 'UTF-8', false);
}
}
if (!function_exists('object_get')) {
/**
* Get an item from an object using "dot" notation.
*
* @param object $object
* @param string $key
* @param mixed $default
*
* @return mixed
*/
function object_get(object $object, string $key, $default = null)
{
if (trim($key) == '') {
return $object;
}
foreach (explode('.', $key) as $segment) {
if (!is_object($object) || !isset($object->{$segment})) {
return value($default);
}
$object = $object->{$segment};
}
return $object;
}
}
if (!function_exists('tap')) {
/**
* Call the given Closure with the given value then return the value.
*
* @param mixed $value
* @param callable $callback
*
* @return mixed
*/
function tap($value, callable $callback)
{
$callback($value);
return $value;
}
}
if (!function_exists('value')) {
/**
* Return the default value of the given value.
*
* @param mixed $value
* @return mixed
*/
function value($value)
{
return $value instanceof Closure ? $value() : $value;
}
}
if (!function_exists('with')) {
/**
* Return the given object. Useful for chaining.
*
* @param mixed $object
* @return mixed
*/
function with($object)
{
return $object;
}
}