Completed
Pull Request — master (#410)
by Anton
22:49
created

_functions.php ➔ array_has()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
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 2
dl 0
loc 3
ccs 0
cts 0
cp 0
crap 2
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\Translator\Translator;
13
14
/**
15
 * Simple functions of framework
16
 * be careful with this way
17
 *
18
 * @author   Anton Shevchuk
19
 */
20
if (!function_exists('array_get')) {
21
    /**
22
     * Get value of array by keys
23
     *
24
     * @param $array
25
     * @param array ...$keys
26
     * @return mixed|null
27
     */
28
    function array_get(array $array, ...$keys) {
29
        return Collection::get($array, ...$keys);
30
    }
31
}
32
33
if (!function_exists('array_has')) {
34
    /**
35
     * @param $array
36
     * @param array ...$keys
37
     * @return mixed|null
38
     */
39
    function array_has(array $array, ...$keys) {
40
        return Collection::has($array, ...$keys);
41
    }
42
}
43
44
if (!function_exists('array_set')) {
45
    /**
46
     * @param $array
47
     * @param array ...$keys
48
     * @return void
49
     */
50
    function array_set(array &$array, ...$keys) {
51
        Collection::set($array, ...$keys);
52
    }
53
}
54
55
if (!function_exists('debug')) {
56
    /**
57
     * Debug variables
58
     *
59
     * Example of usage
60
     *     debug(123);
61
     *     debug(new stdClass());
62
     *     debug($_GET, $_POST, $_FILES);
63
     *
64
     * @codeCoverageIgnore
65
     *
66
     * @param mixed ...$params
67
     */
68
    function debug(...$params)
69
    {
70
        // check definition
71
        if (!getenv('BLUZ_DEBUG') || !isset($_COOKIE['BLUZ_DEBUG'])) {
72
            return;
73
        }
74 9
75
        ini_set('xdebug.var_display_max_children', '512');
76
77
        if (PHP_SAPI === 'cli') {
78
            if (extension_loaded('xdebug')) {
79
                // try to enable CLI colors
80
                ini_set('xdebug.cli_color', '1');
81
                xdebug_print_function_stack();
82
            } else {
83
                debug_print_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
84
            }
85
            var_dump($params);
86
        } else {
87
            echo '<div class="textleft clear"><pre class="bluz-debug">';
88
            debug_print_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
89
            var_dump($params);
90
            echo '</pre></div>';
91
        }
92
    }
93
}
94
95
if (!function_exists('esc')) {
96
    /**
97
     * Escape variable for use in View
98
     *
99 76
     * Example of usage
100
     *     esc($_GET['name']);
101
     *     esc($_GET['name'], ENT_QUOTES);
102
     *
103
     * @param  string  $variable
104
     * @param  integer $flags
105
     * @return string
106
     */
107
    function esc($variable, int $flags = ENT_HTML5)
108
    {
109
        return htmlentities((string)$variable, $flags, 'UTF-8');
110
    }
111
}
112
113
// @codingStandardsIgnoreStart
114
if (!function_exists('__')) {
115
    /**
116
     * Translate message
117
     *
118
     * Example of usage
119
     *
120
     *     // simple
121
     *     // equal to gettext('Message')
122
     *     __('Message');
123
     *
124
     *     // simple replace of one or more argument(s)
125
     *     // equal to sprintf(gettext('Message to %s'), 'Username')
126
     *     __('Message to %s', 'Username');
127
     *
128
     * @param  string   $message
129
     * @param  string[] $text [optional]
130
     * @return string
131
     */
132
    function __($message, ...$text)
133
    {
134
        return Translator::translate($message, ...$text);
135
    }
136
}
137
138
if (!function_exists('_n')) {
139
    /**
140
     * Translate plural form
141
     *
142
     * Example of usage
143
     *
144
     *     // plural form + sprintf
145
     *     // equal to sprintf(ngettext('%d comment', '%d comments', 4), 4)
146
     *     _n('%d comment', '%d comments', 4, 4)
147
     *
148
     *     // plural form + sprintf
149
     *     // equal to sprintf(ngettext('%d comment', '%d comments', 4), 4, 'Topic')
150
     *     _n('%d comment to %s', '%d comments to %s', 4, 'Topic')
151
     *
152
     * @param  string   $singular
153
     * @param  string   $plural
154
     * @param  integer  $number
155
     * @param  string[] $text      [optional]
156
     * @return string
157
     */
158
    function _n($singular, $plural, $number, ...$text)
159
    {
160
        return Translator::translatePlural($singular, $plural, $number, ...$text);
161
    }
162
}
163
// @codingStandardsIgnoreEnd
164