| 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 mixed | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 | 3 |  |     public function setRfc3339($date = null, $time = null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 | 3 |  |         if (is_null($date)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |             return null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 | 3 |  |         if (!is_null($time)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 | 3 |  |             $format = Carbon::createFromFormat('Y-m-d H:i:s', "{$date} {$time}"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 | 3 |  |         } elseif (Carbon::createFromFormat('Y-m-d', $date)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 | 3 |  |             $format = Carbon::createFromFormat('Y-m-d', "{$date}"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 | 2 |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 | 3 |  |         return $format->toRfc3339String(); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |      * Change snake case to camel case | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |      * @param object|array $data Data to convert | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |      * @see https://stackoverflow.com/a/3600758 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |      * @return null | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 97 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 98 | 6 |  |     public function convertSnakeToCamel($data) | 
            
                                                                        
                            
            
                                    
            
            
                | 99 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 100 | 6 |  |         if (!is_object($data) && !is_array($data)) { | 
            
                                                                        
                            
            
                                    
            
            
                | 101 |  |  |             return null; | 
            
                                                                        
                            
            
                                    
            
            
                | 102 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 103 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 104 | 6 |  |         $dataToWork = is_object($data) ? (array)$data : $data; | 
            
                                                                        
                            
            
                                    
            
            
                | 105 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 106 | 6 |  |         foreach ($dataToWork as $k => $v) { | 
            
                                                                        
                            
            
                                    
            
            
                | 107 | 6 |  |             $newK = strtolower($k); | 
            
                                                                        
                            
            
                                    
            
            
                | 108 | 6 |  |             $newK = str_replace('_', '', ucwords($newK, '_')); | 
            
                                                                        
                            
            
                                    
            
            
                | 109 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 110 | 6 |  |             $dataToWork[$newK] = $v;    // Add element | 
            
                                                                        
                            
            
                                    
            
            
                | 111 | 6 |  |             unset($dataToWork[$k]);     // Remove element | 
            
                                                                        
                            
            
                                    
            
            
                | 112 | 4 |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 113 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 114 | 6 |  |         return is_object($data) ? (object)$dataToWork : $dataToWork; | 
            
                                                                        
                            
            
                                    
            
            
                | 115 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |      * Set ISO-6709 standard | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |      * @param object $coords Array with lat and lng | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |      * @return null | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 | 3 |  |     public function setIso6709($coords) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 | 3 |  |         if (!is_array($coords) || count($coords) != 2) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |             return null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 | 3 |  |         $coordinates = "{$coords[0]}{$coords[1]}"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 | 3 |  |         return $coordinates; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 134 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 135 |  |  |  | 
            
                        
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:
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
Check for existence of the variable explicitly:
Define a default value for the variable:
Add a value for the missing path: