Completed
Branch master (40005c)
by Nicolas
02:35 queued 01:18
created

Format::loopAndFormat()   B

Complexity

Conditions 7
Paths 9

Size

Total Lines 19
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 7.116

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 19
ccs 13
cts 15
cp 0.8667
rs 8.2222
cc 7
eloc 11
nc 9
nop 1
crap 7.116
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
            return false;
90
        }
91
92 6
        $format = (!is_null($time))
93 5
            ? Carbon::createFromFormat('Y-m-d H:i:s', "{$date} {$time}")
94 6
            : Carbon::createFromFormat('Y-m-d', "{$date}");
95
96 6
        return $format->toRfc3339String();
97
    }
98
99
    /**
100
     * Change snake case to camel case
101
     *
102
     * @param object|array $data Data to convert
103
     *
104
     * @see https://stackoverflow.com/a/3600758
105
     *
106
     * @return false|object|array
107
     */
108 9
    public function convertSnakeToCamel($data)
109
    {
110 9
        if (!is_object($data) && !is_array($data)) {
111
            return false;
112
        }
113
114 9
        $dataToWork = is_object($data) ? (array)$data : $data;
115
116 9
        foreach ($dataToWork as $k => $v) {
117 9
            $newK = strtolower($k);
118 9
            $newK = str_replace('_', '', ucwords($newK, '_'));
119
120 9
            $dataToWork[$newK] = $v;    // Add element
121 9
            unset($dataToWork[$k]);     // Remove element
122 6
        }
123
124 9
        return is_object($data) ? (object)$dataToWork : $dataToWork;
125
    }
126
127
    /**
128
     * Set ISO-6709 standard
129
     *
130
     * @param object $coords Array with lat and lng
131
     *
132
     * @return false|array
133
     */
134 3
    public function setIso6709($coords)
135
    {
136 3
        if (!is_array($coords) || count($coords) != 2) {
137
            return false;
138
        }
139
140 3
        $coordinates = "{$coords[0]}{$coords[1]}";
141
142 3
        return $coordinates;
143
    }
144
145
    /**
146
     * Format collection with all formats
147
     *
148
     * @param array $collect collect to format
149
     *
150
     * @return array
151
     */
152 3
    private function loopAndFormat($collect)
153
    {
154 3
        foreach ($collect as $k => $v) {
155 3
            if (!is_array($v)) {
156 3
                if (\DateTime::createFromFormat('Y-m-d', $v)) {
157 3
                    $collect[$k] = $this->setRfc3339($v);
158 3
                } elseif (\DateTime::createFromFormat('Y-m-d H:i:s', $v)) {
159
                    $collect[$k] = $this->setRfc3339($v);
160
                }
161 2
            }
162
163 3
            if (is_array($v) && count($v) > 0) { // Format snake
164 3
                $formatChild = $this->convertSnakeToCamel($v);
165 3
                $collect[$k] = $formatChild;
166 2
            }
167 2
        }
168
169 3
        return $collect;
170
    }
171
}
172