Completed
Push — master ( 279d4e...5d09e0 )
by Elf
02:49
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 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('get_id')) {
6
    /**
7
     * Get id from a mixed variable.
8
     *
9
     * @param  mixed  $var
10
     * @param  string  $key
11
     * @return mixed
12
     */
13
    function get_id($var, $key = 'id')
14
    {
15
        if (is_object($var)) {
16
            return $var->{$key};
17
        } elseif (is_array($var)) {
18
            return $var[$key];
19
        }
20
21
        return $var;
22
    }
23
}
24
25
if (! function_exists('is_domain')) {
26
    /**
27
     * Determines the current domain equals to the given domain identifier.
28
     *
29
     * @param  string  $identifier
30
     * @return bool
31
     */
32
    function is_domain($identifier)
33
    {
34
        return app('request')->getHost() === config('app.domains.'.$identifier);
35
    }
36
}
37
38
if (! function_exists('app_url')) {
39
    /**
40
     * Generate an URL for the application.
41
     *
42
     * @param  string  $path
43
     * @param  mixed  $parameters
44
     * @param  string  $identifier
45
     * @return string
46
     */
47
    function app_url($path = '', $parameters = null, $identifier = 'site')
48
    {
49
        $path = trim($path, '/');
50
        if (! empty($path) && ! starts_with($path, ['?', '&', '#'])) {
51
            $path = '/'.$path;
52
        }
53
54
        if (! is_null($parameters)) {
55
            $query = http_build_query($parameters);
56
            if (! empty($query)) {
57
                $path .= (str_contains($path, ['?', '&', '#']) ? '&' : '?').$query;
58
            }
59
        }
60
61
        if ($identifier && ($root = config('support.url.'.$identifier))) {
62
            return $root.$path;
63
        }
64
65
        return url($path);
66
    }
67
}
68
69
if (! function_exists('revision')) {
70
    /**
71
     * Get the revisioned asset path.
72
     *
73
     * @param  string  $path
74
     * @return string
75
     */
76
    function revision($path)
77
    {
78
        if ($rev = array_get(config('assets'), trim($path, '/'))) {
79
            return $path.'?'.$rev;
80
        }
81
82
        return $path;
83
    }
84
}
85
86
if (! function_exists('asset_from')) {
87
    /**
88
     * Generate the URL to an asset from a custom root domain such as CDN, etc.
89
     *
90
     * @param  string  $root
91
     * @param  string  $path
92
     * @param  bool|null  $secure
93
     * @return string
94
     */
95
    function asset_from($root, $path = '', $secure = null)
96
    {
97
        return app('url')->assetFrom($root, $path, $secure);
98
    }
99
}
100
101
if (! function_exists('asset_url')) {
102
    /**
103
     * Generate an asset URL.
104
     *
105
     * @param  string $path
106
     * @return string
107
     */
108
    function asset_url($path, $identifier = 'asset')
109
    {
110
        if (filter_var($path, FILTER_VALIDATE_URL) !== false) {
111
            return $path;
112
        }
113
114
        return config('support.url.'.$identifier).'/'.revision(trim($path, '/'));
115
    }
116
}
117
118
if (! function_exists('cdn_url')) {
119
    /**
120
     * Generate an asset CDN URL.
121
     *
122
     * @param  string  $path
123
     * @return string
124
     */
125
    function cdn_url($path)
126
    {
127
        return asset_url($path, 'cdn');
128
    }
129
}
130
131
if (! function_exists('optimus_encode')) {
132
    /**
133
     * Encode a number with Optimus.
134
     *
135
     * @param  int  $number
136
     * @return int
137
     */
138
    function optimus_encode($number)
139
    {
140
        return app('optimus')->encode($number);
141
    }
142
}
143
144
if (! function_exists('optimus_decode')) {
145
    /**
146
     * Decode a number with Optimus.
147
     *
148
     * @param  int  $number
149
     * @return int
150
     */
151
    function optimus_decode($number)
152
    {
153
        return app('optimus')->decode($number);
154
    }
155
}
156
157
if (! function_exists('random_uuid')) {
158
    /**
159
     * Generate a version 4 (random) UUID.
160
     *
161
     * @param  bool  $hex
162
     * @return string
163
     */
164
    function random_uuid($hex = false)
165
    {
166
        $uuid = Uuid::uuid4();
167
168
        return $hex ? $uuid->getHex() : $uuid->toString();
169
    }
170
}
171