Passed
Push — dependabot/npm_and_yarn/expect... ( ca50a3 )
by
unknown
82:34 queued 72:34
created

is_array_empty()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 6
nc 3
nop 1
dl 0
loc 12
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Retrieve the logged in admin
5
 */
6
if (!function_exists('chiefAdmin')) {
7
    function chiefAdmin()
8
    {
9
        return \Illuminate\Support\Facades\Auth::guard('chief')->user();
10
    }
11
}
12
13
/**
14
 * Retrieve the public asset with a version stamp.
15
 * This allows for browsercache out of the box
16
 */
17
if (!function_exists('chief_cached_asset')) {
18
    function chief_cached_asset($filepath)
19
    {
20
        $manifestPath = '/chief-assets/back';
21
22
        // Manifest expects each entry to start with a leading slash - we make sure to deduplicate the manifest path.
23
        $entry = str_replace($manifestPath, '', '/' . ltrim($filepath, '/'));
24
25
        try {
26
            // Paths should be given relative to the manifestpath so make sure to remove the basepath
27
            return asset(mix($entry, $manifestPath));
28
        } catch (\Exception $e) {
29
            \Illuminate\Support\Facades\Log::error($e);
30
31
            return $manifestPath . $entry;
32
        }
33
    }
34
}
35
36
if (!function_exists('chiefSetting')) {
37
    function chiefSetting($key = null, $locale = null, $default = null)
38
    {
39
        $settings = app(\Thinktomorrow\Chief\Settings\Settings::class);
40
41
        if (is_null($key)) {
42
            return $settings;
43
        }
44
45
        return $settings->get($key, $locale, $default);
46
    }
47
}
48
49
if (!function_exists('chiefmenu')) {
50
    function chiefmenu($key = 'main')
51
    {
52
        $menu = \Thinktomorrow\Chief\Menu\Menu::find($key);
53
54
        return $menu ?? new \Thinktomorrow\Chief\Menu\NullMenu();
55
    }
56
}
57
58
if (!function_exists('str_slug_slashed')) {
59
    function str_slug_slashed($title, $separator = '-', $language = 'en')
60
    {
61
        $parts = explode('/', $title);
62
63
        foreach ($parts as $i => $part) {
64
            $parts[$i] = Illuminate\Support\Str::slug($part, $separator, $language);
65
        }
66
67
        return implode('/', $parts);
68
    }
69
}
70
71
if (!function_exists('is_array_empty')) {
72
    function is_array_empty(array $values)
73
    {
74
        $empty = true;
75
76
        foreach ($values as $value) {
77
            if (!$value || !trim($value)) {
78
                continue;
79
            }
80
            $empty = false;
81
        }
82
83
        return $empty;
84
    }
85
}
86
87
if (!function_exists('contract')) {
88
    function contract($instance, $contract)
89
    {
90
        return $instance instanceof $contract;
91
    }
92
}
93
94
/**
95
 * --------------------------------------------------------------------------
96
 * Helper: Teaser
97
 * --------------------------------------------------------------------------
98
 */
99
if (!function_exists('teaser')) {
100
    /**
101
     * @param $text
102
     * @param null $max
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $max is correct as it would always require null to be passed?
Loading history...
103
     * @param null $ending
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $ending is correct as it would always require null to be passed?
Loading history...
104
     * @param string $clean - whitelist of html tags: set to null to allow tags
105
     * @return mixed|string
106
     */
107
    function teaser($text, $max = null, $ending = null, $clean = '')
108
    {
109
        if (is_null($max) or is_string($max)) {
0 ignored issues
show
introduced by
The condition is_null($max) is always true.
Loading history...
110
            return $text;
111
        }
112
113
        if (!is_null($clean)) {
114
            $text = cleanupHTML($text, $clean);
115
        }
116
117
        $teaser = mb_substr($text, 0, $max, 'utf-8');
118
119
        return strlen($text) <= $max ? $teaser : $teaser . $ending;
120
    }
121
}
122
123
124
/**
125
 * --------------------------------------------------------------------------
126
 * Helper: cleanupString
127
 * --------------------------------------------------------------------------
128
 *
129
 * Takes an input and cleans up a regular string from unwanted input
130
 *
131
 * @param string $value
132
 * @return    string
133
 */
134
if (!function_exists('cleanupString')) {
135
    function cleanupString($value)
136
    {
137
        $value = strip_tags($value);
138
139
        return trim($value);
140
    }
141
}
142
143
/**
144
 * --------------------------------------------------------------------------
145
 * Helper: cleanupHTML
146
 * --------------------------------------------------------------------------
147
 *
148
 * Takes an input and cleans up unwanted / malicious HTML
149
 *
150
 * @param string $value
151
 * @param string $whitelist - if false no tagstripping will occur - other than HTMLPurifier
152
 * @return    string
153
 */
