diff --git a/common/Definition/DatabaseEngineRequirements.php b/common/Definition/DatabaseEngine/Requirements.php similarity index 62% rename from common/Definition/DatabaseEngineRequirements.php rename to common/Definition/DatabaseEngine/Requirements.php index 9999fac..2a68a78 100644 --- a/common/Definition/DatabaseEngineRequirements.php +++ b/common/Definition/DatabaseEngine/Requirements.php @@ -1,7 +1,7 @@ conditions == null) { $this->conditions = []; } - $this->conditions = array_merge($this->conditions, $conditions); + foreach ($conditions as $condition) { + $this->addCondition($condition); + } return $this; } + protected function addCondition(Condition $condition) { + $this->conditions []= $condition; + } protected function parseWhere(ORM $orm): ORM { if ($this->conditions == null or count($this->conditions) == 0) { return $orm; } foreach ($this->conditions as $condition) { - $method = 'where'; - $op = '='; - if (isset($condition[2])) { - $op = strtolower($condition[2]); - } - if (isset($condition['operator'])) { - $op = strtolower($condition['operator']); - } - $mod = ['=' => '', '>' => 'Gt', '>=' => 'Gte', '<' => 'Lt', '<=' => 'Lte', '!=' => 'NotEqual']; - if (isset($mod[$op])) { - $method .= $mod[$op]; - } else { - switch (strtolower($op)) { - case 'raw': - $method = 'rawWhere'; - break; - case 'like': - $method = 'whereLike'; - break; - } - } - $column = $condition[0]; - if (isset($condition['column'])) { - $column = $condition['column']; - } - $value = $condition[1]; - if (isset($condition['value'])) { - $value = $condition['value']; - } - $orm = $orm->{$method}($column, $value); + $orm = $condition->build($orm); } return $orm; } diff --git a/src/Factory/Condition.php b/src/Factory/Condition.php new file mode 100644 index 0000000..91d2d01 --- /dev/null +++ b/src/Factory/Condition.php @@ -0,0 +1,54 @@ +table = $table; + return $this; + } + public function setColumn(string $column): Condition { + $this->column = $column; + return $this; + } + public function setOperator(Operator $operator): Condition { + $this->operator = $operator; + return $this; + } + public function setValue($value): Condition { + $this->value = $value; + return $this; + } + public function setAlias(string $alias): Condition { + $this->alias = $alias; + return $this; + } + + public function fullColumn(): string { + $str = []; + if ($this->table != null) { + $str []= $this->table; + } + $str []= $this->column; + return implode('.', $str); + } + public function build(ORM $orm): ORM { + $method = $this->operator->method(); + $params = [ + $this->fullColumn() + ]; + if ($this->operator->hasValue()) { + $params []= $this->value; + } + if ($this->alias != null) { + $params []= $this->alias; + } + $orm = call_user_func_array([$orm, $method], $params); + return $orm; + } +} diff --git a/src/Factory/Operator.php b/src/Factory/Operator.php new file mode 100644 index 0000000..7bffb67 --- /dev/null +++ b/src/Factory/Operator.php @@ -0,0 +1,107 @@ +setOperator($operator); + } + public function setOperator(string $operator) { + if (!$this->isValid($operator)) { + throw new \InvalidArgumentException($operator . ' is not a valid operator.'); + } + $this->value = strtolower($operator); + } + protected function isValid(string $operator): bool { + switch (strtolower($operator)) { + case '=': + case 'eq': + case '!=': + case 'neq': + case '<': + case 'lt': + case '<=': + case 'lte': + case '>': + case 'gt': + case '>=': + case 'gte': + case 'like': + case 'not like': + case 'in': + case 'not in': + case 'null': + case 'not null': + return true; + } + return false; + } + public function method(): string { + $method = 'where'; + switch ($this->value) { + case '!=': + case 'neq': + $method .= '_not_equal'; + break; + case '<': + case 'lt': + $method .= '_lt'; + break; + case '<=': + case 'lte': + $method .= '_lte'; + break; + case '>': + case 'gt': + $method .= '_gt'; + break; + case '>=': + case 'gte': + $method .= '_gte'; + break; + case 'like': + $method .= '_like'; + break; + case 'not like': + $method .= '_not_like'; + break; + case 'in': + $method .= '_in'; + break; + case 'not in': + $method .='_not_in'; + break; + case 'null': + $method .= '_null'; + break; + case 'not null': + $method .= '_not_null'; + break; + } + return $method; + } + public function hasValue(): bool { + switch ($this->value) { + case '=': + case 'eq': + case '!=': + case 'neq': + case '<': + case 'lt': + case '<=': + case 'lte': + case '>': + case 'gt': + case '>=': + case 'gte': + case 'like': + case 'not like': + case 'in': + case 'in': + case 'not in': + return true; + } + return false; + } +} diff --git a/src/MySQLRequirements.php b/src/MySQLRequirements.php index 2382eaa..9558068 100644 --- a/src/MySQLRequirements.php +++ b/src/MySQLRequirements.php @@ -1,7 +1,7 @@