@@ -71,15 +71,27 @@ |
||
| 71 | 71 | $defaultSchema = null) |
| 72 | 72 | { |
| 73 | 73 | #Begin: Verify if all parameters send is valid. |
| 74 | - if (!is_string($driver) || !in_array($driver, self::DRIVERS)) throw new \Exception("The driver $driver don't supported."); |
|
| 75 | - if (!is_string($username) || empty($username)) throw new \Exception("Invalid username."); |
|
| 76 | - if (!is_string($password) || empty($password)) throw new \Exception("Invalid password."); |
|
| 77 | - if (!is_string($host) || empty($host)) throw new \Exception("Invalid host."); |
|
| 78 | - if (!is_string($database) || empty($database)) throw new \Exception("Invalid database name."); |
|
| 74 | + if (!is_string($driver) || !in_array($driver, self::DRIVERS)) { |
|
| 75 | + throw new \Exception("The driver $driver don't supported."); |
|
| 76 | + } |
|
| 77 | + if (!is_string($username) || empty($username)) { |
|
| 78 | + throw new \Exception("Invalid username."); |
|
| 79 | + } |
|
| 80 | + if (!is_string($password) || empty($password)) { |
|
| 81 | + throw new \Exception("Invalid password."); |
|
| 82 | + } |
|
| 83 | + if (!is_string($host) || empty($host)) { |
|
| 84 | + throw new \Exception("Invalid host."); |
|
| 85 | + } |
|
| 86 | + if (!is_string($database) || empty($database)) { |
|
| 87 | + throw new \Exception("Invalid database name."); |
|
| 88 | + } |
|
| 79 | 89 | $this->validatesConnectionName($connectionName); |
| 80 | 90 | |
| 81 | 91 | $port = is_null($port) ? '' : (int)$port; |
| 82 | - if (!is_null($port) && !is_int($port)) throw new \Exception("Invalid port format."); |
|
| 92 | + if (!is_null($port) && !is_int($port)) { |
|
| 93 | + throw new \Exception("Invalid port format."); |
|
| 94 | + } |
|
| 83 | 95 | #Constructor of the connection string |
| 84 | 96 | $this->_connectionString[$connectionName] = "$driver:host=$host;dbname=$database;port=$port;"; |
| 85 | 97 | $this->_username[$connectionName] = $username; |
@@ -55,7 +55,9 @@ discard block |
||
| 55 | 55 | $this->cleanNewData(); |
| 56 | 56 | |
| 57 | 57 | if (!is_null($object)) { |
| 58 | - if (!is_array($object)) throw new \InvalidArgumentException('Accept only array from object'); |
|
| 58 | + if (!is_array($object)) { |
|
| 59 | + throw new \InvalidArgumentException('Accept only array from object'); |
|
| 60 | + } |
|
| 59 | 61 | |
| 60 | 62 | $this->_data = $object; |
| 61 | 63 | $this->_newData = $object; |
@@ -82,9 +84,13 @@ discard block |
||
| 82 | 84 | */ |
| 83 | 85 | public function __get($name) |
| 84 | 86 | { |
| 85 | - if (strtolower($name) == 'errors') return Error::instance(); |
|
| 87 | + if (strtolower($name) == 'errors') { |
|
| 88 | + return Error::instance(); |
|
| 89 | + } |
|
| 86 | 90 | |
| 87 | - if (!key_exists($name, $this->_data)) throw new \Exception("The attribute $name not found."); |
|
| 91 | + if (!key_exists($name, $this->_data)) { |
|
| 92 | + throw new \Exception("The attribute $name not found."); |
|
| 93 | + } |
|
| 88 | 94 | |
| 89 | 95 | return $this->_data[$name]; |
| 90 | 96 | } |
@@ -156,14 +162,18 @@ discard block |
||
| 156 | 162 | $sql .= " ($repeat); "; |
| 157 | 163 | } |
| 158 | 164 | |
| 159 | - if (is_callable($this->triggerBefore)) ($this->triggerBefore)(); |
|
| 165 | + if (is_callable($this->triggerBefore)) { |
|
| 166 | + ($this->triggerBefore)(); |
|
| 167 | + } |
|
| 160 | 168 | |
| 161 | 169 | $start = microtime(true); |
| 162 | 170 | $insert = $this->Connection->getConnection()->prepare($sql); |
| 163 | 171 | $this->burnError($insert); |
| 164 | 172 | |
| 165 | 173 | $this->_newData = array_map(function ($data){ |
| 166 | - if(is_bool($data) and $data === false) $data = 0; |
|
| 174 | + if(is_bool($data) and $data === false) { |
|
| 175 | + $data = 0; |
|
| 176 | + } |
|
| 167 | 177 | |
| 168 | 178 | return $data; |
| 169 | 179 | }, $this->_newData); |
@@ -174,7 +184,9 @@ discard block |
||
| 174 | 184 | |
| 175 | 185 | $this->Connection->setPerformedQuery($insert->queryString, round(($end - $start), 5)); |
| 176 | 186 | |
| 177 | - if (is_callable($this->triggerAfter)) ($this->triggerAfter)(); |
|
| 187 | + if (is_callable($this->triggerAfter)) { |
|
| 188 | + ($this->triggerAfter)(); |
|
| 189 | + } |
|
| 178 | 190 | |
| 179 | 191 | if ($update) { |
| 180 | 192 | $this->burnError($insert); |
@@ -193,7 +205,9 @@ discard block |
||
| 193 | 205 | } |
| 194 | 206 | |
| 195 | 207 | private function burnError($statment){ |
| 196 | - if (!is_null($statment->errorInfo()[1])) throw new \Exception($statment->errorInfo()[2], $statment->errorInfo()[1]); |
|
| 208 | + if (!is_null($statment->errorInfo()[1])) { |
|
| 209 | + throw new \Exception($statment->errorInfo()[2], $statment->errorInfo()[1]); |
|
| 210 | + } |
|
| 197 | 211 | } |
| 198 | 212 | |
| 199 | 213 | /** |
@@ -208,16 +222,23 @@ discard block |
||
| 208 | 222 | Throw new \Exception($e->getMessage()); |
| 209 | 223 | } |
| 210 | 224 | |
| 211 | - if (!isset(static::$primary_key)) throw new \Exception('Primary key don\'t set'); |
|
| 225 | + if (!isset(static::$primary_key)) { |
|
| 226 | + throw new \Exception('Primary key don\'t set'); |
|
| 227 | + } |
|
| 212 | 228 | |
| 213 | - if (!is_numeric($this->{static::$primary_key})) throw new \Exception('Primary key value don\'t is valid'); |
|
| 229 | + if (!is_numeric($this->{static::$primary_key})) { |
|
| 230 | + throw new \Exception('Primary key value don\'t is valid'); |
|
| 231 | + } |
|
| 214 | 232 | |
| 215 | 233 | $sql = ' DELETE FROM ' . static::$table_name; |
| 216 | 234 | $sql .= ' WHERE ' . static::$primary_key . ' = ? '; |
| 217 | 235 | |
| 218 | 236 | $instance = self::$_instance; |
| 219 | 237 | |
| 220 | - if (is_callable($this->triggerBefore)) ($this->triggerBefore)();; |
|
| 238 | + if (is_callable($this->triggerBefore)) { |
|
| 239 | + ($this->triggerBefore)(); |
|
| 240 | + } |
|
| 241 | + ; |
|
| 221 | 242 | |
| 222 | 243 | $start = microtime(true); |
| 223 | 244 | |
@@ -228,10 +249,15 @@ discard block |
||
| 228 | 249 | |
| 229 | 250 | $instance->Connection->setPerformedQuery($insert->queryString, round(($end - $start), 5)); |
| 230 | 251 | |
| 231 | - if (is_callable($this->triggerAfter)) ($this->triggerAfter)(); |
|
| 252 | + if (is_callable($this->triggerAfter)) { |
|
| 253 | + ($this->triggerAfter)(); |
|
| 254 | + } |
|
| 232 | 255 | |
| 233 | - if ($insert->rowCount() > 0) return true; |
|
| 234 | - else return false; |
|
| 256 | + if ($insert->rowCount() > 0) { |
|
| 257 | + return true; |
|
| 258 | + } else { |
|
| 259 | + return false; |
|
| 260 | + } |
|
| 235 | 261 | } |
| 236 | 262 | |
| 237 | 263 | /** |
@@ -249,7 +275,9 @@ discard block |
||
| 249 | 275 | */ |
| 250 | 276 | public static function all() |
| 251 | 277 | { |
| 252 | - if (!isset(static::$table_name)) throw new \Exception('Don\'t set table name in model.'); |
|
| 278 | + if (!isset(static::$table_name)) { |
|
| 279 | + throw new \Exception('Don\'t set table name in model.'); |
|
| 280 | + } |
|
| 253 | 281 | |
| 254 | 282 | $currentTable = static::$table_name; |
| 255 | 283 | |
@@ -286,8 +314,12 @@ discard block |
||
| 286 | 314 | { |
| 287 | 315 | self::instance(); |
| 288 | 316 | |
| 289 | - if (!is_string($procedureName)) throw new \Exception("Procedure name is invalid."); |
|
| 290 | - if (!is_array($param)) throw new \Exception("Tipo de parâmetros inválidos."); |
|
| 317 | + if (!is_string($procedureName)) { |
|
| 318 | + throw new \Exception("Procedure name is invalid."); |
|
| 319 | + } |
|
| 320 | + if (!is_array($param)) { |
|
| 321 | + throw new \Exception("Tipo de parâmetros inválidos."); |
|
| 322 | + } |
|
| 291 | 323 | |
| 292 | 324 | $currentTable = static::$table_name; |
| 293 | 325 | |
@@ -339,7 +371,9 @@ discard block |
||
| 339 | 371 | self::$_instance->_data = []; |
| 340 | 372 | self::$_instance->_newData = []; |
| 341 | 373 | |
| 342 | - if (!is_array($attributes)) throw new \Exception("Invalid parameter type."); |
|
| 374 | + if (!is_array($attributes)) { |
|
| 375 | + throw new \Exception("Invalid parameter type."); |
|
| 376 | + } |
|
| 343 | 377 | |
| 344 | 378 | $repeat = substr(str_repeat(' ?, ', count($attributes)), 0, -2); |
| 345 | 379 | |
@@ -356,7 +390,9 @@ discard block |
||
| 356 | 390 | Throw new \Exception($e->getMessage()); |
| 357 | 391 | } |
| 358 | 392 | |
| 359 | - if (is_callable(self::$_instance->triggerBefore)) self::$_instance->triggerBefore(); |
|
| 393 | + if (is_callable(self::$_instance->triggerBefore)) { |
|
| 394 | + self::$_instance->triggerBefore(); |
|
| 395 | + } |
|
| 360 | 396 | |
| 361 | 397 | $start = microtime(true); |
| 362 | 398 | |
@@ -367,7 +403,9 @@ discard block |
||
| 367 | 403 | |
| 368 | 404 | $instance->Connection->setPerformedQuery($insert->queryString, round(($end - $start), 5)); |
| 369 | 405 | |
| 370 | - if (is_callable(self::$_instance->triggerAfter)) self::$_instance->triggerAfter(); |
|
| 406 | + if (is_callable(self::$_instance->triggerAfter)) { |
|
| 407 | + self::$_instance->triggerAfter(); |
|
| 408 | + } |
|
| 371 | 409 | |
| 372 | 410 | $instance->_data = $attributes; |
| 373 | 411 | $instance->_data[static::$primary_key] = $instance->Connection->getConnection()->lastInsertId(); |
@@ -395,13 +433,17 @@ discard block |
||
| 395 | 433 | throw new \Exception($e->getMessage()); |
| 396 | 434 | } |
| 397 | 435 | |
| 398 | - if (!is_array($parameters) and !is_numeric($parameters)) throw new \Exception('Invalid parameter type on model ' . get_called_class() . '.'); |
|
| 436 | + if (!is_array($parameters) and !is_numeric($parameters)) { |
|
| 437 | + throw new \Exception('Invalid parameter type on model ' . get_called_class() . '.'); |
|
| 438 | + } |
|
| 399 | 439 | |
| 400 | 440 | self::$_instance->_current_custom_query[] = 'SELECT * FROM ' . static::$table_name . ' '; |
| 401 | 441 | |
| 402 | 442 | switch ($parameters) { |
| 403 | 443 | case is_numeric($parameters): |
| 404 | - if (!isset(static::$primary_key)) throw new \Exception("Invalid parameter type."); |
|
| 444 | + if (!isset(static::$primary_key)) { |
|
| 445 | + throw new \Exception("Invalid parameter type."); |
|
| 446 | + } |
|
| 405 | 447 | |
| 406 | 448 | self::$_instance->_current_custom_query_values[] = $parameters; |
| 407 | 449 | self::$_instance->_current_custom_query[] = ' WHERE ' . static::$primary_key . ' = ?'; |
@@ -435,11 +477,15 @@ discard block |
||
| 435 | 477 | throw new \Exception($e->getMessage()); |
| 436 | 478 | } |
| 437 | 479 | |
| 438 | - if (!is_string($colunm)) throw new \Exception("Invalid parameter type."); |
|
| 480 | + if (!is_string($colunm)) { |
|
| 481 | + throw new \Exception("Invalid parameter type."); |
|
| 482 | + } |
|
| 439 | 483 | |
| 440 | 484 | self::$_instance->_current_custom_query[] = "SELECT $colunm FROM " . static::$table_name . ' '; |
| 441 | 485 | |
| 442 | - if (!isset(static::$primary_key)) throw new \Exception("Invalid parameter type."); |
|
| 486 | + if (!isset(static::$primary_key)) { |
|
| 487 | + throw new \Exception("Invalid parameter type."); |
|
| 488 | + } |
|
| 443 | 489 | |
| 444 | 490 | return self::$_instance; |
| 445 | 491 | } |
@@ -508,7 +554,9 @@ discard block |
||
| 508 | 554 | throw new \Exception($e->getMessage()); |
| 509 | 555 | } |
| 510 | 556 | |
| 511 | - if (!is_array($param)) throw new \Exception('Tipo de parâmetro inválido.'); |
|
| 557 | + if (!is_array($param)) { |
|
| 558 | + throw new \Exception('Tipo de parâmetro inválido.'); |
|
| 559 | + } |
|
| 512 | 560 | |
| 513 | 561 | $start = microtime(true); |
| 514 | 562 | |
@@ -553,7 +601,9 @@ discard block |
||
| 553 | 601 | throw new \Exception($e->getMessage()); |
| 554 | 602 | } |
| 555 | 603 | |
| 556 | - if (!is_array($param)) throw new \Exception('Invalid parameter type.'); |
|
| 604 | + if (!is_array($param)) { |
|
| 605 | + throw new \Exception('Invalid parameter type.'); |
|
| 606 | + } |
|
| 557 | 607 | |
| 558 | 608 | self::$_instance->_current_custom_query_values = $param; |
| 559 | 609 | |
@@ -592,7 +642,9 @@ discard block |
||
| 592 | 642 | */ |
| 593 | 643 | final protected function setTriggerAfter($closure = null) |
| 594 | 644 | { |
| 595 | - if (!is_callable($closure)) throw new Exception('The parameter don\'t is an closure.'); |
|
| 645 | + if (!is_callable($closure)) { |
|
| 646 | + throw new Exception('The parameter don\'t is an closure.'); |
|
| 647 | + } |
|
| 596 | 648 | |
| 597 | 649 | $this->triggerAfter = $closure; |
| 598 | 650 | |
@@ -607,7 +659,9 @@ discard block |
||
| 607 | 659 | */ |
| 608 | 660 | final protected function setTriggerBefore($closure = null) |
| 609 | 661 | { |
| 610 | - if (!is_callable($closure)) throw new Exception('The parameter don\'t is an closure.'); |
|
| 662 | + if (!is_callable($closure)) { |
|
| 663 | + throw new Exception('The parameter don\'t is an closure.'); |
|
| 664 | + } |
|
| 611 | 665 | |
| 612 | 666 | $this->triggerBefore = $closure; |
| 613 | 667 | |
@@ -621,7 +675,9 @@ discard block |
||
| 621 | 675 | */ |
| 622 | 676 | final protected function changeSchema($schema) |
| 623 | 677 | { |
| 624 | - if (!is_string($schema)) throw new \Exception('The parameter don\'t is an String.'); |
|
| 678 | + if (!is_string($schema)) { |
|
| 679 | + throw new \Exception('The parameter don\'t is an String.'); |
|
| 680 | + } |
|
| 625 | 681 | |
| 626 | 682 | $this->Connection->changeSchema($schema); |
| 627 | 683 | return $this; |
@@ -659,6 +715,8 @@ discard block |
||
| 659 | 715 | |
| 660 | 716 | final private function verifyConnection() |
| 661 | 717 | { |
| 662 | - if (is_null($this->Connection->getCurrentConnectionString())) throw new \Exception('Not set connection.'); |
|
| 718 | + if (is_null($this->Connection->getCurrentConnectionString())) { |
|
| 719 | + throw new \Exception('Not set connection.'); |
|
| 720 | + } |
|
| 663 | 721 | } |
| 664 | 722 | } |
| 665 | 723 | \ No newline at end of file |