Completed
Push — master ( 192b67...c06a47 )
by Mehmet
02:57
created
src/ModelUtils.php 1 patch
Spacing   +59 added lines, -59 removed lines patch added patch discarded remove patch
@@ -29,16 +29,16 @@  discard block
 block discarded – undo
29 29
             // Does doc has a array that does not exist in model definition . 
30 30
             if (!isset($my_model[$key])) {
31 31
                 if ($my_key !== null) {
32
-                    $my_key = strval($my_key) . " . " . strval($key);
32
+                    $my_key = strval($my_key)." . ".strval($key);
33 33
                 } else {
34 34
                     $my_key = $key;
35 35
                 }
36
-                throw new \Exception("Error for key '" . $my_key . "' that does not exist in the model");
36
+                throw new \Exception("Error for key '".$my_key."' that does not exist in the model");
37 37
             }
38 38
             // Is the value of the array[key] again another array? . 
39 39
             elseif (ModelUtils::gettype($my_doc[$key]) == "array") {
40 40
                 if ($my_key !== null) {
41
-                    $my_key = strval($my_key) . " . " . strval($key);
41
+                    $my_key = strval($my_key)." . ".strval($key);
42 42
                 } else {
43 43
                     $my_key = $key;
44 44
                 }
@@ -51,16 +51,16 @@  discard block
 block discarded – undo
51 51
             // Does the value of the array[key] have same variable type that stated in the definition of the model array . 
52 52
             elseif (ModelUtils::gettype($my_doc[$key]) != $my_model[$key]['_type']) {
53 53
                 if ($my_key !== null) {
54
-                    $my_key = $my_key . " . " . $key;
54
+                    $my_key = $my_key." . ".$key;
55 55
                 } else {
56 56
                     $my_key = $key;
57 57
                 }
58
-                throw new \Exception("Error for key '" . $my_key . "'" . ", " . ModelUtils::gettype($my_doc[$key]) . 
59
-                    " given but it must be " . $my_model[$key]['_type']);
58
+                throw new \Exception("Error for key '".$my_key."'".", ".ModelUtils::gettype($my_doc[$key]). 
59
+                    " given but it must be ".$my_model[$key]['_type']);
60 60
             } else {
61 61
                 $v_key = $key;
62 62
                 if ($my_key !== null) {
63
-                    $v_key = $my_key . " . " . $key;
63
+                    $v_key = $my_key." . ".$key;
64 64
                 }
65 65
                 $my_doc[$key] = ModelUtils::validate_doc_item($my_doc[$key], $my_model[$key], $v_key);
66 66
             }
@@ -80,7 +80,7 @@  discard block
 block discarded – undo
80 80
         $my_keys = array_keys($my_doc);
81 81
         foreach ($my_keys as $key) {
82 82
             // If array has a key that is not presented in the model definition, unset it . 
83
-            if (! isset($my_model[$key])) {
83
+            if (!isset($my_model[$key])) {
84 84
                 unset($my_doc[$key]);
85 85
             }
86 86
             // If array[$key] is again an array, recursively fit this array too . 
@@ -91,8 +91,8 @@  discard block
 block discarded – undo
91 91
                     return $my_doc[$key];
92 92
                 }
93 93
             }
94
-            elseif (ModelUtils::gettype($my_doc[$key]) == "array" && $my_model[$key]['_type'] !="array") {
95
-                $my_doc[$key]=$my_model[$key]['_default'];
94
+            elseif (ModelUtils::gettype($my_doc[$key]) == "array" && $my_model[$key]['_type'] != "array") {
95
+                $my_doc[$key] = $my_model[$key]['_default'];
96 96
             }
97 97
             // If array[key] is not an array and not has same variable type that stated in the model definition . 
98 98
             else {
@@ -112,46 +112,46 @@  discard block
 block discarded – undo
112 112
     public static function setting_model_defaults($my_model, $my_doc)
113 113
     {
114 114
         $my_keys = array_keys($my_model);
115
-        $new_doc =[ ];
115
+        $new_doc = [];
116 116
         foreach ($my_keys as $key) {
117 117
             $item_keys = array_keys($my_model[$key]);
118 118
             // If one of the keys of $my_model[$key] is _type this is a definition, not a defined key
119 119
             if (in_array("_type", $item_keys)) {
120 120
                 // If array does not have this key, set the default value . 
121
-                if (! isset($my_doc[$key])) {
121
+                if (!isset($my_doc[$key])) {
122 122
                     if (isset($my_model[$key]['_input_type'])) {
123 123
                         switch ($my_model[$key]['_input_type']) {
124 124
                             case 'uid':
125 125
                                     $shortid = ShortId::create();
126
-                                    $new_doc[$key]=$shortid->generate();
126
+                                    $new_doc[$key] = $shortid->generate();
127 127
                                 break;
128 128
                             case 'date':
129
-                                if ($my_model[$key]['_default']=='today') {
129
+                                if ($my_model[$key]['_default'] == 'today') {
130 130
                                     $new_doc[$key] = date("Y-m-d");
131 131
                                 }
132 132
                                 else {
133
-                                    $new_doc[$key]=$my_model[$key]['_default'];
133
+                                    $new_doc[$key] = $my_model[$key]['_default'];
134 134
                                 }
135 135
                                 break;
136 136
                             case 'timestamp':
137
-                                if (($my_model[$key]['_default']=="now") && ($my_model[$key]['_type'] == "integer")) {
137
+                                if (($my_model[$key]['_default'] == "now") && ($my_model[$key]['_type'] == "integer")) {
138 138
                                     $new_doc[$key] = time();
139 139
                                 }
140
-                                else if ($my_model[$key]['_default']=="now" && ($my_model[$key]['_type']=="string")) {
140
+                                else if ($my_model[$key]['_default'] == "now" && ($my_model[$key]['_type'] == "string")) {
141 141
                                     
142 142
                                     $new_doc[$key] = date("Y-m-d H:i:s");
143 143
                                 }
144 144
                                 else {
145
-                                    $new_doc[$key]=$my_model[$key]['_default'];
145
+                                    $new_doc[$key] = $my_model[$key]['_default'];
146 146
                                 }
147 147
                                 break;
148 148
                     
149 149
                             default:
150
-                                $new_doc[$key]=$my_model[$key]['_default'];
150
+                                $new_doc[$key] = $my_model[$key]['_default'];
151 151
                         }
152 152
                     }
153 153
                     else {
154
-                        $new_doc[$key]=$my_model[$key]['_default'];
154
+                        $new_doc[$key] = $my_model[$key]['_default'];
155 155
                     }
156 156
                 }                
157 157
                 // If array has this key
@@ -159,7 +159,7 @@  discard block
 block discarded – undo
159 159
                     // If model definition stated this key's default value is not Null and has a wrong variable type, fix it . 
160 160
                     if ($my_model[$key]['_default'] !== null) {
161 161
                         if (ModelUtils::gettype($my_doc[$key]) != $my_model[$key]['_type'] && ModelUtils::gettype($my_doc[$key]) == "array") {
162
-                            $my_doc[$key]=$my_model[$key]['_default'];
162
+                            $my_doc[$key] = $my_model[$key]['_default'];
163 163
                         }
164 164
                         settype($my_doc[$key], $my_model[$key]['_type']);
165 165
                     }
@@ -169,7 +169,7 @@  discard block
 block discarded – undo
169 169
             }            
170 170
             // If one of the keys is not _type, this is a defined key, recursively get sub keys . 
171 171
             else {
172
-                if (! isset($my_doc[$key])) {
172
+                if (!isset($my_doc[$key])) {
173 173
                     $my_doc[$key] = "";
174 174
                 }
175 175
                 $new_doc[$key] = ModelUtils::setting_model_defaults($my_model[$key], $my_doc[$key]);
@@ -188,12 +188,12 @@  discard block
 block discarded – undo
188 188
      */
189 189
     public static function validate_doc_item($value, $my_model, $key)
190 190
     {
191
-        $type        = isset($my_model['_type'])         ? $my_model['_type']         : 'string';
192
-        $input_type  = isset($my_model['_input_type'])   ? $my_model['_input_type']   : 'string';
191
+        $type        = isset($my_model['_type']) ? $my_model['_type'] : 'string';
192
+        $input_type  = isset($my_model['_input_type']) ? $my_model['_input_type'] : 'string';
193 193
         $format      = isset($my_model['_input_format']) ? $my_model['_input_format'] : "";
194
-        $min_length  = isset($my_model['_min_length'])   ? $my_model['_min_length']   : null;
195
-        $max_length  = isset($my_model['_max_length'])   ? $my_model['_max_length']   : null;
196
-        $in_options  = isset($my_model['_in_options'])   ? $my_model['_in_options']   : null;
194
+        $min_length  = isset($my_model['_min_length']) ? $my_model['_min_length'] : null;
195
+        $max_length  = isset($my_model['_max_length']) ? $my_model['_max_length'] : null;
196
+        $in_options  = isset($my_model['_in_options']) ? $my_model['_in_options'] : null;
197 197
 
198 198
         if (ModelUtils::gettype($value) != $type) {
199 199
             return false;
@@ -203,35 +203,35 @@  discard block
 block discarded – undo
203 203
                 case 'mail':
204 204
                     $filter_check = filter_var($value, FILTER_VALIDATE_EMAIL);
205 205
                     if ($filter_check === false) {
206
-                        throw new \Exception("Error for value '" . $value . "' for '" . $key . "' couldn't pass the " . 
206
+                        throw new \Exception("Error for value '".$value."' for '".$key."' couldn't pass the ". 
207 207
                             "validation: INVALID_EMAIL_ADDRESS ");
208 208
                     }
209 209
                     break;
210 210
                 case 'bool':
211 211
                     $filter_check = filter_var($value, FILTER_VALIDATE_BOOLEAN);
212 212
                     if ($filter_check === false) {
213
-                        throw new \Exception("Error for value '" . $value . "' for '" . $key . "' couldn't pass the " . 
213
+                        throw new \Exception("Error for value '".$value."' for '".$key."' couldn't pass the ". 
214 214
                             "validation: INVALID_BOOLEAN_VALUE ");
215 215
                     }
216 216
                     break;
217 217
                 case 'url':
218 218
                     $filter_check = filter_var($value, FILTER_VALIDATE_URL);
219 219
                     if ($filter_check === false) {
220
-                        throw new \Exception("Error for value '" . $value . "' for '" . $key . "' couldn't pass the " . 
220
+                        throw new \Exception("Error for value '".$value."' for '".$key."' couldn't pass the ". 
221 221
                             "validation: INVALID_URL ");
222 222
                     }
223 223
                     break;
224 224
                 case 'ip':
225 225
                     $filter_check = filter_var($value, FILTER_VALIDATE_IP);
226 226
                     if ($filter_check === false) {
227
-                        throw new \Exception("Error for value '" . $value . "' for '" . $key . "' couldn't pass the " . 
227
+                        throw new \Exception("Error for value '".$value."' for '".$key."' couldn't pass the ". 
228 228
                             "validation: INVALID_IP_ADDRESS ");
229 229
                     }
230 230
                     break;
231 231
                 case 'mac_address':
232 232
                     $filter_check = filter_var($value, FILTER_VALIDATE_MAC);
233 233
                     if ($filter_check === false) {
234
-                        throw new \Exception("Error for value '" . $value . "' for '" . $key . "' couldn't pass the " . 
234
+                        throw new \Exception("Error for value '".$value."' for '".$key."' couldn't pass the ". 
235 235
                             "validation: INVALID_MAC_ADDRESS ");
236 236
                     }
237 237
                     break;
@@ -239,7 +239,7 @@  discard block
 block discarded – undo
239 239
                     $regex        = "/^[0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])$/";
240 240
                     $filter_check = filter_var($value, FILTER_VALIDATE_REGEXP, array("options"=>array("regexp"=> $regex)));
241 241
                     if ($filter_check === false) {
242
-                        throw new \Exception("Error for value '" . $value . "' for '" . $key . "' couldn't pass the " . 
242
+                        throw new \Exception("Error for value '".$value."' for '".$key."' couldn't pass the ". 
243 243
                             "validation: INVALID_FORMAT ");
244 244
                     }
245 245
                     break;
@@ -247,7 +247,7 @@  discard block
 block discarded – undo
247 247
                     $regex        = "/^([01]?[0-9]|2[0-3]):[0-5][0-9](:[0-5][0-9])$/";
248 248
                     $filter_check = filter_var($value, FILTER_VALIDATE_REGEXP, array("options"=>array("regexp"=> $regex)));
249 249
                     if ($filter_check === false) {
250
-                        throw new \Exception("Error for value '" . $value . "' for '" . $key . "' couldn't pass the " . 
250
+                        throw new \Exception("Error for value '".$value."' for '".$key."' couldn't pass the ". 
251 251
                             "validation: INVALID_FORMAT ");
252 252
                     }
253 253
                     break;
@@ -255,14 +255,14 @@  discard block
 block discarded – undo
255 255
                     $regex        = "/^[0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1]) ([01]?[0-9]|2[0-3]):[0-5][0-9](:[0-5][0-9])$/";
256 256
                     $filter_check = filter_var($value, FILTER_VALIDATE_REGEXP, array("options"=>array("regexp"=> $regex)));
257 257
                     if ($filter_check === false) {
258
-                        throw new \Exception("Error for value '" . $value . "' for '" . $key . "' couldn't pass the validation: INVALID_FORMAT ");
258
+                        throw new \Exception("Error for value '".$value."' for '".$key."' couldn't pass the validation: INVALID_FORMAT ");
259 259
                     }
260 260
                     break;
261 261
                 case 'regex':
262
-                    $regex        = "/^" . $format . "$/";
262
+                    $regex        = "/^".$format."$/";
263 263
                     $filter_check = filter_var($value, FILTER_VALIDATE_REGEXP, array("options"=>array("regexp"=> $regex)));
264 264
                     if ($filter_check === false) {
265
-                        throw new \Exception("Error for value '" . $value . "' for '" . $key . "' couldn't pass the " . 
265
+                        throw new \Exception("Error for value '".$value."' for '".$key."' couldn't pass the ". 
266 266
                             "validation: INVALID_FORMAT ");
267 267
                     }
268 268
                     break;
@@ -272,37 +272,37 @@  discard block
 block discarded – undo
272 272
             case 'integer':
273 273
             case 'float':
274 274
                 if ($min_length !== null) {
275
-                    if ($value < $min_length) {
276
-                        throw new \Exception("Error for value '" . $value . "' for '" . $key . "' couldn't pass the " . 
277
-                            "validation: Must be bigger than " . $min_length . "  ");
275
+                    if ($value<$min_length) {
276
+                        throw new \Exception("Error for value '".$value."' for '".$key."' couldn't pass the ". 
277
+                            "validation: Must be bigger than ".$min_length."  ");
278 278
                     }
279 279
                 }
280 280
                 if ($max_length !== null) {
281
-                    if ($value > $max_length) {
282
-                        throw new \Exception("Error for value '" . $value . "' for '" . $key . "' couldn't pass the " . 
283
-                            "validation: Must be smallerr than " . $max_length . "  ");
281
+                    if ($value>$max_length) {
282
+                        throw new \Exception("Error for value '".$value."' for '".$key."' couldn't pass the ". 
283
+                            "validation: Must be smallerr than ".$max_length."  ");
284 284
                     }
285 285
                 }
286 286
                 break;
287 287
             default:
288 288
                 if ($max_length !== null) {
289
-                    if (strlen($value) > $max_length) {
290
-                        throw new \Exception("Error for value '" . $value . "' for '" . $key . "' couldn't pass the " . 
291
-                            "validation: It's length must be smaller than " . $max_length . "  ");
289
+                    if (strlen($value)>$max_length) {
290
+                        throw new \Exception("Error for value '".$value."' for '".$key."' couldn't pass the ". 
291
+                            "validation: It's length must be smaller than ".$max_length."  ");
292 292
                     }
293 293
                 }
294 294
                 if ($min_length !== null) {
295
-                    if (strlen($value) < $min_length) {
296
-                        throw new \Exception("Error for value '" . $value . "' for '" . $key . "' couldn't pass the " . 
297
-                            "validation: It's length must be longer than " . $min_length . "  ");
295
+                    if (strlen($value)<$min_length) {
296
+                        throw new \Exception("Error for value '".$value."' for '".$key."' couldn't pass the ". 
297
+                            "validation: It's length must be longer than ".$min_length."  ");
298 298
                     }
299 299
                 }
300 300
                 break;
301 301
         }
302 302
         if ($in_options !== null) {
303 303
             if (!in_array($value, $in_options)) {
304
-                throw new \Exception("Error for value '" . $value . "' for '" . $key . "' couldn't pass the validation: " . 
305
-                    "It's length must be one of the these values: " . implode(", ", $in_options) . "  ");
304
+                throw new \Exception("Error for value '".$value."' for '".$key."' couldn't pass the validation: ". 
305
+                    "It's length must be one of the these values: ".implode(", ", $in_options)."  ");
306 306
             }
307 307
         }
308 308
 
@@ -317,11 +317,11 @@  discard block
 block discarded – undo
317 317
      */
318 318
     public static function sanitize_doc_item($value, $my_model)
319 319
     {
320
-        $type        = isset($my_model['_type'])         ? $my_model['_type']         : 'string';
321
-        $input_type  = isset($my_model['_input_type'])   ? $my_model['_input_type']   : null;
322
-        $min_length  = isset($my_model['_min_length'])   ? $my_model['_min_length']   : null;
323
-        $max_length  = isset($my_model['_max_length'])   ? $my_model['_max_length']   : null;
324
-        $in_options  = isset($my_model['_in_options'])   ? $my_model['_in_options']   : null;
320
+        $type        = isset($my_model['_type']) ? $my_model['_type'] : 'string';
321
+        $input_type  = isset($my_model['_input_type']) ? $my_model['_input_type'] : null;
322
+        $min_length  = isset($my_model['_min_length']) ? $my_model['_min_length'] : null;
323
+        $max_length  = isset($my_model['_max_length']) ? $my_model['_max_length'] : null;
324
+        $in_options  = isset($my_model['_in_options']) ? $my_model['_in_options'] : null;
325 325
 
326 326
         if ($input_type !== null) {
327 327
             switch ($input_type) {
@@ -340,19 +340,19 @@  discard block
 block discarded – undo
340 340
             case 'integer':
341 341
             case 'float':
342 342
                 if ($min_length !== null) {
343
-                    if ($value < $min_length) {
343
+                    if ($value<$min_length) {
344 344
                         $value = $min_length;
345 345
                     }
346 346
                 }
347 347
                 if ($max_length !== null) {
348
-                    if ($value > $max_length) {
348
+                    if ($value>$max_length) {
349 349
                         $value = $max_length;
350 350
                     }
351 351
                 }
352 352
                 break;
353 353
             default:
354 354
                 if ($max_length !== null) {
355
-                    if (strlen($value) > $max_length) {
355
+                    if (strlen($value)>$max_length) {
356 356
                         $value = substr($value, 0, $max_length);
357 357
                     }
358 358
                 }
Please login to merge, or discard this patch.