@@ -61,7 +61,7 @@ discard block |
||
| 61 | 61 | * Remove all existing criteria from the filter. |
| 62 | 62 | * @return self |
| 63 | 63 | */ |
| 64 | - public function clear( $what = [] ) { |
|
| 64 | + public function clear($what = []) { |
|
| 65 | 65 | |
| 66 | 66 | $defaults = [ |
| 67 | 67 | 'criteria' => [], |
@@ -72,10 +72,10 @@ discard block |
||
| 72 | 72 | |
| 73 | 73 | $what = (array) $what; |
| 74 | 74 | |
| 75 | - if( empty($what) ) |
|
| 75 | + if (empty($what)) |
|
| 76 | 76 | $what = array_keys($defaults); |
| 77 | 77 | |
| 78 | - foreach( $what as $k ) { |
|
| 78 | + foreach ($what as $k) { |
|
| 79 | 79 | $this->$k = $defaults[$k]; |
| 80 | 80 | } |
| 81 | 81 | |
@@ -89,7 +89,7 @@ discard block |
||
| 89 | 89 | * @param string $value |
| 90 | 90 | * @return self |
| 91 | 91 | */ |
| 92 | - public function equals( $field, $value ) { |
|
| 92 | + public function equals($field, $value) { |
|
| 93 | 93 | return $this->addCriteria($field, '=', $value); |
| 94 | 94 | } |
| 95 | 95 | |
@@ -99,7 +99,7 @@ discard block |
||
| 99 | 99 | * @param string $value |
| 100 | 100 | * @return self |
| 101 | 101 | */ |
| 102 | - public function notEquals( $field, $value ) { |
|
| 102 | + public function notEquals($field, $value) { |
|
| 103 | 103 | return $this->addCriteria($field, '!=', $value); |
| 104 | 104 | } |
| 105 | 105 | |
@@ -109,7 +109,7 @@ discard block |
||
| 109 | 109 | * @param string $value |
| 110 | 110 | * @return self |
| 111 | 111 | */ |
| 112 | - public function lessThan( $field, $value ) { |
|
| 112 | + public function lessThan($field, $value) { |
|
| 113 | 113 | return $this->addCriteria($field, '<', $value); |
| 114 | 114 | } |
| 115 | 115 | |
@@ -119,7 +119,7 @@ discard block |
||
| 119 | 119 | * @param string $value |
| 120 | 120 | * @return self |
| 121 | 121 | */ |
| 122 | - public function greaterThan( $field, $value ) { |
|
| 122 | + public function greaterThan($field, $value) { |
|
| 123 | 123 | return $this->addCriteria($field, '>', $value); |
| 124 | 124 | } |
| 125 | 125 | |
@@ -129,7 +129,7 @@ discard block |
||
| 129 | 129 | * @param string $value |
| 130 | 130 | * @return self |
| 131 | 131 | */ |
| 132 | - public function equalsLessThan( $field, $value ) { |
|
| 132 | + public function equalsLessThan($field, $value) { |
|
| 133 | 133 | return $this->addCriteria($field, '<=', $value); |
| 134 | 134 | } |
| 135 | 135 | |
@@ -139,7 +139,7 @@ discard block |
||
| 139 | 139 | * @param string $value |
| 140 | 140 | * @return self |
| 141 | 141 | */ |
| 142 | - public function equalsGreaterThan( $field, $value ) { |
|
| 142 | + public function equalsGreaterThan($field, $value) { |
|
| 143 | 143 | return $this->addCriteria($field, '>=', $value); |
| 144 | 144 | } |
| 145 | 145 | |
@@ -149,7 +149,7 @@ discard block |
||
| 149 | 149 | * @param string $value pattern to match |
| 150 | 150 | * @return self |
| 151 | 151 | */ |
| 152 | - public function like( $field, $value ) { |
|
| 152 | + public function like($field, $value) { |
|
| 153 | 153 | return $this->addCriteria($field, 'LIKE', $value); |
| 154 | 154 | } |
| 155 | 155 | |
@@ -159,7 +159,7 @@ discard block |
||
| 159 | 159 | * @param string $value pattern to match |
| 160 | 160 | * @return self |
| 161 | 161 | */ |
| 162 | - public function notLike( $field, $value ) { |
|
| 162 | + public function notLike($field, $value) { |
|
| 163 | 163 | return $this->addCriteria($field, 'NOT LIKE', $value); |
| 164 | 164 | } |
| 165 | 165 | |
@@ -169,7 +169,7 @@ discard block |
||
| 169 | 169 | * @param array $values values to match |
| 170 | 170 | * @return self |
| 171 | 171 | */ |
| 172 | - public function in( $field, array $values ) { |
|
| 172 | + public function in($field, array $values) { |
|
| 173 | 173 | return $this->addCriteria($field, 'IN', $values); |
| 174 | 174 | } |
| 175 | 175 | |
@@ -179,7 +179,7 @@ discard block |
||
| 179 | 179 | * @param array $values values to exclude |
| 180 | 180 | * @return self |
| 181 | 181 | */ |
| 182 | - public function notIn( $field, array $values ) { |
|
| 182 | + public function notIn($field, array $values) { |
|
| 183 | 183 | return $this->addCriteria($field, 'NOT IN', $values); |
| 184 | 184 | } |
| 185 | 185 | |
@@ -190,16 +190,16 @@ discard block |
||
| 190 | 190 | * @param boolean $dir true to sort ascending, false to sort descending |
| 191 | 191 | * @return self |
| 192 | 192 | */ |
| 193 | - public function orderBy( $field, $dir = Filter::SORT_ASC ) { |
|
| 193 | + public function orderBy($field, $dir = Filter::SORT_ASC) { |
|
| 194 | 194 | |
| 195 | 195 | $field = trim($field); |
| 196 | 196 | |
| 197 | - if( empty($field) ) |
|
| 197 | + if (empty($field)) |
|
| 198 | 198 | throw new \InvalidArgumentException('No field specified'); |
| 199 | 199 | |
| 200 | 200 | $modifier = substr($field, 0, 1); |
| 201 | 201 | |
| 202 | - if( in_array($modifier, ['+', '-']) ) { |
|
| 202 | + if (in_array($modifier, ['+', '-'])) { |
|
| 203 | 203 | $dir = ($modifier == '+'); |
| 204 | 204 | $field = substr($field, 1); |
| 205 | 205 | } |
@@ -215,7 +215,7 @@ discard block |
||
| 215 | 215 | * @param integer $offset |
| 216 | 216 | * @return self |
| 217 | 217 | */ |
| 218 | - public function offset( $offset ) { |
|
| 218 | + public function offset($offset) { |
|
| 219 | 219 | $this->offset = max(0, (int) $offset); |
| 220 | 220 | return $this; |
| 221 | 221 | } |
@@ -225,7 +225,7 @@ discard block |
||
| 225 | 225 | * @param integer $limit |
| 226 | 226 | * @return self |
| 227 | 227 | */ |
| 228 | - public function limit( $limit ) { |
|
| 228 | + public function limit($limit) { |
|
| 229 | 229 | $this->limit = max(1, (int) $limit); |
| 230 | 230 | return $this; |
| 231 | 231 | } |
@@ -236,47 +236,47 @@ discard block |
||
| 236 | 236 | * @param array $columns a map of field names to database column names |
| 237 | 237 | * @return Query |
| 238 | 238 | */ |
| 239 | - public function toQuery( Query $query, array $columns = [], $alias = '' ) { |
|
| 239 | + public function toQuery(Query $query, array $columns = [], $alias = '') { |
|
| 240 | 240 | |
| 241 | - foreach( $this->criteria as $column => $criteria ) { |
|
| 241 | + foreach ($this->criteria as $column => $criteria) { |
|
| 242 | 242 | |
| 243 | - if( !$column = $this->getColumnName($columns, $column, $alias) ) |
|
| 243 | + if (!$column = $this->getColumnName($columns, $column, $alias)) |
|
| 244 | 244 | continue; |
| 245 | 245 | |
| 246 | - foreach( $criteria as $operator => $value ) { |
|
| 246 | + foreach ($criteria as $operator => $value) { |
|
| 247 | 247 | $query->where($column, $operator, $value); |
| 248 | 248 | } |
| 249 | 249 | |
| 250 | 250 | } |
| 251 | 251 | |
| 252 | - foreach( $this->getOrder() as $column => $ascending ) { |
|
| 252 | + foreach ($this->getOrder() as $column => $ascending) { |
|
| 253 | 253 | $column = $this->getColumnName($columns, $column, $alias); |
| 254 | - if( !$column ) |
|
| 254 | + if (!$column) |
|
| 255 | 255 | continue; |
| 256 | 256 | $query->orderBy($column, $ascending); |
| 257 | 257 | } |
| 258 | 258 | |
| 259 | - if( $this->offset ) |
|
| 259 | + if ($this->offset) |
|
| 260 | 260 | $query->offset($this->offset); |
| 261 | 261 | |
| 262 | - if( $this->limit ) |
|
| 262 | + if ($this->limit) |
|
| 263 | 263 | $query->limit($this->limit); |
| 264 | 264 | |
| 265 | 265 | return $query; |
| 266 | 266 | |
| 267 | 267 | } |
| 268 | 268 | |
| 269 | - protected function getColumnName( $columns, $column, $alias ) { |
|
| 269 | + protected function getColumnName($columns, $column, $alias) { |
|
| 270 | 270 | |
| 271 | 271 | $column = isset($columns[$column]) ? $columns[$column] : $column; |
| 272 | 272 | |
| 273 | - if( !$column ) |
|
| 273 | + if (!$column) |
|
| 274 | 274 | return ''; |
| 275 | 275 | |
| 276 | - elseif( strpos($column, '.') ) |
|
| 276 | + elseif (strpos($column, '.')) |
|
| 277 | 277 | return $column; |
| 278 | 278 | |
| 279 | - elseif( $alias ) |
|
| 279 | + elseif ($alias) |
|
| 280 | 280 | return "{$alias}.{$column}"; |
| 281 | 281 | |
| 282 | 282 | return $column; |
@@ -290,11 +290,11 @@ discard block |
||
| 290 | 290 | * @param mixed $value |
| 291 | 291 | * @return self |
| 292 | 292 | */ |
| 293 | - protected function addCriteria( $field, $operator, $value ) { |
|
| 293 | + protected function addCriteria($field, $operator, $value) { |
|
| 294 | 294 | |
| 295 | 295 | $field = trim($field); |
| 296 | 296 | |
| 297 | - if( !isset($this->criteria[$field]) ) |
|
| 297 | + if (!isset($this->criteria[$field])) |
|
| 298 | 298 | $this->criteria[$field] = []; |
| 299 | 299 | |
| 300 | 300 | $this->criteria[$field][$operator] = $value; |
@@ -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 | |
@@ -33,16 +33,16 @@ discard block |
||
| 33 | 33 | */ |
| 34 | 34 | class BaseConfig extends collections\BaseDictionary implements Config { |
| 35 | 35 | |
| 36 | - public function load( $file, $key = '' ) { |
|
| 36 | + public function load($file, $key = '') { |
|
| 37 | 37 | |
| 38 | 38 | $config = []; |
| 39 | 39 | |
| 40 | 40 | require $file; |
| 41 | 41 | |
| 42 | - if( !isset($config) || !is_array($config) ) |
|
| 42 | + if (!isset($config) || !is_array($config)) |
|
| 43 | 43 | throw new \LogicException('Invalid Configuration'); |
| 44 | 44 | |
| 45 | - if( empty($key) ) |
|
| 45 | + if (empty($key)) |
|
| 46 | 46 | $this->merge($config); |
| 47 | 47 | else |
| 48 | 48 | $this->set($key, $config); |
@@ -51,17 +51,17 @@ discard block |
||
| 51 | 51 | |
| 52 | 52 | } |
| 53 | 53 | |
| 54 | - public function has( $key ) { |
|
| 54 | + public function has($key) { |
|
| 55 | 55 | return $this->get($key) !== null; |
| 56 | 56 | } |
| 57 | 57 | |
| 58 | - public function get( $key, $default = null ) { |
|
| 58 | + public function get($key, $default = null) { |
|
| 59 | 59 | |
| 60 | 60 | $parts = explode('.', $key); |
| 61 | 61 | $context = &$this->items; |
| 62 | 62 | |
| 63 | - foreach( $parts as $part ) { |
|
| 64 | - if( !isset($context[$part]) ) { |
|
| 63 | + foreach ($parts as $part) { |
|
| 64 | + if (!isset($context[$part])) { |
|
| 65 | 65 | return $default; |
| 66 | 66 | } |
| 67 | 67 | $context = &$context[$part]; |
@@ -71,20 +71,20 @@ discard block |
||
| 71 | 71 | |
| 72 | 72 | } |
| 73 | 73 | |
| 74 | - public function set( $key, $value ) { |
|
| 74 | + public function set($key, $value) { |
|
| 75 | 75 | |
| 76 | 76 | $parts = explode('.', $key); |
| 77 | 77 | $count = count($parts) - 1; |
| 78 | 78 | $context = &$this->items; |
| 79 | 79 | |
| 80 | - for( $i = 0; $i <= $count; $i++ ) { |
|
| 80 | + for ($i = 0; $i <= $count; $i++) { |
|
| 81 | 81 | $part = $parts[$i]; |
| 82 | - if( !isset($context[$part]) && ($i < $count) ) { |
|
| 82 | + if (!isset($context[$part]) && ($i < $count)) { |
|
| 83 | 83 | $context[$part] = []; |
| 84 | 84 | } |
| 85 | - elseif( $i == $count ) { |
|
| 85 | + elseif ($i == $count) { |
|
| 86 | 86 | $context[$part] = $value; |
| 87 | - if( $parts[0] == 'php' ) { |
|
| 87 | + if ($parts[0] == 'php') { |
|
| 88 | 88 | ini_set($part, $value); |
| 89 | 89 | } |
| 90 | 90 | return $this; |
@@ -96,9 +96,9 @@ discard block |
||
| 96 | 96 | |
| 97 | 97 | } |
| 98 | 98 | |
| 99 | - public function merge( array $config ) { |
|
| 99 | + public function merge(array $config) { |
|
| 100 | 100 | |
| 101 | - foreach( $config as $k => $v ) { |
|
| 101 | + foreach ($config as $k => $v) { |
|
| 102 | 102 | $this->set($k, $v); |
| 103 | 103 | } |
| 104 | 104 | |
@@ -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); |
@@ -38,9 +38,9 @@ discard block |
||
| 38 | 38 | * @param array $rules an array of validation rules |
| 39 | 39 | * @return self |
| 40 | 40 | */ |
| 41 | - public function add( $name, $type = Type::TEXT, $rules = [] ) { |
|
| 41 | + public function add($name, $type = Type::TEXT, $rules = []) { |
|
| 42 | 42 | $this->fields[$name] = new Field($name, $type, $rules); |
| 43 | - $this->types = []; // clear this so it will be refreshed when we've finished adding fields |
|
| 43 | + $this->types = []; // clear this so it will be refreshed when we've finished adding fields |
|
| 44 | 44 | return $this; |
| 45 | 45 | } |
| 46 | 46 | |
@@ -49,18 +49,18 @@ discard block |
||
| 49 | 49 | * @param array $data |
| 50 | 50 | * @return array first element is an array of cleaned data, second is an array of the errors (if any) |
| 51 | 51 | */ |
| 52 | - public function validate( array $data ) { |
|
| 52 | + public function validate(array $data) { |
|
| 53 | 53 | |
| 54 | 54 | $errors = []; |
| 55 | 55 | |
| 56 | - foreach( $this->fields as $field ) { |
|
| 56 | + foreach ($this->fields as $field) { |
|
| 57 | 57 | |
| 58 | 58 | $f = $field->name; |
| 59 | 59 | $v = isset($data[$f]) ? $data[$f] : null; |
| 60 | 60 | |
| 61 | 61 | list($clean, $errors[$f]) = $field->validate($v); |
| 62 | 62 | |
| 63 | - if( !$errors[$f] ) |
|
| 63 | + if (!$errors[$f]) |
|
| 64 | 64 | $data[$f] = $clean; |
| 65 | 65 | |
| 66 | 66 | } |
@@ -78,17 +78,17 @@ discard block |
||
| 78 | 78 | |
| 79 | 79 | public function getDefaults() { |
| 80 | 80 | $defaults = []; |
| 81 | - foreach( $this->fields as $field ) { |
|
| 81 | + foreach ($this->fields as $field) { |
|
| 82 | 82 | $defaults[$field->name] = $field->default; |
| 83 | 83 | } |
| 84 | 84 | return $defaults; |
| 85 | 85 | } |
| 86 | 86 | |
| 87 | - public function __get( $key ) { |
|
| 87 | + public function __get($key) { |
|
| 88 | 88 | return isset($this->fields[$key]) ? $this->fields[$key] : null; |
| 89 | 89 | } |
| 90 | 90 | |
| 91 | - public function __isset( $key ) { |
|
| 91 | + public function __isset($key) { |
|
| 92 | 92 | return isset($this->fields[$key]); |
| 93 | 93 | } |
| 94 | 94 | |
@@ -100,20 +100,20 @@ discard block |
||
| 100 | 100 | return count($this->fields); |
| 101 | 101 | } |
| 102 | 102 | |
| 103 | - public function getByType( $type ) { |
|
| 103 | + public function getByType($type) { |
|
| 104 | 104 | |
| 105 | 105 | $type = strtolower($type); |
| 106 | 106 | |
| 107 | - if( !$this->types ) { |
|
| 107 | + if (!$this->types) { |
|
| 108 | 108 | $this->types['unique'] = []; |
| 109 | - foreach( $this->fields as $field ) { |
|
| 109 | + foreach ($this->fields as $field) { |
|
| 110 | 110 | |
| 111 | - if( !isset($this->types[$field->type]) ) |
|
| 111 | + if (!isset($this->types[$field->type])) |
|
| 112 | 112 | $this->types[$field->type] = []; |
| 113 | 113 | |
| 114 | 114 | $this->types[$field->type][] = $field->name; |
| 115 | 115 | |
| 116 | - if( $field->isUnique() ) |
|
| 116 | + if ($field->isUnique()) |
|
| 117 | 117 | $this->types['unique'][] = $field->name; |
| 118 | 118 | |
| 119 | 119 | } |
@@ -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 | } |
@@ -15,7 +15,7 @@ |
||
| 15 | 15 | |
| 16 | 16 | protected $errors = []; |
| 17 | 17 | |
| 18 | - public function __construct( array $errors, $source, \Exception $previous = null ) {
|
|
| 18 | + public function __construct(array $errors, $source, \Exception $previous = null) {
|
|
| 19 | 19 | parent::__construct("Validation Error: {$source}");
|
| 20 | 20 | $this->errors = $errors; |
| 21 | 21 | } |
@@ -23,7 +23,7 @@ discard block |
||
| 23 | 23 | |
| 24 | 24 | protected $items; |
| 25 | 25 | |
| 26 | - public function __construct( array $items = [] ) { |
|
| 26 | + public function __construct(array $items = []) { |
|
| 27 | 27 | $this->items = $items; |
| 28 | 28 | } |
| 29 | 29 | |
@@ -40,7 +40,7 @@ discard block |
||
| 40 | 40 | return $this; |
| 41 | 41 | } |
| 42 | 42 | |
| 43 | - public function contains( $item ) { |
|
| 43 | + public function contains($item) { |
|
| 44 | 44 | return array_search($item, $this->items) !== false; |
| 45 | 45 | } |
| 46 | 46 | |
@@ -18,15 +18,15 @@ discard block |
||
| 18 | 18 | */ |
| 19 | 19 | class BaseDictionary extends BaseCollection implements Dictionary { |
| 20 | 20 | |
| 21 | - public function has( $key ) { |
|
| 21 | + public function has($key) { |
|
| 22 | 22 | return array_key_exists($key, $this->items); |
| 23 | 23 | } |
| 24 | 24 | |
| 25 | - public function get( $key, $default = null ) { |
|
| 25 | + public function get($key, $default = null) { |
|
| 26 | 26 | return isset($this->items[$key]) ? $this->items[$key] : $default; |
| 27 | 27 | } |
| 28 | 28 | |
| 29 | - public function set( $key, $item ) { |
|
| 29 | + public function set($key, $item) { |
|
| 30 | 30 | |
| 31 | 31 | $current = $this->get($key); |
| 32 | 32 | |
@@ -36,7 +36,7 @@ discard block |
||
| 36 | 36 | |
| 37 | 37 | } |
| 38 | 38 | |
| 39 | - public function remove( $key ) { |
|
| 39 | + public function remove($key) { |
|
| 40 | 40 | $current = $this->get($key); |
| 41 | 41 | unset($this->items[$key]); |
| 42 | 42 | return $current; |
@@ -18,21 +18,21 @@ |
||
| 18 | 18 | */ |
| 19 | 19 | class BaseSet extends BaseCollection implements Set { |
| 20 | 20 | |
| 21 | - public function add( $item ) { |
|
| 21 | + public function add($item) { |
|
| 22 | 22 | |
| 23 | 23 | $new = !$this->contains($item); |
| 24 | 24 | |
| 25 | - if( $new ) |
|
| 25 | + if ($new) |
|
| 26 | 26 | $this->items[] = $item; |
| 27 | 27 | |
| 28 | 28 | return $new; |
| 29 | 29 | } |
| 30 | 30 | |
| 31 | - public function remove( $item ) { |
|
| 31 | + public function remove($item) { |
|
| 32 | 32 | |
| 33 | 33 | $key = array_search($item, $this->items); |
| 34 | 34 | |
| 35 | - if( $key !== false ) |
|
| 35 | + if ($key !== false) |
|
| 36 | 36 | unset($this->items[$key]); |
| 37 | 37 | |
| 38 | 38 | return $key !== false; |
@@ -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 | |
@@ -29,7 +29,7 @@ discard block |
||
| 29 | 29 | |
| 30 | 30 | protected $default; |
| 31 | 31 | |
| 32 | - protected $label; |
|
| 32 | + protected $label; |
|
| 33 | 33 | |
| 34 | 34 | protected $rules; |
| 35 | 35 | |
@@ -43,7 +43,7 @@ discard block |
||
| 43 | 43 | 'required' => false, |
| 44 | 44 | 'nullable' => false, |
| 45 | 45 | 'default' => '', |
| 46 | - 'label' => $name, |
|
| 46 | + 'label' => $name, |
|
| 47 | 47 | ]; |
| 48 | 48 | $rules += $defaults; |
| 49 | 49 | |
@@ -66,17 +66,17 @@ discard block |
||
| 66 | 66 | return isset($this->$key) || isset($this->rules[$key]); |
| 67 | 67 | } |
| 68 | 68 | |
| 69 | - public function toArray() { |
|
| 70 | - return [ |
|
| 71 | - 'name' => $this->name, |
|
| 72 | - 'type' => $this->type, |
|
| 73 | - 'required' => $this->required, |
|
| 74 | - 'nullable' => $this->nullable, |
|
| 75 | - 'default' => $this->default, |
|
| 76 | - 'label' => $this->label, |
|
| 77 | - 'rules' => $this->rules, |
|
| 78 | - ]; |
|
| 79 | - } |
|
| 69 | + public function toArray() { |
|
| 70 | + return [ |
|
| 71 | + 'name' => $this->name, |
|
| 72 | + 'type' => $this->type, |
|
| 73 | + 'required' => $this->required, |
|
| 74 | + 'nullable' => $this->nullable, |
|
| 75 | + 'default' => $this->default, |
|
| 76 | + 'label' => $this->label, |
|
| 77 | + 'rules' => $this->rules, |
|
| 78 | + ]; |
|
| 79 | + } |
|
| 80 | 80 | |
| 81 | 81 | public function isNumeric() { |
| 82 | 82 | return in_array($this->type, [ |
@@ -335,7 +335,7 @@ discard block |
||
| 335 | 335 | $this->required = $rules['required']; |
| 336 | 336 | $this->nullable = $rules['nullable']; |
| 337 | 337 | $this->default = $rules['default']; |
| 338 | - $this->label = $rules['label']; |
|
| 338 | + $this->label = $rules['label']; |
|
| 339 | 339 | |
| 340 | 340 | unset( |
| 341 | 341 | $rules['required'], |
@@ -33,7 +33,7 @@ discard block |
||
| 33 | 33 | |
| 34 | 34 | protected $rules; |
| 35 | 35 | |
| 36 | - public function __construct( $name, $type = Type::TEXT, array $rules = [] ) { |
|
| 36 | + public function __construct($name, $type = Type::TEXT, array $rules = []) { |
|
| 37 | 37 | |
| 38 | 38 | $this->name = $name; |
| 39 | 39 | $this->type = $type; |
@@ -51,18 +51,18 @@ discard block |
||
| 51 | 51 | |
| 52 | 52 | } |
| 53 | 53 | |
| 54 | - public function __get( $key ) { |
|
| 55 | - if( $key == 'default' ) |
|
| 54 | + public function __get($key) { |
|
| 55 | + if ($key == 'default') |
|
| 56 | 56 | return $this->getDefault(); |
| 57 | - elseif( isset($this->rules[$key]) ) |
|
| 57 | + elseif (isset($this->rules[$key])) |
|
| 58 | 58 | return $this->rules[$key]; |
| 59 | - elseif( isset($this->$key) ) |
|
| 59 | + elseif (isset($this->$key)) |
|
| 60 | 60 | return $this->$key; |
| 61 | 61 | else |
| 62 | 62 | return null; |
| 63 | 63 | } |
| 64 | 64 | |
| 65 | - public function __isset( $key ) { |
|
| 65 | + public function __isset($key) { |
|
| 66 | 66 | return isset($this->$key) || isset($this->rules[$key]); |
| 67 | 67 | } |
| 68 | 68 | |
@@ -126,9 +126,9 @@ discard block |
||
| 126 | 126 | return !empty($this->rules['unique']); |
| 127 | 127 | } |
| 128 | 128 | |
| 129 | - public function cast( $v ) { |
|
| 129 | + public function cast($v) { |
|
| 130 | 130 | |
| 131 | - switch( $this->type ) { |
|
| 131 | + switch ($this->type) { |
|
| 132 | 132 | case Type::INTEGER: |
| 133 | 133 | case Type::TIMESTAMP: |
| 134 | 134 | case Type::YEAR: |
@@ -145,9 +145,9 @@ discard block |
||
| 145 | 145 | return preg_match('/0000-00-00/', $v) ? '' : $v; |
| 146 | 146 | |
| 147 | 147 | case Type::JSON: |
| 148 | - if( !$v ) |
|
| 148 | + if (!$v) |
|
| 149 | 149 | $v = []; |
| 150 | - elseif( !is_array($v) && is_array($arr = json_decode($v, true)) ) |
|
| 150 | + elseif (!is_array($v) && is_array($arr = json_decode($v, true))) |
|
| 151 | 151 | $v = $arr; |
| 152 | 152 | return $v; |
| 153 | 153 | |
@@ -157,17 +157,17 @@ discard block |
||
| 157 | 157 | |
| 158 | 158 | } |
| 159 | 159 | |
| 160 | - public function validate( $v ) { |
|
| 160 | + public function validate($v) { |
|
| 161 | 161 | |
| 162 | - if( $this->required && Validator::isEmpty($v, $this->type) ) |
|
| 162 | + if ($this->required && Validator::isEmpty($v, $this->type)) |
|
| 163 | 163 | return [$v, Error::REQUIRED]; |
| 164 | 164 | |
| 165 | - elseif( !$this->nullable && ($v === null) ) |
|
| 165 | + elseif (!$this->nullable && ($v === null)) |
|
| 166 | 166 | return [$v, Error::NULL]; |
| 167 | 167 | |
| 168 | 168 | list($clean, $error) = $this->validateType($v, $this->type); |
| 169 | 169 | |
| 170 | - if( $error ) |
|
| 170 | + if ($error) |
|
| 171 | 171 | return [$v, $error]; |
| 172 | 172 | else |
| 173 | 173 | return $this->validateRules($clean); |
@@ -180,17 +180,17 @@ discard block |
||
| 180 | 180 | */ |
| 181 | 181 | protected function getDefault() { |
| 182 | 182 | $default = $this->default; |
| 183 | - if( $default instanceof \Closure ) |
|
| 183 | + if ($default instanceof \Closure) |
|
| 184 | 184 | return $default(); |
| 185 | 185 | else |
| 186 | 186 | return $this->cast($default); |
| 187 | 187 | } |
| 188 | 188 | |
| 189 | - protected function validateType( $v, $type ) { |
|
| 189 | + protected function validateType($v, $type) { |
|
| 190 | 190 | |
| 191 | 191 | $error = Error::NONE; |
| 192 | 192 | |
| 193 | - switch( $type ) { |
|
| 193 | + switch ($type) { |
|
| 194 | 194 | |
| 195 | 195 | case Type::TEXT: |
| 196 | 196 | $clean = trim((string) $v); |
@@ -261,81 +261,81 @@ discard block |
||
| 261 | 261 | } |
| 262 | 262 | |
| 263 | 263 | // boolean fields will be null on error |
| 264 | - if( $type == Type::BOOLEAN ) |
|
| 264 | + if ($type == Type::BOOLEAN) |
|
| 265 | 265 | $error = ($clean === null) ? Error::BOOLEAN : Error::NONE; |
| 266 | - elseif( $clean === false ) |
|
| 266 | + elseif ($clean === false) |
|
| 267 | 267 | $error = Error::getTypeError($type); |
| 268 | 268 | |
| 269 | 269 | return [$clean, $error]; |
| 270 | 270 | |
| 271 | 271 | } |
| 272 | 272 | |
| 273 | - protected function validateRules( $v ) { |
|
| 273 | + protected function validateRules($v) { |
|
| 274 | 274 | |
| 275 | - if( $error = $this->validateRange($v) ) |
|
| 275 | + if ($error = $this->validateRange($v)) |
|
| 276 | 276 | return [$v, $error]; |
| 277 | 277 | |
| 278 | - elseif( $error = $this->validateLength($v) ) |
|
| 278 | + elseif ($error = $this->validateLength($v)) |
|
| 279 | 279 | return [$v, $error]; |
| 280 | 280 | |
| 281 | - elseif( $error = $this->validateValues($v) ) |
|
| 281 | + elseif ($error = $this->validateValues($v)) |
|
| 282 | 282 | return [$v, $error]; |
| 283 | 283 | |
| 284 | - elseif( $error = $this->validateRegex($v) ) |
|
| 284 | + elseif ($error = $this->validateRegex($v)) |
|
| 285 | 285 | return [$v, $error]; |
| 286 | 286 | |
| 287 | 287 | return [$v, $error]; |
| 288 | 288 | |
| 289 | 289 | } |
| 290 | 290 | |
| 291 | - protected function validateRange( $v ) { |
|
| 291 | + protected function validateRange($v) { |
|
| 292 | 292 | |
| 293 | - if( isset($this->rules['min']) && $v && ($v < $this->rules['min']) ) |
|
| 293 | + if (isset($this->rules['min']) && $v && ($v < $this->rules['min'])) |
|
| 294 | 294 | return Error::MIN; |
| 295 | 295 | |
| 296 | - if( isset($this->rules['max']) && $v && ($v > $this->rules['max']) ) |
|
| 296 | + if (isset($this->rules['max']) && $v && ($v > $this->rules['max'])) |
|
| 297 | 297 | return Error::MAX; |
| 298 | 298 | |
| 299 | 299 | return Error::NONE; |
| 300 | 300 | |
| 301 | 301 | } |
| 302 | 302 | |
| 303 | - protected function validateLength( $v ) { |
|
| 303 | + protected function validateLength($v) { |
|
| 304 | 304 | |
| 305 | - if( isset($this->rules['min_length']) && mb_strlen($v) < $this->rules['min_length'] ) |
|
| 305 | + if (isset($this->rules['min_length']) && mb_strlen($v) < $this->rules['min_length']) |
|
| 306 | 306 | return Error::TOO_SHORT; |
| 307 | 307 | |
| 308 | - if( isset($this->rules['max_length']) && mb_strlen($v) > $this->rules['max_length'] ) |
|
| 308 | + if (isset($this->rules['max_length']) && mb_strlen($v) > $this->rules['max_length']) |
|
| 309 | 309 | return Error::TOO_LONG; |
| 310 | 310 | |
| 311 | 311 | return Error::NONE; |
| 312 | 312 | |
| 313 | 313 | } |
| 314 | 314 | |
| 315 | - protected function validateRegex( $v ) { |
|
| 315 | + protected function validateRegex($v) { |
|
| 316 | 316 | |
| 317 | - if( isset($this->rules['regex']) && !preg_match($this->rules['regex'], $v) ) |
|
| 317 | + if (isset($this->rules['regex']) && !preg_match($this->rules['regex'], $v)) |
|
| 318 | 318 | return Error::REGEX; |
| 319 | 319 | |
| 320 | 320 | return Error::NONE; |
| 321 | 321 | |
| 322 | 322 | } |
| 323 | 323 | |
| 324 | - protected function validateValues( $v ) { |
|
| 324 | + protected function validateValues($v) { |
|
| 325 | 325 | |
| 326 | - if( isset($this->rules['values']) && !in_array($v, $this->rules['values']) ) |
|
| 326 | + if (isset($this->rules['values']) && !in_array($v, $this->rules['values'])) |
|
| 327 | 327 | return Error::VALUE; |
| 328 | 328 | |
| 329 | 329 | return Error::NONE; |
| 330 | 330 | |
| 331 | 331 | } |
| 332 | 332 | |
| 333 | - protected function processRules( array $rules, $container = 'array' ) { |
|
| 333 | + protected function processRules(array $rules, $container = 'array') { |
|
| 334 | 334 | |
| 335 | 335 | $this->required = $rules['required']; |
| 336 | 336 | $this->nullable = $rules['nullable']; |
| 337 | 337 | $this->default = $rules['default']; |
| 338 | - $this->label = $rules['label']; |
|
| 338 | + $this->label = $rules['label']; |
|
| 339 | 339 | |
| 340 | 340 | unset( |
| 341 | 341 | $rules['required'], |
@@ -344,10 +344,10 @@ discard block |
||
| 344 | 344 | $rules['label'] |
| 345 | 345 | ); |
| 346 | 346 | |
| 347 | - if( $this->isObject() ) { |
|
| 347 | + if ($this->isObject()) { |
|
| 348 | 348 | |
| 349 | 349 | // object fields must specify a class |
| 350 | - if( empty($rules['class']) ) |
|
| 350 | + if (empty($rules['class'])) |
|
| 351 | 351 | throw new \LogicException("Missing class name for item: {$this->name}"); |
| 352 | 352 | |
| 353 | 353 | // object fields are nullable by default |
@@ -355,10 +355,10 @@ discard block |
||
| 355 | 355 | $this->default = null; |
| 356 | 356 | |
| 357 | 357 | } |
| 358 | - elseif( $this->isCollection() ) { |
|
| 358 | + elseif ($this->isCollection()) { |
|
| 359 | 359 | |
| 360 | 360 | // collection fields must specify a class |
| 361 | - if( empty($rules['class']) ) |
|
| 361 | + if (empty($rules['class'])) |
|
| 362 | 362 | throw new \LogicException("Missing item class for collection: {$this->name}"); |
| 363 | 363 | |
| 364 | 364 | $rules['container'] = empty($rules['container']) ? $container : $rules['container']; |
@@ -368,7 +368,7 @@ discard block |
||
| 368 | 368 | |
| 369 | 369 | $container = $rules['container']; |
| 370 | 370 | |
| 371 | - if( $container == 'array' ) |
|
| 371 | + if ($container == 'array') |
|
| 372 | 372 | return []; |
| 373 | 373 | else |
| 374 | 374 | return new $container($rules['class']); |
@@ -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 | |
@@ -24,16 +24,16 @@ discard block |
||
| 24 | 24 | * @param array $args array of arguments pass to the function |
| 25 | 25 | * @return boolean |
| 26 | 26 | */ |
| 27 | - public static function __callStatic( $name, $args ) { |
|
| 27 | + public static function __callStatic($name, $args) { |
|
| 28 | 28 | |
| 29 | - $name = 'validate'. substr($name, 2); |
|
| 29 | + $name = 'validate'.substr($name, 2); |
|
| 30 | 30 | |
| 31 | - if( !method_exists(__CLASS__, $name) ) |
|
| 31 | + if (!method_exists(__CLASS__, $name)) |
|
| 32 | 32 | throw new \BadMethodCallException(sprintf("%s::%s()", get_called_class(), $name)); |
| 33 | 33 | |
| 34 | 34 | $failure = ($name == 'validateBoolean') ? null : false; |
| 35 | 35 | |
| 36 | - switch( count($args) ) { |
|
| 36 | + switch (count($args)) { |
|
| 37 | 37 | case 1: |
| 38 | 38 | return static::$name($args[0]) !== $failure; |
| 39 | 39 | case 2: |
@@ -45,12 +45,12 @@ discard block |
||
| 45 | 45 | } |
| 46 | 46 | |
| 47 | 47 | |
| 48 | - public static function isEmpty( $v, $type = Type::TEXT ) { |
|
| 48 | + public static function isEmpty($v, $type = Type::TEXT) { |
|
| 49 | 49 | |
| 50 | - if( !$v || (is_scalar($v) && !trim($v)) ) |
|
| 50 | + if (!$v || (is_scalar($v) && !trim($v))) |
|
| 51 | 51 | return true; |
| 52 | 52 | |
| 53 | - switch( $type ) { |
|
| 53 | + switch ($type) { |
|
| 54 | 54 | |
| 55 | 55 | case Type::COLLECTION: |
| 56 | 56 | return count($v) == 0; |
@@ -81,7 +81,7 @@ discard block |
||
| 81 | 81 | * @param mixed $v |
| 82 | 82 | * @return integer|false |
| 83 | 83 | */ |
| 84 | - public static function validateInteger( $v ) { |
|
| 84 | + public static function validateInteger($v) { |
|
| 85 | 85 | return $v ? filter_var($v, FILTER_VALIDATE_INT) : 0; |
| 86 | 86 | } |
| 87 | 87 | |
@@ -91,7 +91,7 @@ discard block |
||
| 91 | 91 | * @param mixed $v |
| 92 | 92 | * @return float|false |
| 93 | 93 | */ |
| 94 | - public static function validateFloat( $v ) { |
|
| 94 | + public static function validateFloat($v) { |
|
| 95 | 95 | return $v ? filter_var($v, FILTER_VALIDATE_FLOAT, FILTER_FLAG_ALLOW_THOUSAND) : 0; |
| 96 | 96 | } |
| 97 | 97 | |
@@ -101,21 +101,21 @@ discard block |
||
| 101 | 101 | * @param mixed $v |
| 102 | 102 | * @return boolean|null |
| 103 | 103 | */ |
| 104 | - public static function validateBoolean( $v ) { |
|
| 104 | + public static function validateBoolean($v) { |
|
| 105 | 105 | // FILTER_VALIDATE_BOOLEAN will return null if passed an actual boolean false |
| 106 | 106 | return ($v === false) ? $v : filter_var($v, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE); |
| 107 | 107 | } |
| 108 | 108 | |
| 109 | - public static function validateTimestamp( $v ) { |
|
| 109 | + public static function validateTimestamp($v) { |
|
| 110 | 110 | $ts = filter_var($v, FILTER_VALIDATE_INT); |
| 111 | - if( $ts === false ) |
|
| 111 | + if ($ts === false) |
|
| 112 | 112 | $ts = strtotime(str_replace('@', ' ', $v)); |
| 113 | 113 | return $ts; |
| 114 | 114 | } |
| 115 | 115 | |
| 116 | - public static function validateDateTime( $v, $format = 'Y-m-d H:i:s' ) { |
|
| 116 | + public static function validateDateTime($v, $format = 'Y-m-d H:i:s') { |
|
| 117 | 117 | |
| 118 | - if( !trim($v) || preg_match('/0000-00-00/', $v) ) |
|
| 118 | + if (!trim($v) || preg_match('/0000-00-00/', $v)) |
|
| 119 | 119 | return ''; |
| 120 | 120 | |
| 121 | 121 | $ts = static::validateTimestamp($v); |
@@ -124,15 +124,15 @@ discard block |
||
| 124 | 124 | |
| 125 | 125 | } |
| 126 | 126 | |
| 127 | - public static function validateDate( $v ) { |
|
| 127 | + public static function validateDate($v) { |
|
| 128 | 128 | return static::validateDateTime($v, 'Y-m-d'); |
| 129 | 129 | } |
| 130 | 130 | |
| 131 | - public static function validateTime( $v ) { |
|
| 131 | + public static function validateTime($v) { |
|
| 132 | 132 | return static::validateDateTime($v, 'H:i:s'); |
| 133 | 133 | } |
| 134 | 134 | |
| 135 | - public static function validateYear( $v, $min = 1900, $max = 2155 ) { |
|
| 135 | + public static function validateYear($v, $min = 1900, $max = 2155) { |
|
| 136 | 136 | $v = static::validateInteger($v); |
| 137 | 137 | return ($v >= $min) && ($v <= $max); |
| 138 | 138 | } |
@@ -142,9 +142,9 @@ discard block |
||
| 142 | 142 | * @param mixed $v |
| 143 | 143 | * @return string |
| 144 | 144 | */ |
| 145 | - public static function validateIP( $v ) { |
|
| 145 | + public static function validateIP($v) { |
|
| 146 | 146 | // if integer then convert to string |
| 147 | - if( $ip = filter_var($v, FILTER_VALIDATE_INT) ) |
|
| 147 | + if ($ip = filter_var($v, FILTER_VALIDATE_INT)) |
|
| 148 | 148 | $v = long2ip($ip); |
| 149 | 149 | return filter_var($v, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4); |
| 150 | 150 | } |
@@ -155,7 +155,7 @@ discard block |
||
| 155 | 155 | * @param mixed $v |
| 156 | 156 | * @return string|boolean |
| 157 | 157 | */ |
| 158 | - public static function validateEmail( $v ) { |
|
| 158 | + public static function validateEmail($v) { |
|
| 159 | 159 | return $v ? filter_var($v, FILTER_VALIDATE_EMAIL) : ''; |
| 160 | 160 | } |
| 161 | 161 | |
@@ -165,14 +165,14 @@ discard block |
||
| 165 | 165 | * @param mixed $v |
| 166 | 166 | * @return string|boolean |
| 167 | 167 | */ |
| 168 | - public static function validateURL( $v ) { |
|
| 168 | + public static function validateURL($v) { |
|
| 169 | 169 | return $v ? filter_var($v, FILTER_VALIDATE_URL) : ''; |
| 170 | 170 | } |
| 171 | 171 | |
| 172 | - public static function validateJSON( $v ) { |
|
| 172 | + public static function validateJSON($v) { |
|
| 173 | 173 | |
| 174 | 174 | // if it's a string then try and decode it |
| 175 | - if( is_string($v) ) |
|
| 175 | + if (is_string($v)) |
|
| 176 | 176 | $v = json_decode($v, true); |
| 177 | 177 | // otherwise check we can encode it - we don't care about the function result |
| 178 | 178 | else |
@@ -182,12 +182,12 @@ discard block |
||
| 182 | 182 | |
| 183 | 183 | } |
| 184 | 184 | |
| 185 | - public static function validateObject( $v, $class, $nullable = false ) { |
|
| 185 | + public static function validateObject($v, $class, $nullable = false) { |
|
| 186 | 186 | |
| 187 | - if( $v instanceof $class ) |
|
| 187 | + if ($v instanceof $class) |
|
| 188 | 188 | return $v; |
| 189 | 189 | |
| 190 | - elseif( $nullable && ($v === null) ) |
|
| 190 | + elseif ($nullable && ($v === null)) |
|
| 191 | 191 | return $v; |
| 192 | 192 | |
| 193 | 193 | return false; |
@@ -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 | |