From 0825725abe27fc274f3586c37c7d6feb7ca5917e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mr=C2=B7x?= Date: Fri, 26 Mar 2021 19:18:43 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=B9=E5=90=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Database/Condition/Condition.php | 87 ++++++++++++++++++++------------ 1 file changed, 54 insertions(+), 33 deletions(-) diff --git a/Database/Condition/Condition.php b/Database/Condition/Condition.php index c991689a..a3638829 100644 --- a/Database/Condition/Condition.php +++ b/Database/Condition/Condition.php @@ -4,6 +4,7 @@ declare(strict_types=1); namespace Database\Condition; +use JetBrains\PhpStorm\Pure; use Snowflake\Abstracts\BaseObject; use Snowflake\Core\Str; @@ -14,48 +15,68 @@ use Snowflake\Core\Str; abstract class Condition extends BaseObject { - protected string $column = ''; - protected string $opera = '='; + protected string $column = ''; + protected string $opera = '='; - /** @var array|mixed */ - protected $value; + /** @var array|mixed */ + protected $value; - const INT_TYPE = ['bit', 'bool', 'tinyint', 'smallint', 'mediumint', 'int', 'bigint', 'float', 'double', 'decimal', 'timestamp']; + const INT_TYPE = ['bit', 'bool', 'tinyint', 'smallint', 'mediumint', 'int', 'bigint', 'float', 'double', 'decimal', 'timestamp']; - protected array $attributes = []; + protected array $attributes = []; - abstract public function builder(); + abstract public function builder(); - /** - * @param string $column - */ - public function setColumn(string $column): void - { - $this->column = $column; - } + /** + * @param string $column + */ + public function setColumn(string $column): void + { + $this->column = $column; + } - /** - * @param string $opera - */ - public function setOpera(string $opera): void - { - $this->opera = $opera; - } + /** + * @param string $opera + */ + public function setOpera(string $opera): void + { + $this->opera = $opera; + } /** * @param $params */ - public function setValue($params): void - { - if (is_array($params)) { - $values = []; - foreach ($params as $item => $value) { - $values[$item] = is_numeric($value) ? $value : '\'' . $value . '\''; - } - $this->value = $values; - } else { - $this->value = is_numeric($params) ? $params : '\'' . $params . '\''; - } - } + public function setValue($params): void + { + if (is_array($params)) { + $values = []; + foreach ($params as $item => $value) { + $values[$item] = is_numeric($value) ? $value : '\'' . $value . '\''; + } + $this->value = $values; + } else { + $this->value = $this->checkIsSqlString($params); + } + } + + + /** + * @param $params + * @return int|string + */ + #[Pure] private function checkIsSqlString($params): int|string + { + if (is_numeric($params)) { + return $params; + } + + $check = strtolower(substr($params, 0, 6)); + if (in_array($check, ['update', 'select', 'insert', 'delete'])) { + return $params; + } else { + return sprintf('\'%s\'', $params); + } + } + }