Completed
Push — master ( 29f67a...08ea87 )
by Dmitry
03:47
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
class Converter
6
{
7 4
    public function toObject($data)
8
    {
9 4
        if (is_array($data)) {
10 4
            if (array_keys($data) === range(0, count($data) -1)) {
11
                foreach ($data as $k => $v) {
12
                    $data[$k] = $this->toObject($v);
13
                }
14
                return $data;
15
            }
16
        }
17
18 4
        $data = (object) $data;
19
20 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...
21 4
            if (is_array($v) || is_object($v)) {
22
                $data->$k = $this->toObject($v);
23
            }
24
        }
25
26 4
        return $data;
27
    }
28
29 1
    public function toArray($data)
30
    {
31 1
        if (!$data) {
32
            return [];
33
        }
34
35 1
        if (is_object($data)) {
36 1
            $data = get_object_vars($data);
37
        }
38
39 1 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...
40 1
            if (is_array($v) || is_object($v)) {
41
                $data[$k] = $this->toArray($v);
42
            }
43
        }
44
45 1
        return $data;
46
    }
47
48
    private $underscores = [];
49
50 1
    public function toUnderscore($input)
51
    {
52 1
        if (!array_key_exists($input, $this->underscores)) {
53 1
            preg_match_all('!([A-Z][A-Z0-9]*(?=$|[A-Z][a-z0-9])|[A-Za-z][a-z0-9]+)!', $input, $matches);
54 1
            $ret = $matches[0];
55 1
            foreach ($ret as &$match) {
56 1
                $match = $match == strtoupper($match) ? strtolower($match) : lcfirst($match);
57
            }
58 1
            $this->underscores[$input] = implode('_', $ret);
59
        }
60 1
        return $this->underscores[$input];
61
    }
62
63
    private $camelcased = [];
64
65 1
    public function toCamelCase($string, $capitalize = false)
66
    {
67 1
        $capitalize = $capitalize ? 1 : 0;
68
69 1
        if (!array_key_exists($capitalize, $this->camelcased)) {
70 1
            $this->camelcased[$capitalize] = [];
71
        }
72
73 1
        if (!array_key_exists($string, $this->camelcased[$capitalize])) {
74 1
            $chain = explode('_', $string);
75 1
            foreach ($chain as $index => $word) {
76 1
                $chain[$index] = $index || $capitalize ? ucfirst($word) : $word;
77
            }
78
79 1
            $this->camelcased[$capitalize][$string] = implode('', $chain);
80
        }
81
82 1
        return $this->camelcased[$capitalize][$string];
83
    }
84
}
85