Completed
Push — master ( ed0837...22809e )
by Dmitry
07:58
created

Converter::getDate()   B

Complexity

Conditions 6
Paths 5

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 15
ccs 9
cts 9
cp 1
rs 8.8571
c 0
b 0
f 0
cc 6
eloc 10
nc 5
nop 1
crap 6
1
<?php
2
3
namespace Basis;
4
5
use Carbon\Carbon;
6
use Exception;
7
use stdClass;
8
use Tarantool\Mapper\Entity;
9
10
class Converter
11
{
12 45
    public function isTuple($array)
13
    {
14 45
        if (is_object($array)) {
15
            $array = get_object_vars($array);
16
        }
17 45
        if (!is_array($array)) {
18 10
            return false;
19
        }
20 45
        return !count($array) || array_keys($array) === range(0, count($array) -1);
21
    }
22
23 45
    public function toObject($data)
24
    {
25 45
        if (is_array($data)) {
26 45
            if ($this->isTuple($data)) {
27 45
                return $this->convertArrayToObject($data);
28
            }
29
        }
30
31 45
        if (is_object($data)) {
32 5
            if ($data instanceof Entity) {
33
                // keep instance
34
                return $data;
35
            }
36
37 5
            $tmp = $data;
38 5
            $data = [];
39 5
            foreach ($tmp as $k => $v) {
40 1
                $data[$k] = $v;
41
            }
42
        }
43
44 45
        $data = (object) $data;
45
46 45
        foreach ($data as $k => $v) {
47 45
            if (is_array($v) && $this->isTuple($v)) {
48 45
                $data->$k = $this->convertArrayToObject($v);
49 45
            } elseif(is_array($v) || is_object($v)) {
50 45
                $data->$k = $this->toObject($v);
51
            }
52
        }
53
54 45
        return $data;
55
    }
56
57 45
    public function convertArrayToObject($data)
58
    {
59 45
        $result = [];
60 45
        foreach ($data as $k => $v) {
61 10
            if ($this->isTuple($v)) {
62
                $result[$k] = $this->convertArrayToObject($v);
63 10
            } elseif (is_object($v) || is_array($v)) {
64
                $result[$k] = $this->toObject($v);
65
            } else {
66 10
                $result[$k] = $v;
67
            }
68
        }
69 45
        return $result;
70
    }
71
72 11
    public function toArray($data) : array
73
    {
74 11
        if (!$data) {
75 5
            return [];
76
        }
77
78 11
        if (is_object($data)) {
79 7
            $data = get_object_vars($data);
80
        }
81
82 11
        foreach ($data as $k => $v) {
83 11
            if (is_array($v) || is_object($v)) {
84 11
                $data[$k] = $this->toArray($v);
85
            }
86
        }
87
88 11
        return $data;
89
    }
90
91
    private $underscores = [];
92
93 1
    public function toUnderscore(string $input) : string
94
    {
95 1
        if (!array_key_exists($input, $this->underscores)) {
96 1
            preg_match_all('!([A-Z][A-Z0-9]*(?=$|[A-Z][a-z0-9])|[A-Za-z][a-z0-9]+)!', $input, $matches);
97 1
            $ret = $matches[0];
98 1
            foreach ($ret as &$match) {
99 1
                $match = $match == strtoupper($match) ? strtolower($match) : lcfirst($match);
100
            }
101 1
            $this->underscores[$input] = implode('_', $ret);
102
        }
103 1
        return $this->underscores[$input];
104
    }
105
106
    private $camelcased = [];
107
108 1
    public function toCamelCase(string $string, bool $capitalize = false) : string
109
    {
110 1
        $capitalize = $capitalize ? 1 : 0;
111
112 1
        if (!array_key_exists($capitalize, $this->camelcased)) {
113 1
            $this->camelcased[$capitalize] = [];
114
        }
115
116 1
        if (!array_key_exists($string, $this->camelcased[$capitalize])) {
117 1
            $chain = explode('_', $string);
118 1
            foreach ($chain as $index => $word) {
119 1
                $chain[$index] = $index || $capitalize ? ucfirst($word) : $word;
120
            }
121
122 1
            $this->camelcased[$capitalize][$string] = implode('', $chain);
123
        }
124
125 1
        return $this->camelcased[$capitalize][$string];
126
    }
127
128
129
    private $dates = [];
130
131 1
    public function getDate($string)
132
    {
133 1
        if (Carbon::hasTestNow() || !array_key_exists($string, $this->dates)) {
134 1
            if (strlen($string) == 8 && is_numeric($string)) {
135 1
                $value = Carbon::createFromFormat('Ymd', $string)->setTime(0, 0, 0);
136
            } else {
137 1
                $value = Carbon::parse($string);
138
            }
139 1
            if (Carbon::hasTestNow()) {
140 1
                return $value;
141
            }
142 1
            $this->dates[$string] = $value;
143
        }
144 1
        return $this->dates[$string];
145
    }
146
147 1
    public function getTimestamp($string)
148
    {
149 1
        return $this->getDate($string)->timestamp;
150
    }
151
152
}
153