Completed
Branch master (d20c76)
by Nicolas
02:33 queued 01:17
created

Format::setRfc3339()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3.0261

Importance

Changes 0
Metric Value
cc 3
eloc 7
nc 3
nop 2
dl 0
loc 12
ccs 6
cts 7
cp 0.8571
crap 3.0261
rs 9.4285
c 0
b 0
f 0
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
    /**
47
     * Remove empty fields (null, '' or zero)
48
     *
49
     * @param object|array $collect items
50
     *
51
     * @return mixed
52
     */
53 3
    public function removeEmpty($collect)
54
    {
55 3
        $data = ( !is_array($collect) ) ? (array)$collect : $collect;
56
57 3
        foreach ($data as $k => $v) {
58 3
            if (empty($v)) {
59 3
                unset($data[$k]);
60 2
            }
61 2
        }
62
63 3
        return is_array($collect) ? $data : (object)$data;
64
    }
65
66
    /**
67
     * Set RFC3339 format
68
     *
69
     * @param string $date Format yyyy-mm-dd
70
     * @param string $time Format hh:ii:ss
71
     *
72
     * @return null|string
73
     */
74 3
    public function setRfc3339($date = null, $time = null)
75
    {
76 3
        if (is_null($date)) {
77
            return null;
78
        }
79
80 3
        $format = (!is_null($time))
81 3
            ? Carbon::createFromFormat('Y-m-d H:i:s', "{$date} {$time}")
82 3
            : Carbon::createFromFormat('Y-m-d', "{$date}");
83
84 3
        return $format->toRfc3339String();
85
    }
86
87
    /**
88
     * Change snake case to camel case
89
     *
90
     * @param object|array $data Data to convert
91
     *
92
     * @see https://stackoverflow.com/a/3600758
93
     *
94
     * @return null
95
     */
96 6
    public function convertSnakeToCamel($data)
97
    {
98 6
        if (!is_object($data) && !is_array($data)) {
99
            return null;
100
        }
101
102 6
        $dataToWork = is_object($data) ? (array)$data : $data;
103
104 6
        foreach ($dataToWork as $k => $v) {
105 6
            $newK = strtolower($k);
106 6
            $newK = str_replace('_', '', ucwords($newK, '_'));
107
108 6
            $dataToWork[$newK] = $v;    // Add element
109 6
            unset($dataToWork[$k]);     // Remove element
110 4
        }
111
112 6
        return is_object($data) ? (object)$dataToWork : $dataToWork;
113
    }
114
115
    /**
116
     * Set ISO-6709 standard
117
     *
118
     * @param object $coords Array with lat and lng
119
     *
120
     * @return null
121
     */
122 3
    public function setIso6709($coords)
123
    {
124 3
        if (!is_array($coords) || count($coords) != 2) {
125
            return null;
126
        }
127
128 3
        $coordinates = "{$coords[0]}{$coords[1]}";
129
130 3
        return $coordinates;
131
    }
132
}
133