@@ -72,8 +72,9 @@ discard block |
||
72 | 72 | |
73 | 73 | $what = (array) $what; |
74 | 74 | |
75 | - if( empty($what) ) |
|
76 | - $what = array_keys($defaults); |
|
75 | + if( empty($what) ) { |
|
76 | + $what = array_keys($defaults); |
|
77 | + } |
|
77 | 78 | |
78 | 79 | foreach( $what as $k ) { |
79 | 80 | $this->$k = $defaults[$k]; |
@@ -194,8 +195,9 @@ discard block |
||
194 | 195 | |
195 | 196 | $field = trim($field); |
196 | 197 | |
197 | - if( empty($field) ) |
|
198 | - throw new \InvalidArgumentException('No field specified'); |
|
198 | + if( empty($field) ) { |
|
199 | + throw new \InvalidArgumentException('No field specified'); |
|
200 | + } |
|
199 | 201 | |
200 | 202 | $modifier = substr($field, 0, 1); |
201 | 203 | |
@@ -240,8 +242,9 @@ discard block |
||
240 | 242 | |
241 | 243 | foreach( $this->criteria as $column => $criteria ) { |
242 | 244 | |
243 | - if( !$column = $this->getColumnName($columns, $column, $alias) ) |
|
244 | - continue; |
|
245 | + if( !$column = $this->getColumnName($columns, $column, $alias) ) { |
|
246 | + continue; |
|
247 | + } |
|
245 | 248 | |
246 | 249 | foreach( $criteria as $operator => $value ) { |
247 | 250 | $query->where($column, $operator, $value); |
@@ -251,16 +254,19 @@ discard block |
||
251 | 254 | |
252 | 255 | foreach( $this->getOrder() as $column => $ascending ) { |
253 | 256 | $column = $this->getColumnName($columns, $column, $alias); |
254 | - if( !$column ) |
|
255 | - continue; |
|
257 | + if( !$column ) { |
|
258 | + continue; |
|
259 | + } |
|
256 | 260 | $query->orderBy($column, $ascending); |
257 | 261 | } |
258 | 262 | |
259 | - if( $this->offset ) |
|
260 | - $query->offset($this->offset); |
|
263 | + if( $this->offset ) { |
|
264 | + $query->offset($this->offset); |
|
265 | + } |
|
261 | 266 | |
262 | - if( $this->limit ) |
|
263 | - $query->limit($this->limit); |
|
267 | + if( $this->limit ) { |
|
268 | + $query->limit($this->limit); |
|
269 | + } |
|
264 | 270 | |
265 | 271 | return $query; |
266 | 272 | |
@@ -270,14 +276,13 @@ discard block |
||
270 | 276 | |
271 | 277 | $column = isset($columns[$column]) ? $columns[$column] : $column; |
272 | 278 | |
273 | - if( !$column ) |
|
274 | - return ''; |
|
275 | - |
|
276 | - elseif( strpos($column, '.') ) |
|
277 | - return $column; |
|
278 | - |
|
279 | - elseif( $alias ) |
|
280 | - return "{$alias}.{$column}"; |
|
279 | + if( !$column ) { |
|
280 | + return ''; |
|
281 | + } elseif( strpos($column, '.') ) { |
|
282 | + return $column; |
|
283 | + } elseif( $alias ) { |
|
284 | + return "{$alias}.{$column}"; |
|
285 | + } |
|
281 | 286 | |
282 | 287 | return $column; |
283 | 288 | |
@@ -294,8 +299,9 @@ discard block |
||
294 | 299 | |
295 | 300 | $field = trim($field); |
296 | 301 | |
297 | - if( !isset($this->criteria[$field]) ) |
|
298 | - $this->criteria[$field] = []; |
|
302 | + if( !isset($this->criteria[$field]) ) { |
|
303 | + $this->criteria[$field] = []; |
|
304 | + } |
|
299 | 305 | |
300 | 306 | $this->criteria[$field][$operator] = $value; |
301 | 307 |
@@ -39,13 +39,15 @@ discard block |
||
39 | 39 | |
40 | 40 | require $file; |
41 | 41 | |
42 | - if( !isset($config) || !is_array($config) ) |
|
43 | - throw new \LogicException('Invalid Configuration'); |
|
42 | + if( !isset($config) || !is_array($config) ) { |
|
43 | + throw new \LogicException('Invalid Configuration'); |
|
44 | + } |
|
44 | 45 | |
45 | - if( empty($key) ) |
|
46 | - $this->merge($config); |
|
47 | - else |
|
48 | - $this->set($key, $config); |
|
46 | + if( empty($key) ) { |
|
47 | + $this->merge($config); |
|
48 | + } else { |
|
49 | + $this->set($key, $config); |
|
50 | + } |
|
49 | 51 | |
50 | 52 | return $this; |
51 | 53 | |
@@ -81,8 +83,7 @@ discard block |
||
81 | 83 | $part = $parts[$i]; |
82 | 84 | if( !isset($context[$part]) && ($i < $count) ) { |
83 | 85 | $context[$part] = []; |
84 | - } |
|
85 | - elseif( $i == $count ) { |
|
86 | + } elseif( $i == $count ) { |
|
86 | 87 | $context[$part] = $value; |
87 | 88 | if( $parts[0] == 'php' ) { |
88 | 89 | ini_set($part, $value); |
@@ -60,8 +60,9 @@ discard block |
||
60 | 60 | |
61 | 61 | list($clean, $errors[$f]) = $field->validate($v); |
62 | 62 | |
63 | - if( !$errors[$f] ) |
|
64 | - $data[$f] = $clean; |
|
63 | + if( !$errors[$f] ) { |
|
64 | + $data[$f] = $clean; |
|
65 | + } |
|
65 | 66 | |
66 | 67 | } |
67 | 68 | |
@@ -108,13 +109,15 @@ discard block |
||
108 | 109 | $this->types['unique'] = []; |
109 | 110 | foreach( $this->fields as $field ) { |
110 | 111 | |
111 | - if( !isset($this->types[$field->type]) ) |
|
112 | - $this->types[$field->type] = []; |
|
112 | + if( !isset($this->types[$field->type]) ) { |
|
113 | + $this->types[$field->type] = []; |
|
114 | + } |
|
113 | 115 | |
114 | 116 | $this->types[$field->type][] = $field->name; |
115 | 117 | |
116 | - if( $field->isUnique() ) |
|
117 | - $this->types['unique'][] = $field->name; |
|
118 | + if( $field->isUnique() ) { |
|
119 | + $this->types['unique'][] = $field->name; |
|
120 | + } |
|
118 | 121 | |
119 | 122 | } |
120 | 123 | } |
@@ -22,8 +22,9 @@ discard block |
||
22 | 22 | |
23 | 23 | $new = !$this->contains($item); |
24 | 24 | |
25 | - if( $new ) |
|
26 | - $this->items[] = $item; |
|
25 | + if( $new ) { |
|
26 | + $this->items[] = $item; |
|
27 | + } |
|
27 | 28 | |
28 | 29 | return $new; |
29 | 30 | } |
@@ -32,8 +33,9 @@ discard block |
||
32 | 33 | |
33 | 34 | $key = array_search($item, $this->items); |
34 | 35 | |
35 | - if( $key !== false ) |
|
36 | - unset($this->items[$key]); |
|
36 | + if( $key !== false ) { |
|
37 | + unset($this->items[$key]); |
|
38 | + } |
|
37 | 39 | |
38 | 40 | return $key !== false; |
39 | 41 |
@@ -52,14 +52,15 @@ discard block |
||
52 | 52 | } |
53 | 53 | |
54 | 54 | public function __get( $key ) { |
55 | - if( $key == 'default' ) |
|
56 | - return $this->getDefault(); |
|
57 | - elseif( isset($this->rules[$key]) ) |
|
58 | - return $this->rules[$key]; |
|
59 | - elseif( isset($this->$key) ) |
|
60 | - return $this->$key; |
|
61 | - else |
|
62 | - return null; |
|
55 | + if( $key == 'default' ) { |
|
56 | + return $this->getDefault(); |
|
57 | + } elseif( isset($this->rules[$key]) ) { |
|
58 | + return $this->rules[$key]; |
|
59 | + } elseif( isset($this->$key) ) { |
|
60 | + return $this->$key; |
|
61 | + } else { |
|
62 | + return null; |
|
63 | + } |
|
63 | 64 | } |
64 | 65 | |
65 | 66 | public function __isset( $key ) { |
@@ -145,10 +146,11 @@ discard block |
||
145 | 146 | return preg_match('/0000-00-00/', $v) ? '' : $v; |
146 | 147 | |
147 | 148 | case Type::JSON: |
148 | - if( !$v ) |
|
149 | - $v = []; |
|
150 | - elseif( !is_array($v) && is_array($arr = json_decode($v, true)) ) |
|
151 | - $v = $arr; |
|
149 | + if( !$v ) { |
|
150 | + $v = []; |
|
151 | + } elseif( !is_array($v) && is_array($arr = json_decode($v, true)) ) { |
|
152 | + $v = $arr; |
|
153 | + } |
|
152 | 154 | return $v; |
153 | 155 | |
154 | 156 | default: |
@@ -159,18 +161,19 @@ discard block |
||
159 | 161 | |
160 | 162 | public function validate( $v ) { |
161 | 163 | |
162 | - if( $this->required && Validator::isEmpty($v, $this->type) ) |
|
163 | - return [$v, Error::REQUIRED]; |
|
164 | - |
|
165 | - elseif( !$this->nullable && ($v === null) ) |
|
166 | - return [$v, Error::NULL]; |
|
164 | + if( $this->required && Validator::isEmpty($v, $this->type) ) { |
|
165 | + return [$v, Error::REQUIRED]; |
|
166 | + } elseif( !$this->nullable && ($v === null) ) { |
|
167 | + return [$v, Error::NULL]; |
|
168 | + } |
|
167 | 169 | |
168 | 170 | list($clean, $error) = $this->validateType($v, $this->type); |
169 | 171 | |
170 | - if( $error ) |
|
171 | - return [$v, $error]; |
|
172 | - else |
|
173 | - return $this->validateRules($clean); |
|
172 | + if( $error ) { |
|
173 | + return [$v, $error]; |
|
174 | + } else { |
|
175 | + return $this->validateRules($clean); |
|
176 | + } |
|
174 | 177 | |
175 | 178 | } |
176 | 179 | |
@@ -180,10 +183,11 @@ discard block |
||
180 | 183 | */ |
181 | 184 | protected function getDefault() { |
182 | 185 | $default = $this->default; |
183 | - if( $default instanceof \Closure ) |
|
184 | - return $default(); |
|
185 | - else |
|
186 | - return $this->cast($default); |
|
186 | + if( $default instanceof \Closure ) { |
|
187 | + return $default(); |
|
188 | + } else { |
|
189 | + return $this->cast($default); |
|
190 | + } |
|
187 | 191 | } |
188 | 192 | |
189 | 193 | protected function validateType( $v, $type ) { |
@@ -261,10 +265,11 @@ discard block |
||
261 | 265 | } |
262 | 266 | |
263 | 267 | // boolean fields will be null on error |
264 | - if( $type == Type::BOOLEAN ) |
|
265 | - $error = ($clean === null) ? Error::BOOLEAN : Error::NONE; |
|
266 | - elseif( $clean === false ) |
|
267 | - $error = Error::getTypeError($type); |
|
268 | + if( $type == Type::BOOLEAN ) { |
|
269 | + $error = ($clean === null) ? Error::BOOLEAN : Error::NONE; |
|
270 | + } elseif( $clean === false ) { |
|
271 | + $error = Error::getTypeError($type); |
|
272 | + } |
|
268 | 273 | |
269 | 274 | return [$clean, $error]; |
270 | 275 | |
@@ -272,17 +277,15 @@ discard block |
||
272 | 277 | |
273 | 278 | protected function validateRules( $v ) { |
274 | 279 | |
275 | - if( $error = $this->validateRange($v) ) |
|
276 | - return [$v, $error]; |
|
277 | - |
|
278 | - elseif( $error = $this->validateLength($v) ) |
|
279 | - return [$v, $error]; |
|
280 | - |
|
281 | - elseif( $error = $this->validateValues($v) ) |
|
282 | - return [$v, $error]; |
|
283 | - |
|
284 | - elseif( $error = $this->validateRegex($v) ) |
|
285 | - return [$v, $error]; |
|
280 | + if( $error = $this->validateRange($v) ) { |
|
281 | + return [$v, $error]; |
|
282 | + } elseif( $error = $this->validateLength($v) ) { |
|
283 | + return [$v, $error]; |
|
284 | + } elseif( $error = $this->validateValues($v) ) { |
|
285 | + return [$v, $error]; |
|
286 | + } elseif( $error = $this->validateRegex($v) ) { |
|
287 | + return [$v, $error]; |
|
288 | + } |
|
286 | 289 | |
287 | 290 | return [$v, $error]; |
288 | 291 | |
@@ -290,11 +293,13 @@ discard block |
||
290 | 293 | |
291 | 294 | protected function validateRange( $v ) { |
292 | 295 | |
293 | - if( isset($this->rules['min']) && $v && ($v < $this->rules['min']) ) |
|
294 | - return Error::MIN; |
|
296 | + if( isset($this->rules['min']) && $v && ($v < $this->rules['min']) ) { |
|
297 | + return Error::MIN; |
|
298 | + } |
|
295 | 299 | |
296 | - if( isset($this->rules['max']) && $v && ($v > $this->rules['max']) ) |
|
297 | - return Error::MAX; |
|
300 | + if( isset($this->rules['max']) && $v && ($v > $this->rules['max']) ) { |
|
301 | + return Error::MAX; |
|
302 | + } |
|
298 | 303 | |
299 | 304 | return Error::NONE; |
300 | 305 | |
@@ -302,11 +307,13 @@ discard block |
||
302 | 307 | |
303 | 308 | protected function validateLength( $v ) { |
304 | 309 | |
305 | - if( isset($this->rules['min_length']) && mb_strlen($v) < $this->rules['min_length'] ) |
|
306 | - return Error::TOO_SHORT; |
|
310 | + if( isset($this->rules['min_length']) && mb_strlen($v) < $this->rules['min_length'] ) { |
|
311 | + return Error::TOO_SHORT; |
|
312 | + } |
|
307 | 313 | |
308 | - if( isset($this->rules['max_length']) && mb_strlen($v) > $this->rules['max_length'] ) |
|
309 | - return Error::TOO_LONG; |
|
314 | + if( isset($this->rules['max_length']) && mb_strlen($v) > $this->rules['max_length'] ) { |
|
315 | + return Error::TOO_LONG; |
|
316 | + } |
|
310 | 317 | |
311 | 318 | return Error::NONE; |
312 | 319 | |
@@ -314,8 +321,9 @@ discard block |
||
314 | 321 | |
315 | 322 | protected function validateRegex( $v ) { |
316 | 323 | |
317 | - if( isset($this->rules['regex']) && !preg_match($this->rules['regex'], $v) ) |
|
318 | - return Error::REGEX; |
|
324 | + if( isset($this->rules['regex']) && !preg_match($this->rules['regex'], $v) ) { |
|
325 | + return Error::REGEX; |
|
326 | + } |
|
319 | 327 | |
320 | 328 | return Error::NONE; |
321 | 329 | |
@@ -323,8 +331,9 @@ discard block |
||
323 | 331 | |
324 | 332 | protected function validateValues( $v ) { |
325 | 333 | |
326 | - if( isset($this->rules['values']) && !in_array($v, $this->rules['values']) ) |
|
327 | - return Error::VALUE; |
|
334 | + if( isset($this->rules['values']) && !in_array($v, $this->rules['values']) ) { |
|
335 | + return Error::VALUE; |
|
336 | + } |
|
328 | 337 | |
329 | 338 | return Error::NONE; |
330 | 339 | |
@@ -347,19 +356,20 @@ discard block |
||
347 | 356 | if( $this->isObject() ) { |
348 | 357 | |
349 | 358 | // object fields must specify a class |
350 | - if( empty($rules['class']) ) |
|
351 | - throw new \LogicException("Missing class name for item: {$this->name}"); |
|
359 | + if( empty($rules['class']) ) { |
|
360 | + throw new \LogicException("Missing class name for item: {$this->name}"); |
|
361 | + } |
|
352 | 362 | |
353 | 363 | // object fields are nullable by default |
354 | 364 | $this->nullable = true; |
355 | 365 | $this->default = null; |
356 | 366 | |
357 | - } |
|
358 | - elseif( $this->isCollection() ) { |
|
367 | + } elseif( $this->isCollection() ) { |
|
359 | 368 | |
360 | 369 | // collection fields must specify a class |
361 | - if( empty($rules['class']) ) |
|
362 | - throw new \LogicException("Missing item class for collection: {$this->name}"); |
|
370 | + if( empty($rules['class']) ) { |
|
371 | + throw new \LogicException("Missing item class for collection: {$this->name}"); |
|
372 | + } |
|
363 | 373 | |
364 | 374 | $rules['container'] = empty($rules['container']) ? $container : $rules['container']; |
365 | 375 | |
@@ -368,10 +378,11 @@ discard block |
||
368 | 378 | |
369 | 379 | $container = $rules['container']; |
370 | 380 | |
371 | - if( $container == 'array' ) |
|
372 | - return []; |
|
373 | - else |
|
374 | - return new $container($rules['class']); |
|
381 | + if( $container == 'array' ) { |
|
382 | + return []; |
|
383 | + } else { |
|
384 | + return new $container($rules['class']); |
|
385 | + } |
|
375 | 386 | |
376 | 387 | }; |
377 | 388 |
@@ -28,8 +28,9 @@ discard block |
||
28 | 28 | |
29 | 29 | $name = 'validate'. substr($name, 2); |
30 | 30 | |
31 | - if( !method_exists(__CLASS__, $name) ) |
|
32 | - throw new \BadMethodCallException(sprintf("%s::%s()", get_called_class(), $name)); |
|
31 | + if( !method_exists(__CLASS__, $name) ) { |
|
32 | + throw new \BadMethodCallException(sprintf("%s::%s()", get_called_class(), $name)); |
|
33 | + } |
|
33 | 34 | |
34 | 35 | $failure = ($name == 'validateBoolean') ? null : false; |
35 | 36 | |
@@ -47,8 +48,9 @@ discard block |
||
47 | 48 | |
48 | 49 | public static function isEmpty( $v, $type = Type::TEXT ) { |
49 | 50 | |
50 | - if( !$v || (is_scalar($v) && !trim($v)) ) |
|
51 | - return true; |
|
51 | + if( !$v || (is_scalar($v) && !trim($v)) ) { |
|
52 | + return true; |
|
53 | + } |
|
52 | 54 | |
53 | 55 | switch( $type ) { |
54 | 56 | |
@@ -108,15 +110,17 @@ discard block |
||
108 | 110 | |
109 | 111 | public static function validateTimestamp( $v ) { |
110 | 112 | $ts = filter_var($v, FILTER_VALIDATE_INT); |
111 | - if( $ts === false ) |
|
112 | - $ts = strtotime(str_replace('@', ' ', $v)); |
|
113 | + if( $ts === false ) { |
|
114 | + $ts = strtotime(str_replace('@', ' ', $v)); |
|
115 | + } |
|
113 | 116 | return $ts; |
114 | 117 | } |
115 | 118 | |
116 | 119 | public static function validateDateTime( $v, $format = 'Y-m-d H:i:s' ) { |
117 | 120 | |
118 | - if( !trim($v) || preg_match('/0000-00-00/', $v) ) |
|
119 | - return ''; |
|
121 | + if( !trim($v) || preg_match('/0000-00-00/', $v) ) { |
|
122 | + return ''; |
|
123 | + } |
|
120 | 124 | |
121 | 125 | $ts = static::validateTimestamp($v); |
122 | 126 | |
@@ -144,8 +148,9 @@ discard block |
||
144 | 148 | */ |
145 | 149 | public static function validateIP( $v ) { |
146 | 150 | // if integer then convert to string |
147 | - if( $ip = filter_var($v, FILTER_VALIDATE_INT) ) |
|
148 | - $v = long2ip($ip); |
|
151 | + if( $ip = filter_var($v, FILTER_VALIDATE_INT) ) { |
|
152 | + $v = long2ip($ip); |
|
153 | + } |
|
149 | 154 | return filter_var($v, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4); |
150 | 155 | } |
151 | 156 | |
@@ -172,11 +177,13 @@ discard block |
||
172 | 177 | public static function validateJSON( $v ) { |
173 | 178 | |
174 | 179 | // if it's a string then try and decode it |
175 | - if( is_string($v) ) |
|
176 | - $v = json_decode($v, true); |
|
180 | + if( is_string($v) ) { |
|
181 | + $v = json_decode($v, true); |
|
182 | + } |
|
177 | 183 | // otherwise check we can encode it - we don't care about the function result |
178 | - else |
|
179 | - json_encode($v); |
|
184 | + else { |
|
185 | + json_encode($v); |
|
186 | + } |
|
180 | 187 | |
181 | 188 | return (json_last_error() === JSON_ERROR_NONE) ? $v : false; |
182 | 189 | |
@@ -184,11 +191,11 @@ discard block |
||
184 | 191 | |
185 | 192 | public static function validateObject( $v, $class, $nullable = false ) { |
186 | 193 | |
187 | - if( $v instanceof $class ) |
|
188 | - return $v; |
|
189 | - |
|
190 | - elseif( $nullable && ($v === null) ) |
|
191 | - return $v; |
|
194 | + if( $v instanceof $class ) { |
|
195 | + return $v; |
|
196 | + } elseif( $nullable && ($v === null) ) { |
|
197 | + return $v; |
|
198 | + } |
|
192 | 199 | |
193 | 200 | return false; |
194 | 201 |
@@ -33,8 +33,9 @@ discard block |
||
33 | 33 | }), |
34 | 34 | new \Twig_SimpleTest('object', function( $value, $class = '' ) { |
35 | 35 | $result = is_object($value); |
36 | - if( $class ) |
|
37 | - $result = $result && ($value instanceof $class); |
|
36 | + if( $class ) { |
|
37 | + $result = $result && ($value instanceof $class); |
|
38 | + } |
|
38 | 39 | return $result; |
39 | 40 | }), |
40 | 41 | ]; |
@@ -46,8 +47,9 @@ discard block |
||
46 | 47 | new \Twig_SimpleFilter('md5', 'md5'), |
47 | 48 | new \Twig_SimpleFilter('sha1', 'sha1'), |
48 | 49 | new \Twig_SimpleFilter('truncate', function( $str, $length, $replace = '...' ) { |
49 | - if( isset($str) ) |
|
50 | - return strlen($str) <= $length ? $str : substr($str, 0, $length - strip_tags($replace)) . $replace; |
|
50 | + if( isset($str) ) { |
|
51 | + return strlen($str) <= $length ? $str : substr($str, 0, $length - strip_tags($replace)) . $replace; |
|
52 | + } |
|
51 | 53 | return null; |
52 | 54 | }), |
53 | 55 | new \Twig_SimpleFilter('sum', 'array_sum'), |