Completed
Pull Request — master (#413)
by Anton
05:33
created

_functions.php ➔ to_camel_case()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
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\Line;
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
     * @return mixed|null
28
     */
29
    function array_get(array $array, ...$keys)
30
    {
31 1
        return Collection::get($array, ...$keys);
32
    }
33
}
34
35
if (!function_exists('array_has')) {
36
    /**
37
     * @param $array
38
     * @param array ...$keys
39
     * @return bool
40
     */
41
    function array_has(array $array, ...$keys)
42
    {
43 1
        return Collection::has($array, ...$keys);
44
    }
45
}
46
47
if (!function_exists('array_add')) {
48
    /**
49
     * @param $array
50
     * @param array ...$keys
51
     * @return void
52
     */
53
    function array_add(array &$array, ...$keys)
54
    {
55 1
        Collection::add($array, ...$keys);
56 1
    }
57
}
58
59
if (!function_exists('array_set')) {
60
    /**
61
     * @param $array
62
     * @param array ...$keys
63
     * @return void
64
     */
65
    function array_set(array &$array, ...$keys)
66
    {
67 1
        Collection::set($array, ...$keys);
68 1
    }
69
}
70
71
if (!function_exists('debug')) {
72
    /**
73
     * Debug variables
74
     *
75
     * Example of usage
76
     *     debug(123);
77
     *     debug(new stdClass());
78
     *     debug($_GET, $_POST, $_FILES);
79
     *
80
     * @codeCoverageIgnore
81
     *
82
     * @param mixed ...$params
83
     */
84
    function debug(...$params)
85
    {
86
        // check definition
87
        if (!getenv('BLUZ_DEBUG') || !isset($_COOKIE['BLUZ_DEBUG'])) {
88
            return;
89
        }
90
91
        ini_set('xdebug.var_display_max_children', '512');
92
93
        if (PHP_SAPI === 'cli') {
94
            if (extension_loaded('xdebug')) {
95
                // try to enable CLI colors
96
                ini_set('xdebug.cli_color', '1');
97
                xdebug_print_function_stack();
98
            } else {
99
                debug_print_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
100
            }
101
            var_dump($params);
102
        } else {
103
            echo '<div class="textleft clear"><pre class="bluz-debug">';
104
            debug_print_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
105
            var_dump($params);
106
            echo '</pre></div>';
107
        }
108
    }
109
}
110
111
if (!function_exists('esc')) {
112
    /**
113
     * Escape variable for use in View
114
     *
115
     * Example of usage
116
     *     esc($_GET['name']);
117
     *     esc($_GET['name'], ENT_QUOTES);
118
     *
119
     * @param  string  $variable
120
     * @param  integer $flags
121
     * @return string
122
     */
123
    function esc($variable, int $flags = ENT_HTML5)
124
    {
125 9
        return htmlentities((string)$variable, $flags, 'UTF-8');
126
    }
127
}
128
129
if (!function_exists('str_trim_end')) {
130
    /**
131
     * Added symbol to end of string, trim it before
132
     *
133
     * @param  string $subject
134
     * @param  string $symbols
135
     * @return mixed
136
     */
137
    function str_trim_end($subject, $symbols)
138
    {
139 713
        return rtrim($subject, $symbols) . $symbols;
140
    }
141
}
142
143
if (!function_exists('to_camel_case')) {
144
    /**
145
     * Convert string to camel case
146
     *
147
     * @param  string $subject
148
     * @return string
149
     */
150
    function to_camel_case($subject)
151
    {
152 4
        return Line::toCamelCase($subject);
153
    }
154
}
155
156
if (!function_exists('value')) {
157
    /**
158
     * Return the value for callable
159
     *
160
     * @param  mixed  $value
161
     * @return mixed
162
     */
163
    function value($value)
164
    {
165 5
        return is_callable($value) ? $value() : $value;
166
    }
167
}
168
169
// @codingStandardsIgnoreStart
170
if (!function_exists('__')) {
171
    /**
172
     * Translate message
173
     *
174
     * Example of usage
175
     *
176
     *     // simple
177
     *     // equal to gettext('Message')
178
     *     __('Message');
179
     *
180
     *     // simple replace of one or more argument(s)
181
     *     // equal to sprintf(gettext('Message to %s'), 'Username')
182
     *     __('Message to %s', 'Username');
183
     *
184
     * @param  string   $message
185
     * @param  string[] $text [optional]
186
     * @return string
187
     */
188
    function __($message, ...$text)
189
    {
190 76
        return Translator::translate($message, ...$text);
191
    }
192
}
193
194
if (!function_exists('_n')) {
195
    /**
196
     * Translate plural form
197
     *
198
     * Example of usage
199
     *
200
     *     // plural form + sprintf
201
     *     // equal to sprintf(ngettext('%d comment', '%d comments', 4), 4)
202
     *     _n('%d comment', '%d comments', 4, 4)
203
     *
204
     *     // plural form + sprintf
205
     *     // equal to sprintf(ngettext('%d comment', '%d comments', 4), 4, 'Topic')
206
     *     _n('%d comment to %s', '%d comments to %s', 4, 'Topic')
207
     *
208
     * @param  string   $singular
209
     * @param  string   $plural
210
     * @param  integer  $number
211
     * @param  string[] $text      [optional]
212
     * @return string
213
     */
214
    function _n($singular, $plural, $number, ...$text)
215
    {
216
        return Translator::translatePlural($singular, $plural, $number, ...$text);
217
    }
218
}
219
// @codingStandardsIgnoreEnd
220