Test Failed
Push — main ( ad999b...e26c51 )
by Rafael
06:04
created

Utils::snakeToCamel()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 2
dl 0
loc 3
rs 10
c 1
b 0
f 0
1
<?php
2
/**
3
 * Copyright (C) 2022-2023  Rafael San José Tovar   <[email protected]>
4
 *
5
 * This program is free software; you can redistribute it and/or modify
6
 * it under the terms of the GNU General Public License as published by
7
 * the Free Software Foundation; either version 3 of the License, or
8
 * (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program. If not, see <https://www.gnu.org/licenses/>.
17
 */
18
19
namespace Alxarafe\Core\Utils;
20
21
/**
22
 * Class Utils
23
 *
24
 * @package Alxarafe\Helpers
25
 */
26
class Utils
27
{
28
29
    /**
30
     * Translate a literal in CamelCase format to snake_case format
31
     *
32
     * @param string $string
33
     * @param string $us
34
     *
35
     * @return string
36
     */
37
    public static function camelToSnake($string, $us = '_'): string
38
    {
39
        $patterns = [
40
            '/([a-z]+)([0-9]+)/i',
41
            '/([a-z]+)([A-Z]+)/',
42
            '/([0-9]+)([a-z]+)/i',
43
        ];
44
        $string = preg_replace($patterns, '$1' . $us . '$2', $string);
45
46
        // Lowercase
47
        $string = strtolower($string);
48
49
        return $string;
50
    }
51
52
    /**
53
     * Translate a literal in snake_case format to CamelCase format
54
     *
55
     * @param string $string
56
     * @param string $us
57
     *
58
     * @return string
59
     */
60
    public static function snakeToCamel($string, $us = '_'): string
61
    {
62
        return str_replace($us, '', ucwords($string, $us));
63
    }
64
65
    /**
66
     * Define a constant if it does not exist
67
     *
68
     * @param string $const
69
     * @param        $value
70
     */
71
    public static function defineIfNotExists(string $const, $value)
72
    {
73
        if (!defined($const)) {
74
            define($const, $value);
75
        }
76
    }
77
78
    /**
79
     * Flatten an array to leave it at a single level.
80
     * Ignore the value of the indexes of the array, taking only the values.
81
     * Remove spaces from the result and convert it to lowercase.
82
     *
83
     * @param array $array
84
     *
85
     * @return array
86
     */
87
    public static function flatArray(array $array): array
88
    {
89
        $ret = [];
90
        foreach ($array as $value) {
91
            if (is_array($value)) {
92
                // We expect that the indexes will not overlap
93
                $ret = array_merge($ret, self::flatArray($value));
94
            } else {
95
                $ret[] = strtolower(trim($value));
96
            }
97
        }
98
        return $ret;
99
    }
100
101
    /**
102
     * Crea una carpeta con los permisos seleccionados.
103
     *
104
     * @author  Rafael San José Tovar <[email protected]>
105
     * @version 2023.0101
106
     *
107
     * @param string $directory
108
     * @param int    $permissions
109
     * @param bool   $recursive
110
     *
111
     * @return bool
112
     */
113
    public static function createDir(string $directory, int $permissions = 0777, bool $recursive = true): bool
114
    {
115
        return (\is_dir($directory) || @\mkdir($directory, $permissions, $recursive) || \is_dir($directory));
116
    }
117
    public static function delTree($dir)
118
    {
119
        $files = scandir($dir);
120
        if ($files === false) {
121
            return false;
122
        }
123
        $files = array_diff($files, ['.', '..']);
124
        foreach ($files as $file) {
125
            (is_dir("$dir/$file")) ? self::delTree("$dir/$file") : unlink("$dir/$file");
126
        }
127
        return rmdir($dir);
128
    }
129
}
130