Completed
Push — master ( 23871e...90b1af )
by Dmitry
11:08 queued 08:52
created

Converter::toUnderscore()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 12
ccs 8
cts 8
cp 1
rs 9.2
c 0
b 0
f 0
cc 4
eloc 8
nc 2
nop 1
crap 4
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