Utils::unCamelize()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 1
1
<?php
2
declare(strict_types=1);
3
4
/**
5
 * Base Infra Class
6
 * @category    Ticaje
7
 * @author      Max Demian <[email protected]>
8
 */
9
10
namespace Ticaje\Contract\Traits;
11
12
/**
13
 * Trait Utils
14
 * @package Ticaje\Contract\Traits
15
 */
16
trait Utils
17
{
18
    /**
19
     * @param $string
20
     * @param string $separator
21
     * @return string
22
     * This method converts a string into camelCase
23
     * The main constraint: the properties of a class using this trait must be compliant with the following pattern
24
     * (^[a-z]|[A-Z0-9])[a-z]*
25
     */
26
    public function camelize($string, $separator = '_')
27
    {
28
        return lcfirst(
29
            array_reduce(
30
                explode($separator, strtolower($string)),
31
                function ($carry, $value) {
32
                    $carry .= ucfirst($value);
33
                    return $carry;
34
                },
35
                ''
36
            )
37
        );
38
    }
39
40
    /**
41
     * @param $string
42
     * @return string
43
     */
44
    public function unCamelize($string)
45
    {
46
        return strtolower(preg_replace('/(?<!^)[A-Z]/', '_$0', $string));
47
    }
48
}
49