Completed
Pull Request — master (#410)
by Anton
07:16
created

_functions.php ➔ array_set()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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