Passed
Pull Request — master (#59)
by Raúl
04:00
created

Str   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 12
c 1
b 0
f 0
dl 0
loc 38
rs 10
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A toSnakeCase() 0 10 3
A toCamelCase() 0 6 1
1
<?php
2
3
namespace Nayjest\StrCaseConverter;
4
5
class Str
6
{
7
    /**
8
     * Converts string to camel case.
9
     *
10
     * @link https://en.wikipedia.org/wiki/CamelCase
11
     *
12
     * @param string $str
13
     * @return string
14
     */
15
    public static function toCamelCase($str)
16
    {
17
        return str_replace(
18
            ' ',
19
            '',
20
            ucwords(str_replace(array('-', '_'), ' ', $str))
21
        );
22
    }
23
24
    /**
25
     * Converts string to snake case.
26
     *
27
     * @link https://en.wikipedia.org/wiki/Snake_case
28
     *
29
     * @param string $str
30
     * @param string $delimiter
31
     * @return string
32
     */
33
    public static function toSnakeCase($str, $delimiter = '_')
34
    {
35
        $str = lcfirst($str);
36
        $lowerCase = strtolower($str);
37
        $result = '';
38
        $length = strlen($str);
39
        for ($i = 0; $i < $length; $i++) {
40
            $result .= ($str[$i] === $lowerCase[$i] ? '' : $delimiter) . $lowerCase[$i];
41
        }
42
        return $result;
43
    }
44
}
45