Completed
Push — master ( d19868...7842ed )
by Elf
09:36
created

helpers.php ➔ get_id()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
eloc 6
nc 3
nop 2
dl 0
loc 10
ccs 0
cts 0
cp 0
crap 12
rs 9.4285
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
        if (is_object($var)) {
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_url')) {
181
    /**
182
     * Generate an asset URL.
183
     *
184
     * @param  string $path
185
     * @return string
186
     */
187
    function asset_url($path, $identifier = 'asset')
188
    {
189
        if (filter_var($path, FILTER_VALIDATE_URL) !== false) {
190
            return $path;
191
        }
192
193
        return config('support.url.'.$identifier).'/'.revision(trim($path, '/'));
194
    }
195
}
196
197
if (! function_exists('cdn_url')) {
198
    /**
199
     * Generate an asset CDN URL.
200
     *
201
     * @param  string  $path
202
     * @return string
203
     */
204
    function cdn_url($path)
205
    {
206
        return asset_url($path, 'cdn');
207
    }
208
}
209
210
if (! function_exists('api')) {
211
    /**
212
     * Create a new API response.
213
     *
214
     * @return \App\Http\ApiResponse
215
     */
216
    function api(...$args)
217
    {
218
        return new ApiResponse(...$args);
219
    }
220
}
221
222
if (! function_exists('optimus_encode')) {
223
    /**
224
     * Encode a number with Optimus.
225
     *
226
     * @param  int  $number
227
     * @return int
228
     */
229
    function optimus_encode($number)
230
    {
231
        return app('optimus')->encode($number);
232
    }
233
}
234
235
if (! function_exists('optimus_decode')) {
236
    /**
237
     * Decode a number with Optimus.
238
     *
239
     * @param  int  $number
240
     * @return int
241
     */
242
    function optimus_decode($number)
243
    {
244
        return app('optimus')->decode($number);
245
    }
246
}
247
248
if (! function_exists('random_uuid')) {
249
    /**
250
     * Generate a version 4 (random) UUID.
251
     *
252
     * @param  bool  $hex
253
     * @return string
254
     */
255
    function random_uuid($hex = false)
256
    {
257
        $uuid = Uuid::uuid4();
258
259
        return $hex ? $uuid->getHex() : $uuid->toString();
260
    }
261
}
262