Completed
Push — master ( c06a47...fbe281 )
by Mehmet
02:46
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 . 
@@ -90,8 +90,8 @@  discard block
 block discarded – undo
90 90
                 if (ModelUtils::gettype($my_doc[$key]) != "array") {
91 91
                     return $my_doc[$key];
92 92
                 }
93
-            } elseif (ModelUtils::gettype($my_doc[$key]) == "array" && $my_model[$key]['_type'] !="array") {
94
-                $my_doc[$key]=$my_model[$key]['_default'];
93
+            } elseif (ModelUtils::gettype($my_doc[$key]) == "array" && $my_model[$key]['_type'] != "array") {
94
+                $my_doc[$key] = $my_model[$key]['_default'];
95 95
             }
96 96
             // If array[key] is not an array and not has same variable type that stated in the model definition . 
97 97
             else {
@@ -111,42 +111,42 @@  discard block
 block discarded – undo
111 111
     public static function setting_model_defaults($my_model, $my_doc)
112 112
     {
113 113
         $my_keys = array_keys($my_model);
114
-        $new_doc =[ ];
114
+        $new_doc = [];
115 115
         foreach ($my_keys as $key) {
116 116
             $item_keys = array_keys($my_model[$key]);
117 117
             // If one of the keys of $my_model[$key] is _type this is a definition, not a defined key
118 118
             if (in_array("_type", $item_keys)) {
119 119
                 // If array does not have this key, set the default value . 
120
-                if (! isset($my_doc[$key])) {
120
+                if (!isset($my_doc[$key])) {
121 121
                     if (isset($my_model[$key]['_input_type'])) {
122 122
                         switch ($my_model[$key]['_input_type']) {
123 123
                             case 'uid':
124 124
                                     $shortid = ShortId::create();
125
-                                    $new_doc[$key]=$shortid->generate();
125
+                                    $new_doc[$key] = $shortid->generate();
126 126
                                 break;
127 127
                             case 'date':
128
-                                if ($my_model[$key]['_default']=='today') {
128
+                                if ($my_model[$key]['_default'] == 'today') {
129 129
                                     $new_doc[$key] = date("Y-m-d");
130 130
                                 } else {
131
-                                    $new_doc[$key]=$my_model[$key]['_default'];
131
+                                    $new_doc[$key] = $my_model[$key]['_default'];
132 132
                                 }
133 133
                                 break;
134 134
                             case 'timestamp':
135
-                                if (($my_model[$key]['_default']=="now") && ($my_model[$key]['_type'] == "integer")) {
135
+                                if (($my_model[$key]['_default'] == "now") && ($my_model[$key]['_type'] == "integer")) {
136 136
                                     $new_doc[$key] = time();
137
-                                } else if ($my_model[$key]['_default']=="now" && ($my_model[$key]['_type']=="string")) {
137
+                                } else if ($my_model[$key]['_default'] == "now" && ($my_model[$key]['_type'] == "string")) {
138 138
                                     
139 139
                                     $new_doc[$key] = date("Y-m-d H:i:s");
140 140
                                 } else {
141
-                                    $new_doc[$key]=$my_model[$key]['_default'];
141
+                                    $new_doc[$key] = $my_model[$key]['_default'];
142 142
                                 }
143 143
                                 break;
144 144
                     
145 145
                             default:
146
-                                $new_doc[$key]=$my_model[$key]['_default'];
146
+                                $new_doc[$key] = $my_model[$key]['_default'];
147 147
                         }
148 148
                     } else {
149
-                        $new_doc[$key]=$my_model[$key]['_default'];
149
+                        $new_doc[$key] = $my_model[$key]['_default'];
150 150
                     }
151 151
                 }                
152 152
                 // If array has this key
@@ -154,7 +154,7 @@  discard block
 block discarded – undo
154 154
                     // If model definition stated this key's default value is not Null and has a wrong variable type, fix it . 
155 155
                     if ($my_model[$key]['_default'] !== null) {
156 156
                         if (ModelUtils::gettype($my_doc[$key]) != $my_model[$key]['_type'] && ModelUtils::gettype($my_doc[$key]) == "array") {
157
-                            $my_doc[$key]=$my_model[$key]['_default'];
157
+                            $my_doc[$key] = $my_model[$key]['_default'];
158 158
                         }
159 159
                         settype($my_doc[$key], $my_model[$key]['_type']);
160 160
                     }
@@ -164,7 +164,7 @@  discard block
 block discarded – undo
164 164
             }            
165 165
             // If one of the keys is not _type, this is a defined key, recursively get sub keys . 
166 166
             else {
167
-                if (! isset($my_doc[$key])) {
167
+                if (!isset($my_doc[$key])) {
168 168
                     $my_doc[$key] = "";
169 169
                 }
170 170
                 $new_doc[$key] = ModelUtils::setting_model_defaults($my_model[$key], $my_doc[$key]);
@@ -183,12 +183,12 @@  discard block
 block discarded – undo
183 183
      */
184 184
     public static function validate_doc_item($value, $my_model, $key)
185 185
     {
186
-        $type        = isset($my_model['_type'])         ? $my_model['_type']         : 'string';
187
-        $input_type  = isset($my_model['_input_type'])   ? $my_model['_input_type']   : 'string';
186
+        $type        = isset($my_model['_type']) ? $my_model['_type'] : 'string';
187
+        $input_type  = isset($my_model['_input_type']) ? $my_model['_input_type'] : 'string';
188 188
         $format      = isset($my_model['_input_format']) ? $my_model['_input_format'] : "";
189
-        $min_length  = isset($my_model['_min_length'])   ? $my_model['_min_length']   : null;
190
-        $max_length  = isset($my_model['_max_length'])   ? $my_model['_max_length']   : null;
191
-        $in_options  = isset($my_model['_in_options'])   ? $my_model['_in_options']   : null;
189
+        $min_length  = isset($my_model['_min_length']) ? $my_model['_min_length'] : null;
190
+        $max_length  = isset($my_model['_max_length']) ? $my_model['_max_length'] : null;
191
+        $in_options  = isset($my_model['_in_options']) ? $my_model['_in_options'] : null;
192 192
 
193 193
         if (ModelUtils::gettype($value) != $type) {
194 194
             return false;
@@ -198,35 +198,35 @@  discard block
 block discarded – undo
198 198
                 case 'mail':
199 199
                     $filter_check = filter_var($value, FILTER_VALIDATE_EMAIL);
200 200
                     if ($filter_check === false) {
201
-                        throw new \Exception("Error for value '" . $value . "' for '" . $key . "' couldn't pass the " . 
201
+                        throw new \Exception("Error for value '".$value."' for '".$key."' couldn't pass the ". 
202 202
                             "validation: INVALID_EMAIL_ADDRESS ");
203 203
                     }
204 204
                     break;
205 205
                 case 'bool':
206 206
                     $filter_check = filter_var($value, FILTER_VALIDATE_BOOLEAN);
207 207
                     if ($filter_check === false) {
208
-                        throw new \Exception("Error for value '" . $value . "' for '" . $key . "' couldn't pass the " . 
208
+                        throw new \Exception("Error for value '".$value."' for '".$key."' couldn't pass the ". 
209 209
                             "validation: INVALID_BOOLEAN_VALUE ");
210 210
                     }
211 211
                     break;
212 212
                 case 'url':
213 213
                     $filter_check = filter_var($value, FILTER_VALIDATE_URL);
214 214
                     if ($filter_check === false) {
215
-                        throw new \Exception("Error for value '" . $value . "' for '" . $key . "' couldn't pass the " . 
215
+                        throw new \Exception("Error for value '".$value."' for '".$key."' couldn't pass the ". 
216 216
                             "validation: INVALID_URL ");
217 217
                     }
218 218
                     break;
219 219
                 case 'ip':
220 220
                     $filter_check = filter_var($value, FILTER_VALIDATE_IP);
221 221
                     if ($filter_check === false) {
222
-                        throw new \Exception("Error for value '" . $value . "' for '" . $key . "' couldn't pass the " . 
222
+                        throw new \Exception("Error for value '".$value."' for '".$key."' couldn't pass the ". 
223 223
                             "validation: INVALID_IP_ADDRESS ");
224 224
                     }
225 225
                     break;
226 226
                 case 'mac_address':
227 227
                     $filter_check = filter_var($value, FILTER_VALIDATE_MAC);
228 228
                     if ($filter_check === false) {
229
-                        throw new \Exception("Error for value '" . $value . "' for '" . $key . "' couldn't pass the " . 
229
+                        throw new \Exception("Error for value '".$value."' for '".$key."' couldn't pass the ". 
230 230
                             "validation: INVALID_MAC_ADDRESS ");
231 231
                     }
232 232
                     break;
@@ -234,7 +234,7 @@  discard block
 block discarded – undo
234 234
                     $regex        = "/^[0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])$/";
235 235
                     $filter_check = filter_var($value, FILTER_VALIDATE_REGEXP, array("options"=>array("regexp"=> $regex)));
236 236
                     if ($filter_check === false) {
237
-                        throw new \Exception("Error for value '" . $value . "' for '" . $key . "' couldn't pass the " . 
237
+                        throw new \Exception("Error for value '".$value."' for '".$key."' couldn't pass the ". 
238 238
                             "validation: INVALID_FORMAT ");
239 239
                     }
240 240
                     break;
@@ -242,7 +242,7 @@  discard block
 block discarded – undo
242 242
                     $regex        = "/^([01]?[0-9]|2[0-3]):[0-5][0-9](:[0-5][0-9])$/";
243 243
                     $filter_check = filter_var($value, FILTER_VALIDATE_REGEXP, array("options"=>array("regexp"=> $regex)));
244 244
                     if ($filter_check === false) {
245
-                        throw new \Exception("Error for value '" . $value . "' for '" . $key . "' couldn't pass the " . 
245
+                        throw new \Exception("Error for value '".$value."' for '".$key."' couldn't pass the ". 
246 246
                             "validation: INVALID_FORMAT ");
247 247
                     }
248 248
                     break;
@@ -250,14 +250,14 @@  discard block
 block discarded – undo
250 250
                     $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])$/";
251 251
                     $filter_check = filter_var($value, FILTER_VALIDATE_REGEXP, array("options"=>array("regexp"=> $regex)));
252 252
                     if ($filter_check === false) {
253
-                        throw new \Exception("Error for value '" . $value . "' for '" . $key . "' couldn't pass the validation: INVALID_FORMAT ");
253
+                        throw new \Exception("Error for value '".$value."' for '".$key."' couldn't pass the validation: INVALID_FORMAT ");
254 254
                     }
255 255
                     break;
256 256
                 case 'regex':
257
-                    $regex        = "/^" . $format . "$/";
257
+                    $regex        = "/^".$format."$/";
258 258
                     $filter_check = filter_var($value, FILTER_VALIDATE_REGEXP, array("options"=>array("regexp"=> $regex)));
259 259
                     if ($filter_check === false) {
260
-                        throw new \Exception("Error for value '" . $value . "' for '" . $key . "' couldn't pass the " . 
260
+                        throw new \Exception("Error for value '".$value."' for '".$key."' couldn't pass the ". 
261 261
                             "validation: INVALID_FORMAT ");
262 262
                     }
263 263
                     break;
@@ -267,37 +267,37 @@  discard block
 block discarded – undo
267 267
             case 'integer':
268 268
             case 'float':
269 269
                 if ($min_length !== null) {
270
-                    if ($value < $min_length) {
271
-                        throw new \Exception("Error for value '" . $value . "' for '" . $key . "' couldn't pass the " . 
272
-                            "validation: Must be bigger than " . $min_length . "  ");
270
+                    if ($value<$min_length) {
271
+                        throw new \Exception("Error for value '".$value."' for '".$key."' couldn't pass the ". 
272
+                            "validation: Must be bigger than ".$min_length."  ");
273 273
                     }
274 274
                 }
275 275
                 if ($max_length !== null) {
276
-                    if ($value > $max_length) {
277
-                        throw new \Exception("Error for value '" . $value . "' for '" . $key . "' couldn't pass the " . 
278
-                            "validation: Must be smallerr than " . $max_length . "  ");
276
+                    if ($value>$max_length) {
277
+                        throw new \Exception("Error for value '".$value."' for '".$key."' couldn't pass the ". 
278
+                            "validation: Must be smallerr than ".$max_length."  ");
279 279
                     }
280 280
                 }
281 281
                 break;
282 282
             default:
283 283
                 if ($max_length !== null) {
284
-                    if (strlen($value) > $max_length) {
285
-                        throw new \Exception("Error for value '" . $value . "' for '" . $key . "' couldn't pass the " . 
286
-                            "validation: It's length must be smaller than " . $max_length . "  ");
284
+                    if (strlen($value)>$max_length) {
285
+                        throw new \Exception("Error for value '".$value."' for '".$key."' couldn't pass the ". 
286
+                            "validation: It's length must be smaller than ".$max_length."  ");
287 287
                     }
288 288
                 }
289 289
                 if ($min_length !== null) {
290
-                    if (strlen($value) < $min_length) {
291
-                        throw new \Exception("Error for value '" . $value . "' for '" . $key . "' couldn't pass the " . 
292
-                            "validation: It's length must be longer than " . $min_length . "  ");
290
+                    if (strlen($value)<$min_length) {
291
+                        throw new \Exception("Error for value '".$value."' for '".$key."' couldn't pass the ". 
292
+                            "validation: It's length must be longer than ".$min_length."  ");
293 293
                     }
294 294
                 }
295 295
                 break;
296 296
         }
297 297
         if ($in_options !== null) {
298 298
             if (!in_array($value, $in_options)) {
299
-                throw new \Exception("Error for value '" . $value . "' for '" . $key . "' couldn't pass the validation: " . 
300
-                    "It's length must be one of the these values: " . implode(", ", $in_options) . "  ");
299
+                throw new \Exception("Error for value '".$value."' for '".$key."' couldn't pass the validation: ". 
300
+                    "It's length must be one of the these values: ".implode(", ", $in_options)."  ");
301 301
             }
302 302
         }
303 303
 
@@ -312,11 +312,11 @@  discard block
 block discarded – undo
312 312
      */
313 313
     public static function sanitize_doc_item($value, $my_model)
314 314
     {
315
-        $type        = isset($my_model['_type'])         ? $my_model['_type']         : 'string';
316
-        $input_type  = isset($my_model['_input_type'])   ? $my_model['_input_type']   : null;
317
-        $min_length  = isset($my_model['_min_length'])   ? $my_model['_min_length']   : null;
318
-        $max_length  = isset($my_model['_max_length'])   ? $my_model['_max_length']   : null;
319
-        $in_options  = isset($my_model['_in_options'])   ? $my_model['_in_options']   : null;
315
+        $type        = isset($my_model['_type']) ? $my_model['_type'] : 'string';
316
+        $input_type  = isset($my_model['_input_type']) ? $my_model['_input_type'] : null;
317
+        $min_length  = isset($my_model['_min_length']) ? $my_model['_min_length'] : null;
318
+        $max_length  = isset($my_model['_max_length']) ? $my_model['_max_length'] : null;
319
+        $in_options  = isset($my_model['_in_options']) ? $my_model['_in_options'] : null;
320 320
 
321 321
         if ($input_type !== null) {
322 322
             switch ($input_type) {
@@ -335,19 +335,19 @@  discard block
 block discarded – undo
335 335
             case 'integer':
336 336
             case 'float':
337 337
                 if ($min_length !== null) {
338
-                    if ($value < $min_length) {
338
+                    if ($value<$min_length) {
339 339
                         $value = $min_length;
340 340
                     }
341 341
                 }
342 342
                 if ($max_length !== null) {
343
-                    if ($value > $max_length) {
343
+                    if ($value>$max_length) {
344 344
                         $value = $max_length;
345 345
                     }
346 346
                 }
347 347
                 break;
348 348
             default:
349 349
                 if ($max_length !== null) {
350
-                    if (strlen($value) > $max_length) {
350
+                    if (strlen($value)>$max_length) {
351 351
                         $value = substr($value, 0, $max_length);
352 352
                     }
353 353
                 }
Please login to merge, or discard this patch.