Strings   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 4
eloc 9
dl 0
loc 46
ccs 9
cts 9
cp 1
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A toPascalCase() 0 3 1
A firstLower() 0 3 1
A toCamelCase() 0 5 1
A toSnakeCase() 0 6 1
1
<?php
2
3
namespace kalanis\Restful\Utils;
4
5
6
use Nette;
7
8
9
/**
10
 * Strings util class
11
 * @package kalanis\Restful\Utils
12
 */
13 1
class Strings extends Nette\Utils\Strings
14
{
15
16
    /**
17
     * Converts string to PascalCase
18
     * @param string $string
19
     * @return string
20
     */
21
    public static function toPascalCase(string $string): string
22
    {
23 1
        return self::firstUpper(self::toCamelCase($string));
24
    }
25
26
    /**
27
     * Converts string to camelCase
28
     * @param string $string
29
     * @return string
30
     */
31
    public static function toCamelCase(string $string): string
32
    {
33 1
        $func = fn($matches): string => self::upper($matches[2]);
34
35 1
        return self::firstLower(self::replace($string, '/(_| |-)([a-zA-Z])/', $func));
36
    }
37
38
    /**
39
     * Converts first letter to lower case
40
     * @param string $s
41
     * @return string
42
     */
43
    public static function firstLower(string $s): string
44
    {
45 1
        return self::lower(self::substring($s, 0, 1)) . self::substring($s, 1);
46
    }
47
48
    /**
49
     * Converts string to snake_case
50
     * @param string $string
51
     * @return string
52
     */
53
    public static function toSnakeCase(string $string): string
54
    {
55 1
        $replace = [' ', '-'];
56 1
        return self::trim(
57 1
            self::lower(
58 1
                str_replace($replace, '_', self::replace(ltrim($string, '!'), '/([^_]+[a-z -]{1})([A-Z])/U', '$1_$2'))
59
            )
60
        );
61
    }
62
}
63