helpers.php ➔ locale()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 0
dl 0
loc 8
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Helpers to Validate some data with laravel validator.
5
 *
6
 * @param string|array $fields
7
 * @param string|array $rules
8
 *
9
 * @return bool
10
 */
11
function validate($fields, $rules): bool
12
{
13
    if (!is_array($fields)) {
14
        $fields = ['default' => $fields];
15
    }
16
17
    if (!is_array($rules)) {
18
        $rules = ['default' => $rules];
19
    }
20
21
    return Validator::make($fields, $rules)->passes();
22
}
23
24
/**
25
 * getLocale
26
 * @return string
27
 */
28
function locale(): string
29
{
30
    $locale = app()->getLocale();
31
    if(!$locale){
32
        return config('app.fallback_locale');
33
    }
34
    return $locale;
35
}
36
37
/**
38
 * Return true if current user is logged, otherwise return false.
39
 * @return bool
40
 */
41
function userIsLogged() : bool
42
{
43
    if (Auth::guest()) {
44
        return false;
45
    }
46
47
    return true;
48
}
49
50
if (!function_exists('current_user')) {
51
    /**
52
     * Fetch currently logged in useruser()
53
     * if User not logged in return false.
54
     * Otherwise return
55
     * a) User object of currently logged in user if $field is null or empty
56
     * b) return single field user()->{$field} if $field is not empty and not null
57
     *
58
     * Returns false if not logged in
59
     * @param string $field
60
     * @return mixed
61
     */
62
    function current_user(string $field = '')
63
    {
64
        if (!Auth::check()) {
65
            return false;
66
        }
67
68
        if ($field == 'id') {
69
            return Auth::id();
70
        }
71
72
        $user = Auth::user();
73
74
        if ($field === null || $field == '') {
75
            return $user;
76
        }
77
78
        return $user->{$field};
79
    }
80
}
81
82
/**
83
 * Fetch log of database queries and return executed queries
84
 *
85
 * Usage:
86
 *
87
 * You need enable query log by calling:
88
 * \DB::enableQueryLog();
89
 *
90
 * If you have more than one DB connection you must specify it and Enables query log for my_connection
91
 * \DB::connection('my_connection')->enableQueryLog();
92
 *
93
 * query the db
94
 * \App\Articoli::query()->where('id','=',343242342)->get();
95
 *
96
 * then you can call queries() or in case of more than oine db call queries($last, 'my_connection')
97
 * dd(queries($last));
98
 *
99
 * the output is an array:
100
 * [
101
 *   "query" => "select * from `negozi` where `id` = ?",
102
 *   "bindings" => [343242342,],
103
 *   "time" => 1.77,
104
 *   "look" => "select * from `negozi` where `id` = 343242342",
105
 * ]
106
 *
107
 * If you want to print only interpolated query
108
 * echo queries(true)['look'] //output: "select * from `negozi` where `id` = 343242342"
109
 *
110
 * For performance and memory reasons, after get queries info, you can disable query log by excecute
111
 * \DB::disableQueryLog();
112
 * or in case of more db connections:
113
 * \DB::connection('my_connection')->disableQueryLog();
114
 *
115
 * @param bool $last [false] - if true, only return last query
116
 * @param string $dbConnectionName if empty use default connection, otherwise if you are multiple DB connections you may specify it.
117
 * @return array of queries
118
 */
119
function queries($last = false, $dbConnectionName = '')
120
{
121
    if($dbConnectionName!=''){
122
        $queries = \DB::connection($dbConnectionName)->getQueryLog();
123
    }else{
124
        $queries = \DB::getQueryLog();
125
    }
126
127
    foreach ($queries as &$query) {
128
        $query['look'] = query_interpolate($query['query'], $query['bindings']);
129
    }
130
131
    if ($last) {
132
        return end($queries);
133
    }
134
    return $queries;
135
}
136
137
/**
138
 * Echo log of database queries
139
 *
140
 * @return string
141
 */
142
function query_table() : string
143
{
144
    $queries = queries();
145
    $html = '<table style="background-color: #FFFF00;border: 1px solid #000000;color: #000000;padding-left: 10px;padding-right: 10px;width: 100%;">';
146
    foreach ($queries as $query) {
147
        $html .= '<tr style="border-top: 1px dashed #000000;"><td style="padding:8px;">' . e($query['look']) . '</td><td style="padding:8px;">' . e($query['time']) . '</td></tr>';
148
    }
149
150
    return $html . '</table>';
151
}
152
153
/**
154
 * Replaces any parameter placeholders in a query with the value of that
155
 * parameter. Useful for debugging. Assumes anonymous parameters from
156
 * $params are are in the same order as specified in $query
157
 * @author glendemon
158
 *
159
 * @param string $query The sql query with parameter placeholders
160
 * @param array $params The array of substitution parameters
161
 * @return string The interpolated query
162
 */
163
function query_interpolate($query, $params)
164
{
165
    $keys = array();
166
    $values = $params;
167
    foreach ($params as $key => $value) {
168
        if (is_string($key)) {
169
            $keys[] = '/:' . $key . '/';
170
        } else {
171
            $keys[] = '/[?]/';
172
        }
173
        if (is_array($value)) {
174
            $values[$key] = implode(',', $value);
175
        }
176
        if (is_null($value)) {
177
            $values[$key] = 'NULL';
178
        }
179
    }
180
    // Walk the array to see if we can add single-quotes to strings
181
    array_walk($values, function(&$v) { if (!is_numeric($v) && $v!="NULL") $v = "'".$v."'";});
182
    $query = preg_replace($keys, $values, $query, 1, $count);
183
    return $query;
184
}
185