StringConverter   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 27
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 3
eloc 16
c 1
b 0
f 0
dl 0
loc 27
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A toCamelCase() 0 9 1
A toSlug() 0 14 2
1
<?php
2
3
/*
4
 * This file is part of the SexyField package.
5
 *
6
 * (c) Dion Snoeijen <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare (strict_types = 1);
13
14
namespace Tardigrades\Helper;
15
16
class StringConverter
17
{
18
    public static function toCamelCase(string $string, array $noStrip = []): string
19
    {
20
        $string = preg_replace('/[^a-z0-9' . implode('', $noStrip) . ']+/i', ' ', $string);
21
        $string = trim($string);
22
        $string = ucwords($string);
23
        $string = str_replace(" ", "", $string);
24
        $string = lcfirst($string);
25
26
        return $string;
27
    }
28
29
    public static function toSlug(string $string): string
30
    {
31
        $string = preg_replace('~[^\pL\d]+~u', '-', $string);
32
        $string = iconv('utf-8', 'us-ascii//TRANSLIT', $string);
33
        $string = preg_replace('~[^-\w]+~', '', $string);
34
        $string = trim($string, '-');
35
        $string = preg_replace('~-+~', '-', $string);
36
        $string = strtolower($string);
37
38
        if (empty($string)) {
39
            return 'n-a';
40
        }
41
42
        return $string;
43
    }
44
}
45