Se crea objeto para manejar las condiciones y otro para los operadores
This commit is contained in:
@ -1,7 +1,7 @@
|
|||||||
<?php
|
<?php
|
||||||
namespace Aldarien\Common\Definition;
|
namespace Aldarien\Common\Definition\DatabaseEngine;
|
||||||
|
|
||||||
interface DatabaseEngineRequirements {
|
interface Requirements {
|
||||||
public function dsnSpecs(): array;
|
public function dsnSpecs(): array;
|
||||||
public function getDSN(object $config): string;
|
public function getDSN(object $config): string;
|
||||||
public function hasUser(): bool;
|
public function hasUser(): bool;
|
@ -127,44 +127,20 @@ class Model {
|
|||||||
if ($this->conditions == null) {
|
if ($this->conditions == null) {
|
||||||
$this->conditions = [];
|
$this->conditions = [];
|
||||||
}
|
}
|
||||||
$this->conditions = array_merge($this->conditions, $conditions);
|
foreach ($conditions as $condition) {
|
||||||
|
$this->addCondition($condition);
|
||||||
|
}
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
protected function addCondition(Condition $condition) {
|
||||||
|
$this->conditions []= $condition;
|
||||||
|
}
|
||||||
protected function parseWhere(ORM $orm): ORM {
|
protected function parseWhere(ORM $orm): ORM {
|
||||||
if ($this->conditions == null or count($this->conditions) == 0) {
|
if ($this->conditions == null or count($this->conditions) == 0) {
|
||||||
return $orm;
|
return $orm;
|
||||||
}
|
}
|
||||||
foreach ($this->conditions as $condition) {
|
foreach ($this->conditions as $condition) {
|
||||||
$method = 'where';
|
$orm = $condition->build($orm);
|
||||||
$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);
|
|
||||||
}
|
}
|
||||||
return $orm;
|
return $orm;
|
||||||
}
|
}
|
||||||
|
54
src/Factory/Condition.php
Normal file
54
src/Factory/Condition.php
Normal file
@ -0,0 +1,54 @@
|
|||||||
|
<?php
|
||||||
|
namespace Aldarien\Models\Factory;
|
||||||
|
|
||||||
|
class Condition {
|
||||||
|
protected $table;
|
||||||
|
protected $column;
|
||||||
|
protected $operator;
|
||||||
|
protected $value;
|
||||||
|
protected $alias;
|
||||||
|
|
||||||
|
public function setTable(string $table): Condition {
|
||||||
|
$this->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;
|
||||||
|
}
|
||||||
|
}
|
107
src/Factory/Operator.php
Normal file
107
src/Factory/Operator.php
Normal file
@ -0,0 +1,107 @@
|
|||||||
|
<?php
|
||||||
|
namespace Aldarien\Models\Factory;
|
||||||
|
|
||||||
|
class Operator {
|
||||||
|
protected $value;
|
||||||
|
|
||||||
|
public function __construct(string $operator) {
|
||||||
|
$this->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;
|
||||||
|
}
|
||||||
|
}
|
@ -1,7 +1,7 @@
|
|||||||
<?php
|
<?php
|
||||||
namespace Aldarien\Models;
|
namespace Aldarien\Models;
|
||||||
|
|
||||||
use Aldarien\Common\Definition\DatabaseEngineRequirements as EngineRequirementsInterface;
|
use Aldarien\Common\Definition\DatabaseEngine\Requirements as EngineRequirementsInterface;
|
||||||
|
|
||||||
class MySQLRequirements implements EngineRequirementsInterface {
|
class MySQLRequirements implements EngineRequirementsInterface {
|
||||||
public function dsnSpecs(): array {
|
public function dsnSpecs(): array {
|
||||||
|
Reference in New Issue
Block a user