Passed
Push — master ( a6375b...78beea )
by Anton
05:54
created

_functions.php ➔ array_has()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 2
dl 0
loc 4
ccs 1
cts 1
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Bluz Framework Component
4
 *
5
 * @copyright Bluz PHP Team
6
 * @link      https://github.com/bluzphp/framework
7
 */
8
9
declare(strict_types=1);
10
11
use Bluz\Common\Collection;
12
use Bluz\Common\Str;
13
use Bluz\Translator\Translator;
14
15
/**
16
 * Simple functions of framework
17
 * be careful with this way
18
 *
19
 * @author   Anton Shevchuk
20
 */
21
if (!function_exists('array_get')) {
22
    /**
23
     * Get value of array by keys
24
     *
25
     * @param       $array
26
     * @param array ...$keys
27
     *
28
     * @return mixed|null
29
     */
30
    function array_get(array $array, ...$keys)
31
    {
32 1
        return Collection::get($array, ...$keys);
33
    }
34
}
35
36
if (!function_exists('array_has')) {
37
    /**
38
     * @param       $array
39
     * @param array ...$keys
40
     *
41
     * @return bool
42
     */
43
    function array_has(array $array, ...$keys)
44
    {
45 1
        return Collection::has($array, ...$keys);
46
    }
47
}
48
49
if (!function_exists('array_add')) {
50
    /**
51
     * @param       $array
52
     * @param array ...$keys
53
     *
54
     * @return void
55
     */
56
    function array_add(array &$array, ...$keys)
57
    {
58 1
        Collection::add($array, ...$keys);
59 1
    }
60
}
61
62
if (!function_exists('array_set')) {
63
    /**
64
     * @param       $array
65
     * @param array ...$keys
66
     *
67
     * @return void
68
     */
69
    function array_set(array &$array, ...$keys)
70
    {
71 1
        Collection::set($array, ...$keys);
72 1
    }
73
}
74
75
if (!function_exists('debug')) {
76
    /**
77
     * Debug variables
78
     *
79
     * Example of usage
80
     *     debug(123);
81
     *     debug(new stdClass());
82
     *     debug($_GET, $_POST, $_FILES);
83
     *
84
     * @codeCoverageIgnore
85
     *
86
     * @param mixed ...$params
87
     */
88
    function debug(...$params)
89
    {
90
        // check definition
91
        if (!getenv('BLUZ_DEBUG') || !isset($_COOKIE['BLUZ_DEBUG'])) {
92
            return;
93
        }
94
95
        ini_set('xdebug.var_display_max_children', '512');
96
97
        if (PHP_SAPI === 'cli') {
98
            if (extension_loaded('xdebug')) {
99
                // try to enable CLI colors
100
                ini_set('xdebug.cli_color', '1');
101
                xdebug_print_function_stack();
102
            } else {
103
                debug_print_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
104
            }
105
            var_dump($params);
0 ignored issues
show
Security Debugging Code introduced by
var_dump($params) looks like debug code. Are you sure you do not want to remove it?
Loading history...
106
        } else {
107
            echo '<div class="textleft clear"><pre class="bluz-debug">';
108
            debug_print_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
109
            var_dump($params);
110
            echo '</pre></div>';
111
        }
112
    }
113
}
114
115
if (!function_exists('esc')) {
116
    /**
117
     * Escape variable for use in View
118
     *
119
     * Example of usage
120
     *     esc($_GET['name']);
121
     *     esc($_GET['name'], ENT_QUOTES);
122
     *
123
     * @param  string  $variable
124
     * @param  integer $flags
125
     *
126
     * @return string
127
     */
128
    function esc($variable, int $flags = ENT_HTML5)
129
    {
130 1
        return htmlentities((string)$variable, $flags, 'UTF-8');
131
    }
132
}
133
134
if (!function_exists('str_trim_end')) {
135
    /**
136
     * Added symbol to end of string, trim it before
137
     *
138
     * @param  string $subject
139
     * @param  string $symbols
140
     *
141
     * @return string
142
     */
143
    function str_trim_end($subject, $symbols)
144
    {
145 602
        return rtrim($subject, $symbols) . $symbols;
146
    }
147
}
148
149
if (!function_exists('to_camel_case')) {
150
    /**
151
     * Convert string to camel case
152
     *
153
     * @param  string $subject
154
     *
155
     * @return string
156
     */
157
    function to_camel_case($subject)
158
    {
159 4
        return Str::toCamelCase($subject);
160
    }
161
}
162
163
if (!function_exists('class_namespace')) {
164
    /**
165
     * Get namespace of class
166
     *
167
     * @param  string $class
168
     *
169
     * @return string
170
     */
171
    function class_namespace($class)
172
    {
173 6
        return substr($class, 0, strrpos($class, '\\'));
174
    }
175
}
176
177
if (!function_exists('value')) {
178
    /**
179
     * Return the value for callable
180
     *
181
     * @param  callable|mixed $value
182
     *
183
     * @return mixed
184
     */
185
    function value($value)
186
    {
187 6
        return is_callable($value) ? $value(): $value;
188
    }
189
}
190
191
// @codingStandardsIgnoreStart
192
if (!function_exists('__')) {
193
    /**
194
     * Translate message
195
     *
196
     * Example of usage
197
     *
198
     *     // simple
199
     *     // equal to gettext('Message')
200
     *     __('Message');
201
     *
202
     *     // simple replace of one or more argument(s)
203
     *     // equal to sprintf(gettext('Message to %s'), 'Username')
204
     *     __('Message to %s', 'Username');
205
     *
206
     * @param  string   $message
207
     * @param  string[] $text [optional]
208
     *
209
     * @return string
210
     */
211
    function __($message, ...$text)
212
    {
213 452
        return Translator::translate($message, ...$text);
214
    }
215
}
216
217
if (!function_exists('_n')) {
218
    /**
219
     * Translate plural form
220
     *
221
     * Example of usage
222
     *
223
     *     // plural form + sprintf
224
     *     // equal to sprintf(ngettext('%d comment', '%d comments', 4), 4)
225
     *     _n('%d comment', '%d comments', 4, 4)
226
     *
227
     *     // plural form + sprintf
228
     *     // equal to sprintf(ngettext('%d comment', '%d comments', 4), 4, 'Topic')
229
     *     _n('%d comment to %s', '%d comments to %s', 4, 'Topic')
230
     *
231
     * @param  string   $singular
232
     * @param  string   $plural
233
     * @param  integer  $number
234
     * @param  string[] $text [optional]
235
     *
236
     * @return string
237
     */
238
    function _n($singular, $plural, $number, ...$text)
239
    {
240
        return Translator::translatePlural($singular, $plural, $number, ...$text);
241
    }
242
}
243
// @codingStandardsIgnoreEnd
244