Completed
Push — master ( cdad02...2b0328 )
by Elf
07:32
created

helpers.php ➔ asset_from()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 3
dl 0
loc 4
ccs 0
cts 1
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
use App\Support\Http\ApiResponse;
4
use Illuminate\Support\Str;
5
use Ramsey\Uuid\Uuid;
6
7
if (! function_exists('mb_trim')) {
8
    /**
9
     * Strip whitespace (or other characters) from the beginning and end of a string.
10
     *
11
     * @see https://github.com/vanderlee/PHP-multibyte-functions/blob/master/functions/mb_trim.php
12
     *
13
     * @param  string  $string
14
     * @return string
15
     */
16
    function mb_trim($string)
17
    {
18
        return mb_ereg_replace('^\s*([\s\S]*?)\s*$', '\1', $string);
19
    }
20
}
21
22
if (! function_exists('urlsafe_base64_encode')) {
23
    /**
24
     * Encodes the given data with base64, and returns an URL-safe string.
25
     *
26
     * @param  string  $data
27
     * @return string
28
     */
29
    function urlsafe_base64_encode($data)
30
    {
31
        return strtr(base64_encode($data), ['+' => '-', '/' => '_', '=' => '']);
32
    }
33
}
34
35
if (! function_exists('urlsafe_base64_decode')) {
36
    /**
37
     * Decodes a base64 encoded data.
38
     *
39
     * @param  string  $data
40
     * @param  bool  $strict
41
     * @return string
42
     */
43
    function urlsafe_base64_decode($data, $strict = false)
44
    {
45
        return base64_decode(strtr($data.str_repeat('=', (4 - strlen($data) % 4)), '-_', '+/'), $strict);
46
    }
47
}
48
49
if (! function_exists('string_value')) {
50
    /**
51
     * Converts any type to a string.
52
     *
53
     * @param  mixed  $value
54
     * @return string
55
     */
56
    function string_value($value, $jsonOptions = JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES)
57
    {
58
        if (is_object($value)) {
59
            if (method_exists($value, '__toString')) {
60
                return (string) $value;
61
            }
62
63
            if (method_exists($value, 'toArray')) {
64
                $value = $value->toArray();
65
            }
66
        }
67
68
        return is_string($value) ? $value : json_encode($value, $jsonOptions);
69
    }
70
}
71
72
if (! function_exists('active_if')) {
73
    /**
74
     * Returns string 'active' if the current request URI matches the given patterns.
75
     *
76
     * @return string
77
     */
78
    function active_if()
79
    {
80
        return call_user_func_array([app('request'), 'is'], func_get_args()) ? 'active' : '';
81
    }
82
}
83
84
if (! function_exists('get_id')) {
85
    /**
86
     * Get id from a mixed variable.
87
     *
88
     * @param  mixed  $var
89
     * @param  string  $key
90
     * @return mixed
91
     */
92
    function get_id($var, $key = 'id')
93
    {
94 View Code Duplication
        if (is_object($var)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
95
            return $var->{$key};
96
        } elseif (is_array($var)) {
97
            return $var[$key];
98
        }
99
100
        return $var;
101
    }
102
}
103
104
if (! function_exists('str_limit2')) {
105
    /**
106
     * Limit the number of characters in a string.
107
     *
108
     * @param  string  $value
109
     * @param  int     $limit
110
     * @param  string  $end
111
     * @return string
112
     */
113
    function str_limit2($value, $limit = 100, $end = '')
114
    {
115
        return Str::limit($value, $limit, $end);
116
    }
117
}
118
119
if (! function_exists('is_domain')) {
120
    /**
121
     * Determines the current domain equals to the given domain identifier.
122
     *
123
     * @param  string  $identifier
124
     * @return bool
125
     */
126
    function is_domain($identifier)
127
    {
128 1
        return app('request')->getHost() === config('app.domains.'.$identifier);
129
    }
130
}
131
132
if (! function_exists('app_url')) {
133
    /**
134
     * Generate an URL for the application.
135
     *
136
     * @param  string  $path
137
     * @param  mixed  $parameters
138
     * @param  string  $identifier
139
     * @return string
140
     */
141
    function app_url($path = '', $parameters = null, $identifier = 'site')
142
    {
143
        $path = trim($path, '/');
144
        if (! empty($path) && ! starts_with($path, ['?', '&', '#'])) {
145
            $path = '/'.$path;
146
        }
147
148
        if (! is_null($parameters)) {
149
            $query = http_build_query($parameters);
150
            if (! empty($query)) {
151
                $path .= (str_contains($path, ['?', '&', '#']) ? '&' : '?').$query;
152
            }
153
        }
154
155
        if ($identifier && ($root = config('support.url.'.$identifier))) {
156
            return $root.$path;
157
        }
158
159
        return url($path);
160
    }
161
}
162
163
if (! function_exists('revision')) {
164
    /**
165
     * Get the revisioned asset path.
166
     *
167
     * @param  string  $path
168
     * @return string
169
     */
170
    function revision($path)
171
    {
172
        if ($rev = array_get(config('assets'), trim($path, '/'))) {
173
            return $path.'?'.$rev;
174
        }
175
176
        return $path;
177
    }
178
}
179
180
if (! function_exists('asset_from')) {
181
    /**
182
     * Generate the URL to an asset from a custom root domain such as CDN, etc.
183
     *
184
     * @param  string  $root
185
     * @param  string  $path
186
     * @param  bool|null  $secure
187
     * @return string
188
     */
189
    function asset_from($root, $path, $secure = null)
190
    {
191
        return app('url')->assetFrom($root, $path, $secure);
192
    }
193
}
194
195
if (! function_exists('asset_url')) {
196
    /**
197
     * Generate an asset URL.
198
     *
199
     * @param  string $path
200
     * @return string
201
     */
202
    function asset_url($path, $identifier = 'asset')
203
    {
204
        if (filter_var($path, FILTER_VALIDATE_URL) !== false) {
205
            return $path;
206
        }
207
208
        return config('support.url.'.$identifier).'/'.revision(trim($path, '/'));
209
    }
210
}
211
212
if (! function_exists('cdn_url')) {
213
    /**
214
     * Generate an asset CDN URL.
215
     *
216
     * @param  string  $path
217
     * @return string
218
     */
219
    function cdn_url($path)
220
    {
221
        return asset_url($path, 'cdn');
222
    }
223
}
224
225
if (! function_exists('api')) {
226
    /**
227
     * Create a new API response.
228
     *
229
     * @return \App\Support\Http\ApiResponse
230
     */
231
    function api(...$args)
232
    {
233
        return new ApiResponse(...$args);
234
    }
235
}
236
237
if (! function_exists('optimus_encode')) {
238
    /**
239
     * Encode a number with Optimus.
240
     *
241
     * @param  int  $number
242
     * @return int
243
     */
244
    function optimus_encode($number)
245
    {
246
        return app('optimus')->encode($number);
247
    }
248
}
249
250
if (! function_exists('optimus_decode')) {
251
    /**
252
     * Decode a number with Optimus.
253
     *
254
     * @param  int  $number
255
     * @return int
256
     */
257
    function optimus_decode($number)
258
    {
259
        return app('optimus')->decode($number);
260
    }
261
}
262
263
if (! function_exists('random_uuid')) {
264
    /**
265
     * Generate a version 4 (random) UUID.
266
     *
267
     * @param  bool  $hex
268
     * @return string
269
     */
270
    function random_uuid($hex = false)
271
    {
272
        $uuid = Uuid::uuid4();
273
274
        return $hex ? $uuid->getHex() : $uuid->toString();
275
    }
276
}
277