Completed
Push — master ( 011717...660375 )
by Dmitry
04:27
created

Converter::toCamelCase()   B

Complexity

Conditions 7
Paths 8

Size

Total Lines 19
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 7

Importance

Changes 0
Metric Value
dl 0
loc 19
ccs 10
cts 10
cp 1
rs 8.2222
c 0
b 0
f 0
cc 7
eloc 10
nc 8
nop 2
crap 7
1
<?php
2
3
namespace Basis;
4
5
use stdClass;
6
7
class Converter
8
{
9 13
    public function toObject($data) : stdClass
10
    {
11 13
        if (is_array($data)) {
12 13
            if (array_keys($data) === range(0, count($data) -1)) {
13
                foreach ($data as $k => $v) {
14
                    $data[$k] = $this->toObject($v);
15
                }
16
                return $data;
17
            }
18
        }
19
20 13
        $data = (object) $data;
21
22 13 View Code Duplication
        foreach ($data as $k => $v) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
23 13
            if (is_array($v) || is_object($v)) {
24 13
                $data->$k = $this->toObject($v);
25
            }
26
        }
27
28 13
        return $data;
29
    }
30
31 4
    public function toArray($data) : array
32
    {
33 4
        if (!$data) {
34
            return [];
35
        }
36
37 4
        if (is_object($data)) {
38 4
            $data = get_object_vars($data);
39
        }
40
41 4 View Code Duplication
        foreach ($data as $k => $v) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
42
            if (is_array($v) || is_object($v)) {
43
                $data[$k] = $this->toArray($v);
44
            }
45
        }
46
47 4
        return $data;
48
    }
49
50
    private $underscores = [];
51
52 1
    public function toUnderscore(string $input) : string
53
    {
54 1
        if (!array_key_exists($input, $this->underscores)) {
55 1
            preg_match_all('!([A-Z][A-Z0-9]*(?=$|[A-Z][a-z0-9])|[A-Za-z][a-z0-9]+)!', $input, $matches);
56 1
            $ret = $matches[0];
57 1
            foreach ($ret as &$match) {
58 1
                $match = $match == strtoupper($match) ? strtolower($match) : lcfirst($match);
59
            }
60 1
            $this->underscores[$input] = implode('_', $ret);
61
        }
62 1
        return $this->underscores[$input];
63
    }
64
65
    private $camelcased = [];
66
67 1
    public function toCamelCase(string $string, bool $capitalize = false) : string
68
    {
69 1
        $capitalize = $capitalize ? 1 : 0;
70
71 1
        if (!array_key_exists($capitalize, $this->camelcased)) {
72 1
            $this->camelcased[$capitalize] = [];
73
        }
74
75 1
        if (!array_key_exists($string, $this->camelcased[$capitalize])) {
76 1
            $chain = explode('_', $string);
77 1
            foreach ($chain as $index => $word) {
78 1
                $chain[$index] = $index || $capitalize ? ucfirst($word) : $word;
79
            }
80
81 1
            $this->camelcased[$capitalize][$string] = implode('', $chain);
82
        }
83
84 1
        return $this->camelcased[$capitalize][$string];
85
    }
86
}
87