Completed
Pull Request — master (#411)
by Anton
06:32
created

_functions.php ➔ value()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 2
nc 2
nop 1
dl 0
loc 4
ccs 1
cts 1
cp 1
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
    {
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
if (!function_exists('value')) {
117
    /**
118
     * Return the value for callable
119
     *
120
     * @param  mixed  $value
121
     * @return mixed
122
     */
123
    function value($value)
124
    {
125 5
        return is_callable($value) ? $value() : $value;
126
    }
127
}
128
129
// @codingStandardsIgnoreStart
130
if (!function_exists('__')) {
131
    /**
132
     * Translate message
133
     *
134
     * Example of usage
135
     *
136
     *     // simple
137
     *     // equal to gettext('Message')
138
     *     __('Message');
139
     *
140
     *     // simple replace of one or more argument(s)
141
     *     // equal to sprintf(gettext('Message to %s'), 'Username')
142
     *     __('Message to %s', 'Username');
143
     *
144
     * @param  string   $message
145
     * @param  string[] $text [optional]
146
     * @return string
147
     */
148
    function __($message, ...$text)
149
    {
150 76
        return Translator::translate($message, ...$text);
151
    }
152
}
153
154
if (!function_exists('_n')) {
155
    /**
156
     * Translate plural form
157
     *
158
     * Example of usage
159
     *
160
     *     // plural form + sprintf
161
     *     // equal to sprintf(ngettext('%d comment', '%d comments', 4), 4)
162
     *     _n('%d comment', '%d comments', 4, 4)
163
     *
164
     *     // plural form + sprintf
165
     *     // equal to sprintf(ngettext('%d comment', '%d comments', 4), 4, 'Topic')
166
     *     _n('%d comment to %s', '%d comments to %s', 4, 'Topic')
167
     *
168
     * @param  string   $singular
169
     * @param  string   $plural
170
     * @param  integer  $number
171
     * @param  string[] $text      [optional]
172
     * @return string
173
     */
174
    function _n($singular, $plural, $number, ...$text)
175
    {
176
        return Translator::translatePlural($singular, $plural, $number, ...$text);
177
    }
178
}
179
// @codingStandardsIgnoreEnd
180