Completed
Branch master (cde465)
by Nicolas
02:47 queued 01:22
created

Format::setRfc3339()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 5.025

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 15
ccs 9
cts 10
cp 0.9
rs 8.8571
cc 5
eloc 9
nc 4
nop 2
crap 5.025
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
        if (\DateTime::createFromFormat('Y-m-d', $date)
93 6
            || \DateTime::createFromFormat('Y-m-d H:i:s', "{$date} {$time}")) {
94 6
            $format = (!is_null($time))
95 5
                ? Carbon::createFromFormat('Y-m-d H:i:s', "{$date} {$time}")
96 6
                : Carbon::createFromFormat('Y-m-d', "{$date}");
97 4
        }
98
99 6
        return $format->toRfc3339String();
0 ignored issues
show
Bug introduced by
The variable $format does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
100
    }
101
102
    /**
103
     * Change snake case to camel case
104
     *
105
     * @param object|array $data Data to convert
106
     *
107
     * @see https://stackoverflow.com/a/3600758
108
     *
109
     * @return false|object|array
110
     */
111 9
    public function convertSnakeToCamel($data)
112
    {
113 9
        if (!is_object($data) && !is_array($data)) {
114
            return false;
115
        }
116
117 9
        $dataToWork = is_object($data) ? (array)$data : $data;
118
119 9
        foreach ($dataToWork as $k => $v) {
120 9
            $newK = strtolower($k);
121 9
            $newK = str_replace('_', '', ucwords($newK, '_'));
122
123 9
            $dataToWork[$newK] = $v;    // Add element
124 9
            unset($dataToWork[$k]);     // Remove element
125 6
        }
126
127 9
        return is_object($data) ? (object)$dataToWork : $dataToWork;
128
    }
129
130
    /**
131
     * Set ISO-6709 standard
132
     *
133
     * @param object $coords Array with lat and lng
134
     *
135
     * @return false|array
136
     */
137 3
    public function setIso6709($coords)
138
    {
139 3
        if (!is_array($coords) || count($coords) != 2) {
140
            return false;
141
        }
142
143 3
        $coordinates = "{$coords[0]}{$coords[1]}";
144
145 3
        return $coordinates;
146
    }
147
148
    /**
149
     * Format collection with all formats
150
     *
151
     * @param array $collect collect to format
152
     *
153
     * @return array
154
     */
155 3
    private function loopAndFormat($collect)
156
    {
157 3
        foreach ($collect as $k => $v) {
158 3
            if (!is_array($v)) {
159 3
                if (\DateTime::createFromFormat('Y-m-d', $v)) {
160 3
                    $collect[$k] = $this->setRfc3339($v);
161 3
                } elseif (\DateTime::createFromFormat('Y-m-d H:i:s', $v)) {
162
                    $collect[$k] = $this->setRfc3339($v);
163
                }
164 2
            }
165
166 3
            if (is_array($v) && count($v) > 0) { // Format snake
167 3
                $formatChild = $this->convertSnakeToCamel($v);
168 3
                $collect[$k] = $formatChild;
169 2
            }
170 2
        }
171
172 3
        return $collect;
173
    }
174
}
175