Test Failed
Push — main ( 5edb73...d2cb63 )
by Fractal
02:48
created

StringHelper   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 11
dl 0
loc 42
rs 10
c 0
b 0
f 0
wmc 9

8 Methods

Rating   Name   Duplication   Size   Complexity  
A contains() 0 3 1
A toSnakeCase() 0 3 1
A toKebabCase() 0 3 1
A makePrefix() 0 5 2
A toCamelCase() 0 3 1
A normalize() 0 3 1
A removeBrackets() 0 3 1
A toLowerCamelCase() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace FRZB\Component\RequestMapper\Helper;
6
7
/**
8
 * @internal
9
 */
10
final class StringHelper
11
{
12
    public static function toSnakeCase(string $value): string
13
    {
14
        return strtolower(preg_replace('/[A-Z]/', '_\\0', lcfirst($value)));
15
    }
16
17
    public static function toKebabCase(string $value): string
18
    {
19
        return strtolower(preg_replace('/[A-Z]/', '-\\0', lcfirst($value)));
20
    }
21
22
    public static function toCamelCase(string $value): string
23
    {
24
        return str_replace(' ', '', ucwords(str_replace(['-', '_'], ' ', $value)));
25
    }
26
27
    public static function toLowerCamelCase(string $value): string
28
    {
29
        return lcfirst(str_replace(' ', '', ucwords(str_replace(['-', '_'], ' ', $value))));
30
    }
31
32
    public static function contains(string $value, string $subValue): bool
33
    {
34
        return str_contains($value, $subValue);
35
    }
36
37
    public static function makePrefix(string $prefix, ?string $value = null, string $delimiter = '-'): string
38
    {
39
        return $value
40
            ? self::normalize($prefix).$delimiter.$value
41
            : self::normalize($prefix);
42
    }
43
44
    public static function normalize(string $value): string
45
    {
46
        return strtolower(preg_replace('/[^a-zA-Z\\d_-]/', '-', $value));
47
    }
48
49
    public static function removeBrackets(string $value, array $brackets = ['[', ']']): string
50
    {
51
        return str_replace($brackets, '', $value);
52
    }
53
}
54