Completed
Push — master ( 769083...aa69d3 )
by Elf
04:59
created

helpers.php ➔ optimus_encode()   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 1
dl 0
loc 4
ccs 0
cts 0
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
use Illuminate\Support\Str;
4
5
if (! function_exists('mb_trim')) {
6
    /**
7
     * Strip whitespace (or other characters) from the beginning and end of a string.
8
     *
9
     * @see https://github.com/vanderlee/PHP-multibyte-functions/blob/master/functions/mb_trim.php
10
     *
11
     * @param  string  $string
12
     * @return string
13
     */
14
    function mb_trim($string)
15
    {
16
        return mb_ereg_replace('^\s*([\s\S]*?)\s*$', '\1', $string);
17
    }
18
}
19
20
if (! function_exists('urlsafe_base64_encode')) {
21
    /**
22
     * Encodes the given data with base64, and returns an URL-safe string.
23
     *
24
     * @param  string  $data
25
     * @return string
26
     */
27
    function urlsafe_base64_encode($data)
28
    {
29
        return strtr(base64_encode($data), ['+' => '-', '/' => '_', '=' => '']);
30
    }
31
}
32
33
if (! function_exists('urlsafe_base64_decode')) {
34
    /**
35
     * Decodes a base64 encoded data.
36
     *
37
     * @param  string  $data
38
     * @param  bool  $strict
39
     * @return string
40
     */
41
    function urlsafe_base64_decode($data, $strict = false)
42
    {
43
        return base64_decode(strtr($data.str_repeat('=', (4 - strlen($data) % 4)), '-_', '+/'), $strict);
44
    }
45
}
46
47
if (! function_exists('string_value')) {
48
    /**
49
     * Converts any type to a string.
50
     *
51
     * @param  mixed  $value
52
     * @return string
53
     */
54
    function string_value($value, $jsonOptions = JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES)
55
    {
56
        if (is_object($value)) {
57
            if (method_exists($value, '__toString')) {
58
                return (string) $value;
59
            }
60
61
            if (method_exists($value, 'toArray')) {
62
                $value = $value->toArray();
63
            }
64
        }
65
66
        return is_string($value) ? $value : json_encode($value, $jsonOptions);
67
    }
68
}
69
70
if (! function_exists('active_if')) {
71
    /**
72
     * Returns string 'active' if the current request URI matches the given patterns.
73
     *
74
     * @return string
75
     */
76
    function active_if()
77
    {
78
        return call_user_func_array([app('request'), 'is'], func_get_args()) ? 'active' : '';
79
    }
80
}
81
82
if (! function_exists('str_limit2')) {
83
    /**
84
     * Limit the number of characters in a string.
85
     *
86
     * @param  string  $value
87
     * @param  int     $limit
88
     * @param  string  $end
89
     * @return string
90
     */
91
    function str_limit2($value, $limit = 100, $end = '')
92
    {
93
        return Str::limit($value, $limit, $end);
94
    }
95
}
96
97
if (! function_exists('is_domain')) {
98
    /**
99
     * Determines the current domain equals to the given domain identifier.
100
     *
101
     * @param  string  $identifier
102
     * @return bool
103
     */
104
    function is_domain($identifier)
105
    {
106 1
        return app('request')->getHost() === config('app.domains.'.$identifier);
107
    }
108
}
109
110
if (! function_exists('app_url')) {
111
    /**
112
     * Generate an URL for the application.
113
     *
114
     * @param  string  $path
115
     * @param  mixed  $parameters
116
     * @param  string  $identifier
117
     * @return string
118
     */
119
    function app_url($path = '', $parameters = null, $identifier = 'site')
120
    {
121
        $path = trim($path, '/');
122
        if (! empty($path) && ! starts_with($path, ['?', '&', '#'])) {
123
            $path = '/'.$path;
124
        }
125
126
        if (! is_null($parameters)) {
127
            $query = http_build_query($parameters);
128
            if (! empty($query)) {
129
                $path .= (str_contains($path, ['?', '&', '#']) ? '&' : '?').$query;
130
            }
131
        }
132
133
        if ($identifier && ($root = config('support.url.'.$identifier))) {
134
            return $root.$path;
135
        }
136
137
        return url($path);
138
    }
139
}
140
141
if (! function_exists('revision')) {
142
    /**
143
     * Get the revisioned asset path.
144
     *
145
     * @param  string  $path
146
     * @return string
147
     */
148
    function revision($path)
149
    {
150
        if ($rev = array_get(config('assets'), trim($path, '/'))) {
151
            return $path.'?'.$rev;
152
        }
153
154
        return $path;
155
    }
156
}
157
158
if (! function_exists('asset_url')) {
159
    /**
160
     * Generate an asset URL.
161
     *
162
     * @param  string $path
163
     * @return string
164
     */
165
    function asset_url($path, $identifier = 'asset')
166
    {
167
        if (filter_var($path, FILTER_VALIDATE_URL) !== false) {
168
            return $path;
169
        }
170
171
        return config('support.url.'.$identifier).'/'.revision(trim($path, '/'));
172
    }
173
}
174
175
if (! function_exists('cdn_url')) {
176
    /**
177
     * Generate an asset CDN URL.
178
     *
179
     * @param  string  $path
180
     * @return string
181
     */
182
    function cdn_url($path)
183
    {
184
        return asset_url($path, 'cdn');
185
    }
186
}
187
188
if (! function_exists('api')) {
189
    /**
190
     * Create a new API response.
191
     *
192
     * @return \App\Http\ApiResponse
193
     */
194
    function api(...$args)
195
    {
196
        return response()->api(...$args);
0 ignored issues
show
Bug introduced by
The method api() does not seem to exist on object<Illuminate\Contra...outing\ResponseFactory>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
197
    }
198
}
199
200
if (! function_exists('optimus_encode')) {
201
    /**
202
     * Encode a number with Optimus.
203
     *
204
     * @param  int  $number
205
     * @return int
206
     */
207
    function optimus_encode($number)
208
    {
209
        return app('optimus')->encode($number);
210
    }
211
}
212
213
if (! function_exists('optimus_decode')) {
214
    /**
215
     * Decode a number with Optimus.
216
     *
217
     * @param  int  $number
218
     * @return int
219
     */
220
    function optimus_decode($number)
221
    {
222
        return app('optimus')->decode($number);
223
    }
224
}
225