Format   A
last analyzed

Complexity

Total Complexity 30

Size/Duplication

Total Lines 138
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 89.29%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 30
c 1
b 0
f 0
lcom 1
cbo 1
dl 0
loc 138
ccs 50
cts 56
cp 0.8929
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A allFormats() 0 11 3
B removeEmpty() 0 12 5
B setRfc3339() 0 14 5
B convertSnakeToCamel() 0 18 6
A setIso6709() 0 10 3
B loopAndFormat() 0 19 7
1
<?php
2
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
3
4
/**
5
 * This file is part of the Gjson library.
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 *
10
 * PHP Version 5, 7
11
 *
12
 * LICENSE: This source file is subject to the MIT license that is available
13
 * through the world-wide-web at the following URI:
14
 * http://opensource.org/licenses/mit-license.php
15
 *
16
 * @category Src
17
 * @package  Normeno\Gjson
18
 * @author   Nicolas Ormeno <[email protected]>
19
 * @license  http://opensource.org/licenses/mit-license.php MIT License
20
 * @link     https://github.com/normeno/gjson
21
 */
22
23
namespace Normeno\Gjson;
24
25
use Carbon\Carbon;
26
27
/**
28
 * Tests for Format
29
 *
30
 * @category Src
31
 * @package  Normeno\Gjson
32
 * @author   Nicolas Ormeno <[email protected]>
33
 * @license  http://opensource.org/licenses/mit-license.php MIT License
34
 * @link     https://github.com/normeno/gjson
35
 */
36
class Format
37
{
38
    /**
39
     * Create a new Skeleton Instance
40
     */
41
    public function __construct()
42
    {
43
        // constructor body
44
    }
45
46 3
    public function allFormats($collect)
47
    {
48 3
        $formatted  = $this->convertSnakeToCamel($collect);
49
50
51 3
        if (is_array($formatted) && count($formatted) > 0) {
52 3
            $formatted = $this->loopAndFormat($formatted);
53 2
        }
54
55 3
        return $formatted;
56
    }
57
58
    /**
59
     * Remove empty fields (null, '' or zero)
60
     *
61
     * @param object|array $collect items
62
     *
63
     * @return array|object
64
     */
65 3
    public function removeEmpty($collect)
66
    {
67 3
        $data = ( !is_array($collect) ) ? (array)$collect : $collect;
68
69 3
        foreach ($data as $k => $v) {
70 3
            if (empty($v)) {
71 3
                unset($data[$k]);
72 2
            }
73 2
        }
74
75 3
        return is_array($collect) ? $data : (object)$data;
76
    }
77
78
    /**
79
     * Set RFC3339 format
80
     *
81
     * @param string $date Format yyyy-mm-dd
82
     * @param string $time Format hh:ii:ss
83
     *
84
     * @return false|string
85
     */
86 6
    public function setRfc3339($date = null, $time = null)
87
    {
88 6
        if (is_null($date)
89 6
            || !\DateTime::createFromFormat('Y-m-d', $date)
90 6
            || !\DateTime::createFromFormat('Y-m-d H:i:s', "{$date} {$time}")) {
91 6
            return false;
92
        }
93
94 3
        $format = (!is_null($time))
95 3
            ? Carbon::createFromFormat('Y-m-d H:i:s', "{$date} {$time}")
96 3
            : Carbon::createFromFormat('Y-m-d', "{$date}");
97
98 3
        return $format->toRfc3339String();
99
    }
100
101
    /**
102
     * Change snake case to camel case
103
     *
104
     * @param object|array $data Data to convert
105
     *
106
     * @see https://stackoverflow.com/a/3600758
107
     *
108
     * @return false|object|array
109
     */
110 9
    public function convertSnakeToCamel($data)
111
    {
112 9
        if (!is_object($data) && !is_array($data)) {
113
            return false;
114
        }
115
116 9
        $dataToWork = is_object($data) ? (array)$data : $data;
117
118 9
        foreach ($dataToWork as $k => $v) {
119 9
            $newK = strtolower($k);
120 9
            $newK = str_replace('_', '', ucwords($newK, '_'));
121
122 9
            $dataToWork[$newK] = $v;    // Add element
123 9
            unset($dataToWork[$k]);     // Remove element
124 6
        }
125
126 9
        return is_object($data) ? (object)$dataToWork : $dataToWork;
127
    }
128
129
    /**
130
     * Set ISO-6709 standard
131
     *
132
     * @param object $coords Array with lat and lng
133
     *
134
     * @return false|array
135
     */
136 3
    public function setIso6709($coords)
137
    {
138 3
        if (!is_array($coords) || count($coords) != 2) {
139
            return false;
140
        }
141
142 3
        $coordinates = "{$coords[0]}{$coords[1]}";
143
144 3
        return $coordinates;
145
    }
146
147
    /**
148
     * Format collection with all formats
149
     *
150
     * @param array $collect collect to format
151
     *
152
     * @return array
153
     */
154 3
    private function loopAndFormat($collect)
155
    {
156 3
        foreach ($collect as $k => $v) {
157 3
            if (!is_array($v)) {
158 3
                if (\DateTime::createFromFormat('Y-m-d', $v)) {
159 3
                    $collect[$k] = $this->setRfc3339($v);
160 3
                } elseif (\DateTime::createFromFormat('Y-m-d H:i:s', $v)) {
161
                    $collect[$k] = $this->setRfc3339($v);
162
                }
163 2
            }
164
165 3
            if (is_array($v) && count($v) > 0) { // Format snake
166 3
                $formatChild = $this->convertSnakeToCamel($v);
167 3
                $collect[$k] = $formatChild;
168 2
            }
169 2
        }
170
171 3
        return $collect;
172
    }
173
}
174