From 4201a7c42cbacf4e24d726362b2e42bd48128b5a Mon Sep 17 00:00:00 2001 From: Timothy J Warren Date: Thu, 23 Apr 2020 18:16:32 -0400 Subject: [PATCH] Minor code cleanup --- src/Drivers/AbstractDriver.php | 14 ++++++++------ src/Drivers/DriverInterface.php | 6 +++--- src/JoinType.php | 27 +++++++++++++++++++++++++++ src/LikeType.php | 25 +++++++++++++++++++++++++ src/QueryBuilder.php | 30 ++++++++++++++++++++++-------- src/QueryBuilderBase.php | 8 +++++--- src/QueryBuilderInterface.php | 24 +++++++++++++++++------- 7 files changed, 107 insertions(+), 27 deletions(-) create mode 100644 src/JoinType.php create mode 100644 src/LikeType.php diff --git a/src/Drivers/AbstractDriver.php b/src/Drivers/AbstractDriver.php index 4aaec7c..e011ff9 100644 --- a/src/Drivers/AbstractDriver.php +++ b/src/Drivers/AbstractDriver.php @@ -15,11 +15,13 @@ */ namespace Query\Drivers; -use function dbFilter; - use InvalidArgumentException; use PDO; use PDOStatement; + +use function call_user_func_array; +use function dbFilter; +use function is_object; use function is_string; /** @@ -127,11 +129,11 @@ abstract class AbstractDriver { if ( isset($this->$name) - && \is_object($this->$name) + && is_object($this->$name) && method_exists($this->$name, '__invoke') ) { - return \call_user_func_array([$this->$name, '__invoke'], $args); + return call_user_func_array([$this->$name, '__invoke'], $args); } return NULL; @@ -287,7 +289,7 @@ abstract class AbstractDriver * @param string $table * @return string */ - public function quoteTable($table): string + public function quoteTable(string $table): string { $table = $this->prefixTable($table); @@ -333,7 +335,7 @@ abstract class AbstractDriver { // Unquote the function // Quote the inside identifiers - $raw = str_replace(array($f[0], $f[3]), array($f[1], $this->quoteIdent($f[3])), $raw); + $raw = str_replace([$f[0], $f[3]], [$f[1], $this->quoteIdent($f[3])], $raw); } return $raw; diff --git a/src/Drivers/DriverInterface.php b/src/Drivers/DriverInterface.php index 4e39119..2c20684 100644 --- a/src/Drivers/DriverInterface.php +++ b/src/Drivers/DriverInterface.php @@ -165,10 +165,10 @@ interface DriverInterface /* extends the interface of PDO */ { /** * Quote database table name, and set prefix * - * @param string|array $table - * @return string|array + * @param string $table + * @return string */ - public function quoteTable($table); + public function quoteTable(string $table): string; /** * Create and execute a prepared statement with the provided parameters diff --git a/src/JoinType.php b/src/JoinType.php new file mode 100644 index 0000000..4bca1be --- /dev/null +++ b/src/JoinType.php @@ -0,0 +1,27 @@ + + * @copyright 2012 - 2020 Timothy J. Warren + * @license http://www.opensource.org/licenses/mit-license.html MIT License + * @link https://git.timshomepage.net/aviat/Query + * @version 3.0.0 + */ +namespace Query; + +/** + * 'Enum' of join types + */ +class JoinType { + public const CROSS = 'cross'; + public const INNER = 'inner'; + public const OUTER = 'outer'; + public const LEFT = 'left'; + public const RIGHT = 'right'; +} \ No newline at end of file diff --git a/src/LikeType.php b/src/LikeType.php new file mode 100644 index 0000000..02ffced --- /dev/null +++ b/src/LikeType.php @@ -0,0 +1,25 @@ + + * @copyright 2012 - 2020 Timothy J. Warren + * @license http://www.opensource.org/licenses/mit-license.html MIT License + * @link https://git.timshomepage.net/aviat/Query + * @version 3.0.0 + */ +namespace Query; + +/** + * 'Enum' of join types + */ +class LikeType { + public const BEFORE = 'before'; + public const AFTER = 'after'; + public const BOTH = 'both'; +} \ No newline at end of file diff --git a/src/QueryBuilder.php b/src/QueryBuilder.php index de083cf..d4b7fb8 100644 --- a/src/QueryBuilder.php +++ b/src/QueryBuilder.php @@ -17,6 +17,7 @@ namespace Query; use function is_array; use function is_int; +use function mb_trim; use PDOStatement; @@ -170,14 +171,27 @@ class QueryBuilder extends QueryBuilderBase implements QueryBuilderInterface { /** * Specify the database table to select from * - * @param string $tblname + * Alias of `from` method to better match CodeIgniter 4 + * + * @param string $tableName * @return self */ - public function from(string $tblname): self + public function table(string $tableName): self + { + return $this->from($tableName); + } + + /** + * Specify the database table to select from + * + * @param string $tableName + * @return self + */ + public function from(string $tableName): self { // Split identifiers on spaces - $identArray = explode(' ', \mb_trim($tblname)); - $identArray = array_map('\\mb_trim', $identArray); + $identArray = explode(' ', mb_trim($tableName)); + $identArray = array_map('mb_trim', $identArray); // Quote the identifiers $identArray[0] = $this->driver->quoteTable($identArray[0]); @@ -201,7 +215,7 @@ class QueryBuilder extends QueryBuilderBase implements QueryBuilderInterface { * @param string $pos * @return self */ - public function like(string $field, $val, string $pos='both'): self + public function like(string $field, $val, string $pos=LikeType::BOTH): self { return $this->_like($field, $val, $pos); } @@ -214,7 +228,7 @@ class QueryBuilder extends QueryBuilderBase implements QueryBuilderInterface { * @param string $pos * @return self */ - public function orLike(string $field, $val, string $pos='both'): self + public function orLike(string $field, $val, string $pos=LikeType::BOTH): self { return $this->_like($field, $val, $pos, 'LIKE', 'OR'); } @@ -227,7 +241,7 @@ class QueryBuilder extends QueryBuilderBase implements QueryBuilderInterface { * @param string $pos * @return self */ - public function notLike(string $field, $val, string $pos='both'): self + public function notLike(string $field, $val, string $pos=LikeType::BOTH): self { return $this->_like($field, $val, $pos, 'NOT LIKE'); } @@ -240,7 +254,7 @@ class QueryBuilder extends QueryBuilderBase implements QueryBuilderInterface { * @param string $pos * @return self */ - public function orNotLike(string $field, $val, string $pos='both'): self + public function orNotLike(string $field, $val, string $pos=LikeType::BOTH): self { return $this->_like($field, $val, $pos, 'NOT LIKE', 'OR'); } diff --git a/src/QueryBuilderBase.php b/src/QueryBuilderBase.php index 30f9a4e..199633d 100644 --- a/src/QueryBuilderBase.php +++ b/src/QueryBuilderBase.php @@ -226,13 +226,15 @@ class QueryBuilderBase { // Add the like string into the order map $like = $field . " {$like} ?"; - if ($pos === 'before') + if ($pos === LikeType::BEFORE) { $val = "%{$val}"; - } elseif ($pos === 'after') + } + elseif ($pos === LikeType::AFTER) { $val = "{$val}%"; - } else + } + else { $val = "%{$val}%"; } diff --git a/src/QueryBuilderInterface.php b/src/QueryBuilderInterface.php index b55d033..17fa754 100644 --- a/src/QueryBuilderInterface.php +++ b/src/QueryBuilderInterface.php @@ -124,10 +124,20 @@ interface QueryBuilderInterface { /** * Specify the database table to select from * - * @param string $tblname + * Alias of `from` method to better match CodeIgniter 4 + * + * @param string $tableName * @return self */ - public function from(string $tblname): self; + public function table(string $tableName): self; + + /** + * Specify the database table to select from + * + * @param string $tableName + * @return self + */ + public function from(string $tableName): self; // -------------------------------------------------------------------------- // ! 'Like' methods @@ -141,7 +151,7 @@ interface QueryBuilderInterface { * @param string $pos * @return self */ - public function like(string $field, $values, string $pos='both'): self; + public function like(string $field, $values, string $pos=LikeType::BOTH): self; /** * Generates an OR Like clause @@ -151,7 +161,7 @@ interface QueryBuilderInterface { * @param string $pos * @return self */ - public function orLike(string $field, $values, string $pos='both'): self; + public function orLike(string $field, $values, string $pos=LikeType::BOTH): self; /** * Generates a NOT LIKE clause @@ -161,7 +171,7 @@ interface QueryBuilderInterface { * @param string $pos * @return self */ - public function notLike(string $field, $values, string $pos='both'): self; + public function notLike(string $field, $values, string $pos=LikeType::BOTH): self; /** * Generates a OR NOT LIKE clause @@ -171,7 +181,7 @@ interface QueryBuilderInterface { * @param string $pos * @return self */ - public function orNotLike(string $field, $values, string $pos='both'): self; + public function orNotLike(string $field, $values, string $pos=LikeType::BOTH): self; // -------------------------------------------------------------------------- // ! Having methods @@ -277,7 +287,7 @@ interface QueryBuilderInterface { * @param string $type * @return self */ - public function join(string $table, string $condition, string $type=''): self; + public function join(string $table, string $condition, string $type=JoinType::INNER): self; /** * Group the results by the selected field(s)