StringUtils   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 95
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 17
c 2
b 0
f 0
dl 0
loc 95
rs 10
wmc 7

6 Methods

Rating   Name   Duplication   Size   Complexity  
A camelize() 0 9 2
A underscore() 0 15 1
A startsWith() 0 3 1
A start() 0 3 1
A endsWith() 0 3 1
A end() 0 3 1
1
<?php
2
3
namespace LAG\Component\StringUtils;
4
5
class StringUtils
6
{
7
    /**
8
     * Camelize a string.
9
     *
10
     * @param string $id A string to camelize
11
     * @param bool   $lowerCase
12
     *
13
     * @return string The camelized string
14
     */
15
    public static function camelize(string $id, bool $lowerCase = false): string
16
    {
17
        $value = strtr(ucwords(strtr($id, ['_' => ' ', '.' => '_ ', '\\' => '_ '])), [' ' => '']);
18
19
        if ($lowerCase) {
20
            $value = strtolower($value);
21
        }
22
23
        return $value;
24
    }
25
26
    /**
27
     * A string to underscore.
28
     *
29
     * @param string $value The string to underscore
30
     *
31
     * @return string The underscored string
32
     */
33
    public static function underscore(string $value): string
34
    {
35
        $value = strtolower(preg_replace([
36
                '/([A-Z]+)([A-Z][a-z])/',
37
                '/([a-z\d])([A-Z])/',
38
            ], [
39
                '\\1_\\2',
40
                '\\1_\\2',
41
            ],
42
                str_replace('_', '.', $value))
43
        );
44
45
        $value = preg_replace('/\B([A-Z])/', '_$1', $value);
46
47
        return $value;
48
    }
49
50
    /**
51
     * Return the start of a string.
52
     *
53
     * @param string $string The string
54
     * @param int $length The number of characters
55
     *
56
     * @return string
57
     */
58
    public static function start(string $string, int $length = 1): string
59
    {
60
        return substr($string, 0, $length);
61
    }
62
63
    /**
64
     * Return the end of a string.
65
     *
66
     * @param string $string The string
67
     * @param int $length The number of characters
68
     *
69
     * @return string
70
     */
71
    public static function end(string $string, int $length = 1): string
72
    {
73
        return substr($string, -$length);
74
    }
75
76
    /**
77
     * Return true if the given string starts with $start.
78
     *
79
     * @param string $string
80
     * @param string $start
81
     *
82
     * @return bool
83
     */
84
    public static function startsWith(string $string, string $start): bool
85
    {
86
        return self::start($string, strlen($start)) === $start;
87
    }
88
89
    /**
90
     * Return true if the given string starts with $start.
91
     *
92
     * @param string $string
93
     * @param string $end
94
     *
95
     * @return bool
96
     */
97
    public static function endsWith(string $string, string $end): bool
98
    {
99
        return self::end($string, strlen($end)) === $end;
100
    }
101
}
102