Issues (43)

app/helpers/string.php (1 issue)

1
<?php
2
3
/**
4
 * Explode string by lines
5
 * @param string $multiline_string - string to be exploded
6
 *
7
 * @return array - array of lines
8
 */
9
function explode_by_new_line(string $multiline_string): array
10
{
11
    return preg_split("/\r\n|\n|\r/", $multiline_string);
0 ignored issues
show
Bug Best Practice introduced by
The expression return preg_split('/ | | /', $multiline_string) could return the type false which is incompatible with the type-hinted return array. Consider adding an additional type-check to rule them out.
Loading history...
12
}
13
14
/**
15
 * Parses srings like main_settings_section to Main settings section
16
 */
17
function parse_to_readable(string $source): string
18
{
19
    return ucfirst(
20
        preg_replace(
21
            '/_+/',
22
            ' ',
23
            mb_strtolower($source)
24
        )
25
    );
26
}
27
28
/**
29
 * Converts strings like addon-xml to addonXml
30
 * @param string $string 
31
 * 
32
 * @return string
33
 */
34
function to_camel_case(string $string): string
35
{
36
    return preg_replace_callback('/(-(\w+))/', function($matches) {
37
        return ucfirst($matches[2]);
38
    }, $string);
39
}
40
41
/**
42
 * Converts strings like addon-xml to AddonXml
43
 * @param string $string
44
 * 
45
 * @return string
46
 */
47
function to_studly_caps(string $string): string
48
{
49
    return ucfirst(to_camel_case($string));
50
}
51
52
/**
53
 * Converts strings like addonXml to addon-xml
54
 * @param string $string
55
 * 
56
 * @return string
57
 */
58
function to_lower_case(string $string): string
59
{
60
    return preg_replace_callback('/([A-Z]+)/', function($matches) {
61
        return '-' . strtolower($matches[1]);
62
    }, lcfirst($string));
63
}
64
65
/**
66
 * Parse command line like arguments into array
67
 * 
68
 * @param string $command
69
 * 
70
 * @return array
71
 */
72
function arguments(string $command) {
73
    $arguments = [];
74
75
    preg_replace_callback(
76
        '/--([\w\.\-_]+)\s*("([^"\\\]*(\\\.[^"\\\]*)*)"|[\w\d][\w\d\.\-_]*)?/ius',
77
        function($matches) use (&$arguments) {
78
            $key = $matches[1];
79
            $value = true;
80
81
            switch(count($matches)) {
82
                case 4:
83
                case 5:
84
                    $value = $matches[3];
85
                break;
86
                case 3:
87
                    $value = $matches[2];
88
                break;
89
            }
90
            
91
            $arguments[$key] = $value;
92
        },
93
        // 'ccg.php addon/create --addon.id "new_addon" --langvar "say my name \"Daniel\"" --cur "" --developer mikhail ddfgd --test');
94
        $command
95
    );
96
97
    return $arguments;
98
}
99