154
if (!function_exists('cleanupHTML')) {
155
    function cleanupHTML($value, $whitelist = null)
156
    {
157
        if (is_null($whitelist)) {
158
            $whitelist = '<code><span><div><label><a><br><p><b><i><del><strike><u><img><video><audio><iframe><object><embed><param><blockquote><mark><cite><small><ul><ol><li><hr><dl><dt><dd><sup><sub><big><pre><code><figure><figcaption><strong><em><table><tr><td><th><tbody><thead><tfoot><h1><h2><h3><h4><h5><h6>';
159
        }
160
        // Strip entire blocks of malicious code
161
        $value = preg_replace(array(
162
            '@<script[^>]*?>.*?</script>@si',
163
            '@onclick=[^ ].*? @si',
164
        ), '', $value);
165
        // strip unwanted tags via whitelist...
166
        if (false !== $whitelist) {
167
            $value = strip_tags($value, $whitelist);
168
        }
169
        // // cleanup HTML and any unwanted attributes
170
        $purifier = new HTMLPurifier();
171
        $value = $purifier->purify($value);
172
173
        /**
174
         * htmlPurifier converts characters to their encode equivalents. This is something
175
         * that we need to reverse after the htmlPurifier cleanup.
176
         */
177
        $value = str_replace('&amp;', '&', $value);
178
179
        return $value;
180
    }
181
}
182
183
/**
184
 * Determine whether current url is the active one
185
 *
186
 * @param string $name routename or path without HOST
187
 * @param array $parameters
188
 * @return bool
189
 */
190
if (!function_exists('isActiveUrl')) {
191
    function isActiveUrl($name, $parameters = [])
192
    {
193
        if (\Illuminate\Support\Facades\Route::currentRouteNamed($name)) {
194
            $flag = true;
195
            $current = \Illuminate\Support\Facades\Route::current();
196
197
            /**
198
             * If a single parameter is passed as string, we will convert this to
199
             * the proper array keyed by the first uri parameter
200
             */
201
            if (!is_array($parameters)) {
202
                $names = $current->parameterNames();
203
                $parameters = [reset($names) => $parameters];
204
            }
205
206
            foreach ($parameters as $key => $parameter) {
207
                if ($current->parameter($key, false) != $parameter) {
208
                    $flag = false;
209
                }
210
            }
211
212
            return $flag;
213
        }
214
215
        $name = ltrim($name, '/');
216
217
        if (false !== strpos($name, '*')) {
218
            $pattern = str_replace('\*', '(.*)', preg_quote($name, '#'));
219
220
            return !!preg_match("#$pattern#", request()->path());
221
        }
222
223
        return ($name == request()->path() || $name == request()->fullUrl());
224
    }
225
}
226
227
/**
228
 * Inject a query parameter into an url
229
 * If the query key already exists, it will be overwritten with the new value
230
 *
231
 * @param $url
232
 * @param array $query_params
233
 * @param array $overrides
234
 * @return string
235
 */
236
if (!function_exists('addQueryToUrl')) {
237
    function addQueryToUrl($url, array $query_params = [], $overrides = [])
238
    {
239
        $parsed_url = parse_url($url);
240
241
        $parsed_url = array_merge(array_fill_keys([
242
            'scheme',
243
            'host',
244
            'port',
245
            'path',
246
            'query',
247
            'fragment',
248
        ], null), $parsed_url, $overrides);
249
250
        $scheme = $parsed_url['scheme'] ? $parsed_url['scheme'] . '://' : null;
251
        $port = $parsed_url['port'] ? ':' . $parsed_url['port'] : null;
252
        $fragment = $parsed_url['fragment'] ? '#' . $parsed_url['fragment'] : null;
253
254
        $baseurl = $scheme . $parsed_url['host'] . $port . $parsed_url['path'];
255
        $current_query = [];
256
257
        $_query = explode('&', $parsed_url['query']);
258
259
        array_map(function ($v) use (&$current_query) {
260
            if (!$v) {
261
                return;
262
            }
263
            $split = explode('=', $v);
264
            if (count($split) == 2) {
265
                $current_query[$split[0]] = $split[1];
266
            }
267
        }, $_query);
268
269
        foreach ($query_params as $key => $value) {
270
            if (isset($current_query[$key])) {
271
                unset($current_query[$key]);
272
            }
273
        }
274
275
        $query = urldecode(http_build_query(array_merge($current_query, $query_params)));
276
277
        return $baseurl . '?' . $query . $fragment;
278
    }
279
}
280
281
if (!function_exists('chiefMemoize')) {
282
    /**
283
     * Memoize a function
284
     *
285
     * @param $key
286
     * @param Closure $closure
287
     * @param array $parameters
288
     * @return mixed
289
     */
290
    function chiefMemoize($key, \Closure $closure, array $parameters = [])
291
    {
292
        return (new \Thinktomorrow\Chief\Common\Helpers\Memoize($key))->run($closure, $parameters);
293
    }
294
}
295