Completed
Push — master ( d6d8a1...fab734 )
by Elf
04:41
created

helpers.php ➔ get_id()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 6

Duplication

Lines 5
Ratio 50 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
eloc 6
nc 3
nop 2
dl 5
loc 10
ccs 0
cts 5
cp 0
crap 12
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
use Ramsey\Uuid\Uuid;
4
5
if (! function_exists('is_domain')) {
6
    /**
7
     * Determines the current domain equals to the given domain identifier.
8
     *
9
     * @param  string  $identifier
10
     * @return bool
11
     */
12
    function is_domain($identifier)
13
    {
14
        return app('request')->getHost() === config('app.domains.'.$identifier);
15
    }
16
}
17
18
if (! function_exists('app_url')) {
19
    /**
20
     * Generate an URL for the application.
21
     *
22
     * @param  string  $path
23
     * @param  mixed  $parameters
24
     * @param  string  $identifier
25
     * @return string
26
     */
27
    function app_url($path = '', $parameters = null, $identifier = 'site')
28
    {
29
        $path = trim($path, '/');
30
        if (! empty($path) && ! starts_with($path, ['?', '&', '#'])) {
31
            $path = '/'.$path;
32
        }
33
34
        if (! is_null($parameters)) {
35
            $query = http_build_query($parameters);
36
            if (! empty($query)) {
37
                $path .= (str_contains($path, ['?', '&', '#']) ? '&' : '?').$query;
38
            }
39
        }
40
41
        if ($identifier && ($root = config('support.url.'.$identifier))) {
42
            return $root.$path;
43
        }
44
45
        return url($path);
46
    }
47
}
48
49
if (! function_exists('revision')) {
50
    /**
51
     * Get the revisioned asset path.
52
     *
53
     * @param  string  $path
54
     * @return string
55
     */
56
    function revision($path)
57
    {
58
        if ($rev = array_get(config('assets-version'), trim($path, DIRECTORY_SEPARATOR))) {
59
            return $path.'?'.$rev;
60
        }
61
62
        return $path;
63
    }
64
}
65
66
if (! function_exists('asset_from')) {
67
    /**
68
     * Generate the URL to an asset from a custom root domain such as CDN, etc.
69
     *
70
     * @param  string  $root
71
     * @param  string  $path
72
     * @param  bool|null  $secure
73
     * @return string
74
     */
75
    function asset_from($root, $path = '', $secure = null)
76
    {
77
        return app('url')->assetFrom($root, $path, $secure);
78
    }
79
}
80
81
if (! function_exists('asset_url')) {
82
    /**
83
     * Generate an asset URL.
84
     *
85
     * @param  string $path
86
     * @return string
87
     */
88
    function asset_url($path, $identifier = 'asset')
89
    {
90
        if (filter_var($path, FILTER_VALIDATE_URL) !== false) {
91
            return $path;
92
        }
93
94
        return config('support.url.'.$identifier).'/'.revision(trim($path, '/'));
95
    }
96
}
97
98
if (! function_exists('cdn_url')) {
99
    /**
100
     * Generate an asset CDN URL.
101
     *
102
     * @param  string  $path
103
     * @return string
104
     */
105
    function cdn_url($path)
106
    {
107
        return asset_url($path, 'cdn');
108
    }
109
}
110
111
if (! function_exists('gravatar')) {
112
    /**
113
     * Generate a Gravatar url.
114
     *
115
     * @see http://cn.gravatar.com/site/implement/images/
116
     *
117
     * @param  string  $email
118
     * @param  int  $size
119
     * @param  string  $default
120
     * @param  string  $rating
121
     * @return string
122
     */
123
    function gravatar($email, $size = 100, $default = null, $rating = null)
124
    {
125
        if (is_null($default)) {
126
            $default = config('support.gravatar.default');
127
        }
128
        if (is_null($rating)) {
129
            $rating = config('support.gravatar.rating');
130
        }
131
132
        $query = http_build_query(array_filter(compact('size', 'default', 'rating')));
133
134
        return app('url')->assetFrom(
135
            config('support.gravatar.host', 'http://gravatar.com/avatar'),
136
            md5(strtolower(trim($email))).'?'.$query
137
        );
138
    }
139
}
140
141
if (! function_exists('optimus_encode')) {
142
    /**
143
     * Encode a number with Optimus.
144
     *
145
     * @param  int  $number
146
     * @return int
147
     */
148
    function optimus_encode($number)
149
    {
150
        return app('optimus')->encode($number);
151
    }
152
}
153
154
if (! function_exists('optimus_decode')) {
155
    /**
156
     * Decode a number with Optimus.
157
     *
158
     * @param  int  $number
159
     * @return int
160
     */
161
    function optimus_decode($number)
162
    {
163
        return app('optimus')->decode($number);
164
    }
165
}
166
167
if (! function_exists('random_uuid')) {
168
    /**
169
     * Generate a version 4 (random) UUID.
170
     *
171
     * @param  bool  $hex
172
     * @return string
173
     */
174
    function random_uuid($hex = false)
175
    {
176
        $uuid = Uuid::uuid4();
177
178
        return $hex ? $uuid->getHex() : $uuid->toString();
179
    }
180
}
181