@@ -94,7 +94,7 @@ discard block |
||
94 | 94 | } |
95 | 95 | } |
96 | 96 | elseif (ModelUtils::gettype($my_doc[$key]) == "array" && $my_model[$key]['_type'] !="array") { |
97 | - $my_doc[$key]=$my_model[$key]['_default']; |
|
97 | + $my_doc[$key]=$my_model[$key]['_default']; |
|
98 | 98 | } |
99 | 99 | // If array[key] is not an array and not has same variable type that stated in the model definition. |
100 | 100 | else { |
@@ -122,47 +122,47 @@ discard block |
||
122 | 122 | // If array does not have this key, set the default value. |
123 | 123 | if (! isset($my_doc[$key])) { |
124 | 124 | if(isset($my_model[$key]['_input_type'])){ |
125 | - switch ($my_model[$key]['_input_type']){ |
|
125 | + switch ($my_model[$key]['_input_type']){ |
|
126 | 126 | case 'uid': |
127 | 127 | $shortid = ShortId::create(); |
128 | 128 | $new_doc[$key]=$shortid->generate(); |
129 | 129 | break; |
130 | - case 'date': |
|
131 | - if($my_model[$key]['_default']=='today'){ |
|
132 | - $new_doc[$key] = date("Y-m-d"); |
|
133 | - } |
|
134 | - else{ |
|
135 | - $new_doc[$key]=$my_model[$key]['_default']; |
|
136 | - } |
|
137 | - break; |
|
138 | - case 'timestamp': |
|
139 | - if(($my_model[$key]['_default']=="now") && ($my_model[$key]['_type'] == "integer")){ |
|
140 | - $new_doc[$key] = time(); |
|
141 | - } |
|
142 | - else if($my_model[$key]['_default']=="now" && ($my_model[$key]['_type']=="string")){ |
|
130 | + case 'date': |
|
131 | + if($my_model[$key]['_default']=='today'){ |
|
132 | + $new_doc[$key] = date("Y-m-d"); |
|
133 | + } |
|
134 | + else{ |
|
135 | + $new_doc[$key]=$my_model[$key]['_default']; |
|
136 | + } |
|
137 | + break; |
|
138 | + case 'timestamp': |
|
139 | + if(($my_model[$key]['_default']=="now") && ($my_model[$key]['_type'] == "integer")){ |
|
140 | + $new_doc[$key] = time(); |
|
141 | + } |
|
142 | + else if($my_model[$key]['_default']=="now" && ($my_model[$key]['_type']=="string")){ |
|
143 | 143 | |
144 | - $new_doc[$key] = date("Y-m-d H:i:s"); |
|
145 | - } |
|
146 | - else{ |
|
147 | - $new_doc[$key]=$my_model[$key]['_default']; |
|
148 | - } |
|
149 | - break; |
|
144 | + $new_doc[$key] = date("Y-m-d H:i:s"); |
|
145 | + } |
|
146 | + else{ |
|
147 | + $new_doc[$key]=$my_model[$key]['_default']; |
|
148 | + } |
|
149 | + break; |
|
150 | 150 | |
151 | - default: |
|
152 | - $new_doc[$key]=$my_model[$key]['_default']; |
|
153 | - } |
|
154 | - } |
|
155 | - else{ |
|
156 | - $new_doc[$key]=$my_model[$key]['_default']; |
|
157 | - } |
|
151 | + default: |
|
152 | + $new_doc[$key]=$my_model[$key]['_default']; |
|
153 | + } |
|
154 | + } |
|
155 | + else{ |
|
156 | + $new_doc[$key]=$my_model[$key]['_default']; |
|
157 | + } |
|
158 | 158 | } |
159 | 159 | // If array has this key |
160 | 160 | else { |
161 | 161 | // If model definition stated this key's default value is not Null and has a wrong variable type, fix it. |
162 | 162 | if ($my_model[$key]['_default'] !== null) { |
163 | - if(ModelUtils::gettype($my_doc[$key]) != $my_model[$key]['_type'] && ModelUtils::gettype($my_doc[$key]) == "array"){ |
|
164 | - $my_doc[$key]=$my_model[$key]['_default']; |
|
165 | - } |
|
163 | + if(ModelUtils::gettype($my_doc[$key]) != $my_model[$key]['_type'] && ModelUtils::gettype($my_doc[$key]) == "array"){ |
|
164 | + $my_doc[$key]=$my_model[$key]['_default']; |
|
165 | + } |
|
166 | 166 | settype($my_doc[$key], $my_model[$key]['_type']); |
167 | 167 | } |
168 | 168 | $new_doc[$key] = $my_doc[$key]; |
@@ -29,16 +29,16 @@ discard block |
||
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,15 +51,15 @@ discard block |
||
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 | if ($my_key !== null) { |
62 | - $v_key = $my_key . "." . $key; |
|
62 | + $v_key = $my_key.".".$key; |
|
63 | 63 | } else { |
64 | 64 | $v_key = $key; |
65 | 65 | } |
@@ -82,7 +82,7 @@ discard block |
||
82 | 82 | $my_keys = array_keys($my_doc); |
83 | 83 | foreach ($my_keys as $key) { |
84 | 84 | // If array has a key that is not presented in the model definition, unset it. |
85 | - if (! isset($my_model[$key])) { |
|
85 | + if (!isset($my_model[$key])) { |
|
86 | 86 | unset($my_doc[$key]); |
87 | 87 | } |
88 | 88 | // If array[$key] is again an array, recursively fit this array too. |
@@ -93,8 +93,8 @@ discard block |
||
93 | 93 | return $my_doc[$key]; |
94 | 94 | } |
95 | 95 | } |
96 | - elseif (ModelUtils::gettype($my_doc[$key]) == "array" && $my_model[$key]['_type'] !="array") { |
|
97 | - $my_doc[$key]=$my_model[$key]['_default']; |
|
96 | + elseif (ModelUtils::gettype($my_doc[$key]) == "array" && $my_model[$key]['_type'] != "array") { |
|
97 | + $my_doc[$key] = $my_model[$key]['_default']; |
|
98 | 98 | } |
99 | 99 | // If array[key] is not an array and not has same variable type that stated in the model definition. |
100 | 100 | else { |
@@ -114,54 +114,54 @@ discard block |
||
114 | 114 | public static function setting_model_defaults($my_model, $my_doc) |
115 | 115 | { |
116 | 116 | $my_keys = array_keys($my_model); |
117 | - $new_doc =[ ]; |
|
117 | + $new_doc = []; |
|
118 | 118 | foreach ($my_keys as $key) { |
119 | 119 | $item_keys = array_keys($my_model[$key]); |
120 | 120 | // If one of the keys of $my_model[$key] is _type this is a definition, not a defined key |
121 | 121 | if (in_array("_type", $item_keys)) { |
122 | 122 | // If array does not have this key, set the default value. |
123 | - if (! isset($my_doc[$key])) { |
|
124 | - if(isset($my_model[$key]['_input_type'])){ |
|
125 | - switch ($my_model[$key]['_input_type']){ |
|
123 | + if (!isset($my_doc[$key])) { |
|
124 | + if (isset($my_model[$key]['_input_type'])) { |
|
125 | + switch ($my_model[$key]['_input_type']) { |
|
126 | 126 | case 'uid': |
127 | 127 | $shortid = ShortId::create(); |
128 | - $new_doc[$key]=$shortid->generate(); |
|
128 | + $new_doc[$key] = $shortid->generate(); |
|
129 | 129 | break; |
130 | 130 | case 'date': |
131 | - if($my_model[$key]['_default']=='today'){ |
|
131 | + if ($my_model[$key]['_default'] == 'today') { |
|
132 | 132 | $new_doc[$key] = date("Y-m-d"); |
133 | 133 | } |
134 | - else{ |
|
135 | - $new_doc[$key]=$my_model[$key]['_default']; |
|
134 | + else { |
|
135 | + $new_doc[$key] = $my_model[$key]['_default']; |
|
136 | 136 | } |
137 | 137 | break; |
138 | 138 | case 'timestamp': |
139 | - if(($my_model[$key]['_default']=="now") && ($my_model[$key]['_type'] == "integer")){ |
|
139 | + if (($my_model[$key]['_default'] == "now") && ($my_model[$key]['_type'] == "integer")) { |
|
140 | 140 | $new_doc[$key] = time(); |
141 | 141 | } |
142 | - else if($my_model[$key]['_default']=="now" && ($my_model[$key]['_type']=="string")){ |
|
142 | + else if ($my_model[$key]['_default'] == "now" && ($my_model[$key]['_type'] == "string")) { |
|
143 | 143 | |
144 | 144 | $new_doc[$key] = date("Y-m-d H:i:s"); |
145 | 145 | } |
146 | - else{ |
|
147 | - $new_doc[$key]=$my_model[$key]['_default']; |
|
146 | + else { |
|
147 | + $new_doc[$key] = $my_model[$key]['_default']; |
|
148 | 148 | } |
149 | 149 | break; |
150 | 150 | |
151 | 151 | default: |
152 | - $new_doc[$key]=$my_model[$key]['_default']; |
|
152 | + $new_doc[$key] = $my_model[$key]['_default']; |
|
153 | 153 | } |
154 | 154 | } |
155 | - else{ |
|
156 | - $new_doc[$key]=$my_model[$key]['_default']; |
|
155 | + else { |
|
156 | + $new_doc[$key] = $my_model[$key]['_default']; |
|
157 | 157 | } |
158 | 158 | } |
159 | 159 | // If array has this key |
160 | 160 | else { |
161 | 161 | // If model definition stated this key's default value is not Null and has a wrong variable type, fix it. |
162 | 162 | if ($my_model[$key]['_default'] !== null) { |
163 | - if(ModelUtils::gettype($my_doc[$key]) != $my_model[$key]['_type'] && ModelUtils::gettype($my_doc[$key]) == "array"){ |
|
164 | - $my_doc[$key]=$my_model[$key]['_default']; |
|
163 | + if (ModelUtils::gettype($my_doc[$key]) != $my_model[$key]['_type'] && ModelUtils::gettype($my_doc[$key]) == "array") { |
|
164 | + $my_doc[$key] = $my_model[$key]['_default']; |
|
165 | 165 | } |
166 | 166 | settype($my_doc[$key], $my_model[$key]['_type']); |
167 | 167 | } |
@@ -171,7 +171,7 @@ discard block |
||
171 | 171 | } |
172 | 172 | // If one of the keys is not _type, this is a defined key, recursively get sub keys. |
173 | 173 | else { |
174 | - if (! isset($my_doc[$key])) { |
|
174 | + if (!isset($my_doc[$key])) { |
|
175 | 175 | $my_doc[$key] = ""; |
176 | 176 | } |
177 | 177 | $new_doc[$key] = ModelUtils::setting_model_defaults($my_model[$key], $my_doc[$key]); |
@@ -190,12 +190,12 @@ discard block |
||
190 | 190 | */ |
191 | 191 | public static function validate_doc_item($value, $my_model, $key) |
192 | 192 | { |
193 | - $type = isset($my_model['_type']) ? $my_model['_type'] : 'string'; |
|
194 | - $input_type = isset($my_model['_input_type']) ? $my_model['_input_type'] : 'string'; |
|
193 | + $type = isset($my_model['_type']) ? $my_model['_type'] : 'string'; |
|
194 | + $input_type = isset($my_model['_input_type']) ? $my_model['_input_type'] : 'string'; |
|
195 | 195 | $format = isset($my_model['_input_format']) ? $my_model['_input_format'] : ""; |
196 | - $min_length = isset($my_model['_min_length']) ? $my_model['_min_length'] : null; |
|
197 | - $max_length = isset($my_model['_max_length']) ? $my_model['_max_length'] : null; |
|
198 | - $in_options = isset($my_model['_in_options']) ? $my_model['_in_options'] : null; |
|
196 | + $min_length = isset($my_model['_min_length']) ? $my_model['_min_length'] : null; |
|
197 | + $max_length = isset($my_model['_max_length']) ? $my_model['_max_length'] : null; |
|
198 | + $in_options = isset($my_model['_in_options']) ? $my_model['_in_options'] : null; |
|
199 | 199 | |
200 | 200 | if (ModelUtils::gettype($value) != $type) { |
201 | 201 | return false; |
@@ -206,21 +206,21 @@ discard block |
||
206 | 206 | case 'mail': |
207 | 207 | $filter_check = filter_var($value, FILTER_VALIDATE_EMAIL); |
208 | 208 | if ($filter_check === false) { |
209 | - throw new \Exception("Error for value '" . $value . "' for '".$key."' couldn't pass the ". |
|
209 | + throw new \Exception("Error for value '".$value."' for '".$key."' couldn't pass the ". |
|
210 | 210 | "validation: INVALID_EMAIL_ADDRESS "); |
211 | 211 | } |
212 | 212 | break; |
213 | 213 | case 'bool': |
214 | 214 | $filter_check = filter_var($value, FILTER_VALIDATE_BOOLEAN); |
215 | 215 | if ($filter_check === false) { |
216 | - throw new \Exception("Error for value '" . $value . "' for '".$key."' couldn't pass the ". |
|
216 | + throw new \Exception("Error for value '".$value."' for '".$key."' couldn't pass the ". |
|
217 | 217 | "validation: INVALID_BOOLEAN_VALUE "); |
218 | 218 | } |
219 | 219 | break; |
220 | 220 | case 'url': |
221 | 221 | $filter_check = filter_var($value, FILTER_VALIDATE_URL); |
222 | 222 | if ($filter_check === false) { |
223 | - throw new \Exception("Error for value '" . $value . "' for '".$key."' couldn't pass the ". |
|
223 | + throw new \Exception("Error for value '".$value."' for '".$key."' couldn't pass the ". |
|
224 | 224 | "validation: INVALID_URL "); |
225 | 225 | } |
226 | 226 | break; |
@@ -230,14 +230,14 @@ discard block |
||
230 | 230 | case 'ip': |
231 | 231 | $filter_check = filter_var($value, FILTER_VALIDATE_IP); |
232 | 232 | if ($filter_check === false) { |
233 | - throw new \Exception("Error for value '" . $value . "' for '".$key."' couldn't pass the ". |
|
233 | + throw new \Exception("Error for value '".$value."' for '".$key."' couldn't pass the ". |
|
234 | 234 | "validation: INVALID_IP_ADDRESS "); |
235 | 235 | } |
236 | 236 | break; |
237 | 237 | case 'mac_address': |
238 | 238 | $filter_check = filter_var($value, FILTER_VALIDATE_MAC); |
239 | 239 | if ($filter_check === false) { |
240 | - throw new \Exception("Error for value '" . $value . "' for '".$key."' couldn't pass the ". |
|
240 | + throw new \Exception("Error for value '".$value."' for '".$key."' couldn't pass the ". |
|
241 | 241 | "validation: INVALID_MAC_ADDRESS "); |
242 | 242 | } |
243 | 243 | break; |
@@ -245,7 +245,7 @@ discard block |
||
245 | 245 | $regex = "/^[0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])$/"; |
246 | 246 | $filter_check = filter_var($value, FILTER_VALIDATE_REGEXP, array("options"=>array("regexp"=> $regex))); |
247 | 247 | if ($filter_check === false) { |
248 | - throw new \Exception("Error for value '" . $value . "' for '".$key."' couldn't pass the ". |
|
248 | + throw new \Exception("Error for value '".$value."' for '".$key."' couldn't pass the ". |
|
249 | 249 | "validation: INVALID_FORMAT "); |
250 | 250 | } |
251 | 251 | break; |
@@ -253,7 +253,7 @@ discard block |
||
253 | 253 | $regex = "/^([01]?[0-9]|2[0-3]):[0-5][0-9](:[0-5][0-9])$/"; |
254 | 254 | $filter_check = filter_var($value, FILTER_VALIDATE_REGEXP, array("options"=>array("regexp"=> $regex))); |
255 | 255 | if ($filter_check === false) { |
256 | - throw new \Exception("Error for value '" . $value . "' for '".$key."' couldn't pass the ". |
|
256 | + throw new \Exception("Error for value '".$value."' for '".$key."' couldn't pass the ". |
|
257 | 257 | "validation: INVALID_FORMAT "); |
258 | 258 | } |
259 | 259 | break; |
@@ -261,14 +261,14 @@ discard block |
||
261 | 261 | $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])$/"; |
262 | 262 | $filter_check = filter_var($value, FILTER_VALIDATE_REGEXP, array("options"=>array("regexp"=> $regex))); |
263 | 263 | if ($filter_check === false) { |
264 | - throw new \Exception("Error for value '" . $value . "' for '".$key."' couldn't pass the validation: INVALID_FORMAT "); |
|
264 | + throw new \Exception("Error for value '".$value."' for '".$key."' couldn't pass the validation: INVALID_FORMAT "); |
|
265 | 265 | } |
266 | 266 | break; |
267 | 267 | case 'regex': |
268 | 268 | $regex = "/^".$format."$/"; |
269 | 269 | $filter_check = filter_var($value, FILTER_VALIDATE_REGEXP, array("options"=>array("regexp"=> $regex))); |
270 | 270 | if ($filter_check === false) { |
271 | - throw new \Exception("Error for value '" . $value . "' for '".$key."' couldn't pass the ". |
|
271 | + throw new \Exception("Error for value '".$value."' for '".$key."' couldn't pass the ". |
|
272 | 272 | "validation: INVALID_FORMAT "); |
273 | 273 | } |
274 | 274 | break; |
@@ -278,28 +278,28 @@ discard block |
||
278 | 278 | case 'integer': |
279 | 279 | case 'float': |
280 | 280 | if ($min_length !== null) { |
281 | - if ($value < $min_length) { |
|
282 | - throw new \Exception("Error for value '" . $value . "' for '".$key."' couldn't pass the ". |
|
281 | + if ($value<$min_length) { |
|
282 | + throw new \Exception("Error for value '".$value."' for '".$key."' couldn't pass the ". |
|
283 | 283 | "validation: Must be bigger than ".$min_length." "); |
284 | 284 | } |
285 | 285 | } |
286 | 286 | if ($max_length !== null) { |
287 | - if ($value > $max_length) { |
|
288 | - throw new \Exception("Error for value '" . $value . "' for '".$key."' couldn't pass the ". |
|
287 | + if ($value>$max_length) { |
|
288 | + throw new \Exception("Error for value '".$value."' for '".$key."' couldn't pass the ". |
|
289 | 289 | "validation: Must be smallerr than ".$max_length." "); |
290 | 290 | } |
291 | 291 | } |
292 | 292 | break; |
293 | 293 | default: |
294 | 294 | if ($max_length !== null) { |
295 | - if (strlen($value) > $max_length) { |
|
296 | - throw new \Exception("Error for value '" . $value . "' for '".$key."' couldn't pass the ". |
|
295 | + if (strlen($value)>$max_length) { |
|
296 | + throw new \Exception("Error for value '".$value."' for '".$key."' couldn't pass the ". |
|
297 | 297 | "validation: It's length must be smaller than ".$max_length." "); |
298 | 298 | } |
299 | 299 | } |
300 | 300 | if ($min_length !== null) { |
301 | - if (strlen($value) < $min_length) { |
|
302 | - throw new \Exception("Error for value '" . $value . "' for '".$key."' couldn't pass the ". |
|
301 | + if (strlen($value)<$min_length) { |
|
302 | + throw new \Exception("Error for value '".$value."' for '".$key."' couldn't pass the ". |
|
303 | 303 | "validation: It's length must be longer than ".$min_length." "); |
304 | 304 | } |
305 | 305 | } |
@@ -307,7 +307,7 @@ discard block |
||
307 | 307 | } |
308 | 308 | if ($in_options !== null) { |
309 | 309 | if (!in_array($value, $in_options)) { |
310 | - throw new \Exception("Error for value '" . $value . "' for '".$key."' couldn't pass the validation: ". |
|
310 | + throw new \Exception("Error for value '".$value."' for '".$key."' couldn't pass the validation: ". |
|
311 | 311 | "It's length must be one of the these values: ".implode(",", $in_options)." "); |
312 | 312 | } |
313 | 313 | } |
@@ -323,20 +323,20 @@ discard block |
||
323 | 323 | */ |
324 | 324 | public static function sanitize_doc_item($value, $my_model) |
325 | 325 | { |
326 | - $type = isset($my_model['_type']) ? $my_model['_type'] : 'string'; |
|
327 | - $input_type = isset($my_model['_input_type']) ? $my_model['_input_type'] : null; |
|
326 | + $type = isset($my_model['_type']) ? $my_model['_type'] : 'string'; |
|
327 | + $input_type = isset($my_model['_input_type']) ? $my_model['_input_type'] : null; |
|
328 | 328 | $format = isset($my_model['_input_format']) ? $my_model['_input_format'] : ""; |
329 | - $min_length = isset($my_model['_min_length']) ? $my_model['_min_length'] : null; |
|
330 | - $max_length = isset($my_model['_max_length']) ? $my_model['_max_length'] : null; |
|
331 | - $in_options = isset($my_model['_in_options']) ? $my_model['_in_options'] : null; |
|
329 | + $min_length = isset($my_model['_min_length']) ? $my_model['_min_length'] : null; |
|
330 | + $max_length = isset($my_model['_max_length']) ? $my_model['_max_length'] : null; |
|
331 | + $in_options = isset($my_model['_in_options']) ? $my_model['_in_options'] : null; |
|
332 | 332 | |
333 | 333 | if ($input_type !== null) { |
334 | 334 | switch ($input_type) { |
335 | 335 | case 'mail': |
336 | - $value= filter_var($value, FILTER_SANITIZE_EMAIL); |
|
336 | + $value = filter_var($value, FILTER_SANITIZE_EMAIL); |
|
337 | 337 | break; |
338 | 338 | case 'timestamp': |
339 | - if ($value=='now') { |
|
339 | + if ($value == 'now') { |
|
340 | 340 | $value = time(); |
341 | 341 | } |
342 | 342 | break; |
@@ -381,19 +381,19 @@ discard block |
||
381 | 381 | case 'integer': |
382 | 382 | case 'float': |
383 | 383 | if ($min_length !== null) { |
384 | - if ($value < $min_length) { |
|
384 | + if ($value<$min_length) { |
|
385 | 385 | $value = $min_length; |
386 | 386 | } |
387 | 387 | } |
388 | 388 | if ($max_length !== null) { |
389 | - if ($value > $max_length) { |
|
389 | + if ($value>$max_length) { |
|
390 | 390 | $value = $max_length; |
391 | 391 | } |
392 | 392 | } |
393 | 393 | break; |
394 | 394 | default: |
395 | 395 | if ($max_length !== null) { |
396 | - if (strlen($value) > $max_length) { |
|
396 | + if (strlen($value)>$max_length) { |
|
397 | 397 | $value = substr($value, 0, $max_length); |
398 | 398 | } |
399 | 399 | } |
@@ -92,8 +92,7 @@ discard block |
||
92 | 92 | if (ModelUtils::gettype($my_doc[$key]) != "array") { |
93 | 93 | return $my_doc[$key]; |
94 | 94 | } |
95 | - } |
|
96 | - elseif (ModelUtils::gettype($my_doc[$key]) == "array" && $my_model[$key]['_type'] !="array") { |
|
95 | + } elseif (ModelUtils::gettype($my_doc[$key]) == "array" && $my_model[$key]['_type'] !="array") { |
|
97 | 96 | $my_doc[$key]=$my_model[$key]['_default']; |
98 | 97 | } |
99 | 98 | // If array[key] is not an array and not has same variable type that stated in the model definition. |
@@ -130,20 +129,17 @@ discard block |
||
130 | 129 | case 'date': |
131 | 130 | if($my_model[$key]['_default']=='today'){ |
132 | 131 | $new_doc[$key] = date("Y-m-d"); |
133 | - } |
|
134 | - else{ |
|
132 | + } else{ |
|
135 | 133 | $new_doc[$key]=$my_model[$key]['_default']; |
136 | 134 | } |
137 | 135 | break; |
138 | 136 | case 'timestamp': |
139 | 137 | if(($my_model[$key]['_default']=="now") && ($my_model[$key]['_type'] == "integer")){ |
140 | 138 | $new_doc[$key] = time(); |
141 | - } |
|
142 | - else if($my_model[$key]['_default']=="now" && ($my_model[$key]['_type']=="string")){ |
|
139 | + } else if($my_model[$key]['_default']=="now" && ($my_model[$key]['_type']=="string")){ |
|
143 | 140 | |
144 | 141 | $new_doc[$key] = date("Y-m-d H:i:s"); |
145 | - } |
|
146 | - else{ |
|
142 | + } else{ |
|
147 | 143 | $new_doc[$key]=$my_model[$key]['_default']; |
148 | 144 | } |
149 | 145 | break; |
@@ -151,8 +147,7 @@ discard block |
||
151 | 147 | default: |
152 | 148 | $new_doc[$key]=$my_model[$key]['_default']; |
153 | 149 | } |
154 | - } |
|
155 | - else{ |
|
150 | + } else{ |
|
156 | 151 | $new_doc[$key]=$my_model[$key]['_default']; |
157 | 152 | } |
158 | 153 | } |
@@ -2,57 +2,57 @@ |
||
2 | 2 | |
3 | 3 | namespace ModelUtils; |
4 | 4 | |
5 | -class Model{ |
|
5 | +class Model { |
|
6 | 6 | |
7 | 7 | public $config = []; |
8 | 8 | public $data_file = null; |
9 | 9 | |
10 | - public function __construct(){ |
|
10 | + public function __construct() { |
|
11 | 11 | $this->config = json_decode(trim($this->config_str), true); |
12 | 12 | } |
13 | 13 | |
14 | - public function validate($doc){ |
|
14 | + public function validate($doc) { |
|
15 | 15 | return ModelUtils::validate_doc($this->config, $doc); |
16 | 16 | } |
17 | 17 | |
18 | - public function set_defaults($doc){ |
|
18 | + public function set_defaults($doc) { |
|
19 | 19 | return ModelUtils::setting_model_defaults($this->config, $doc); |
20 | 20 | } |
21 | 21 | |
22 | 22 | |
23 | - public function fit_doc($doc){ |
|
23 | + public function fit_doc($doc) { |
|
24 | 24 | return ModelUtils::fit_doc_to_model($this->config, $doc); |
25 | 25 | } |
26 | 26 | |
27 | - public function install($db){ |
|
27 | + public function install($db) { |
|
28 | 28 | |
29 | 29 | $db->drop($this->collection_name, $this->config); |
30 | 30 | $db->create($this->collection_name, $this->config); |
31 | - $indexes =[]; |
|
32 | - foreach ($this->config as $field=>$fconfig){ |
|
33 | - if($fconfig['_index']===true){ |
|
34 | - $index=['key'=>[$field=>1]]; |
|
35 | - if(isset($fconfig["_index_type"])){ |
|
36 | - switch ($fconfig["_index_type"]){ |
|
31 | + $indexes = []; |
|
32 | + foreach ($this->config as $field=>$fconfig) { |
|
33 | + if ($fconfig['_index'] === true) { |
|
34 | + $index = ['key'=>[$field=>1]]; |
|
35 | + if (isset($fconfig["_index_type"])) { |
|
36 | + switch ($fconfig["_index_type"]) { |
|
37 | 37 | case 'unique': |
38 | - $index['unique']=true; |
|
38 | + $index['unique'] = true; |
|
39 | 39 | break; |
40 | 40 | } |
41 | 41 | } |
42 | - $indexes[]=$index; |
|
42 | + $indexes[] = $index; |
|
43 | 43 | } |
44 | 44 | } |
45 | - if($this->data_file !== null){ |
|
46 | - if(file_exists(BASE_DIR.$this->data_file)){ |
|
47 | - $data = json_decode(file_get_contents(BASE_DIR.$this->data_file),true); |
|
48 | - foreach ($data as $item){ |
|
45 | + if ($this->data_file !== null) { |
|
46 | + if (file_exists(BASE_DIR.$this->data_file)) { |
|
47 | + $data = json_decode(file_get_contents(BASE_DIR.$this->data_file), true); |
|
48 | + foreach ($data as $item) { |
|
49 | 49 | $item = ModelUtils::setting_model_defaults($this->config, $item); |
50 | 50 | $doc = ModelUtils::validate_doc($this->config, $item); |
51 | - $db->insert($this->collection_name,$doc); |
|
51 | + $db->insert($this->collection_name, $doc); |
|
52 | 52 | } |
53 | 53 | } |
54 | 54 | } |
55 | - if(count($indexes)>0){ |
|
55 | + if (count($indexes)>0) { |
|
56 | 56 | $db->create_indexes($this->collection_name, $indexes); |
57 | 57 | } |
58 | 58 | } |