Skip to content

Commit

Permalink
feat: adding group methods to the where object
Browse files Browse the repository at this point in the history
  • Loading branch information
noelma committed Dec 4, 2021
1 parent 69c36ac commit 178c549
Show file tree
Hide file tree
Showing 5 changed files with 148 additions and 66 deletions.
54 changes: 35 additions & 19 deletions src/RequestHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@
*
* @author Mathieu NOËL <mathieu@soosyze.com>
*
* @method Request where(\Closure|string $column, null|string $operator = null, null|numeric|string $value = null) Alias de la fonction de l'objet Queryflatfile\Where
* @method Request notWhere(\Closure|string $column, null|string $operator = null, null|numeric|string $value = null) Alias de la fonction de l'objet Queryflatfile\Where
* @method Request orWhere(\Closure|string $column, null|string $operator = null, null|numeric|string $value = null) Alias de la fonction de l'objet Queryflatfile\Where
* @method Request orNotWhere(\Closure|string $column, null|string $operator = null, null|numeric|string $value = null) Alias de la fonction de l'objet Queryflatfile\Where
* @method Request where(string $column, string $operator, null|scalar $value) Alias de la fonction de l'objet Queryflatfile\Where
* @method Request notWhere(string $column, string $operator, null|scalar $value) Alias de la fonction de l'objet Queryflatfile\Where
* @method Request orWhere(string $column, string $operator, null|scalar $value) Alias de la fonction de l'objet Queryflatfile\Where
* @method Request orNotWhere(string $column, string $operator, null|scalar $value) Alias de la fonction de l'objet Queryflatfile\Where
*
* @method Request between(string $column, numeric|string $min, numeric|string $max) Alias de la fonction de l'objet Queryflatfile\Where
* @method Request orBetween(string $column, numeric|string $min, numeric|string $max) Alias de la fonction de l'objet Queryflatfile\Where
Expand All @@ -41,6 +41,11 @@
* @method Request orRegex(string $column, string $pattern) Alias de la fonction de l'objet Queryflatfile\Where
* @method Request notRegex(string $column, string $pattern) Alias de la fonction de l'objet Queryflatfile\Where
* @method Request orNotRegex(string $column, string $pattern) Alias de la fonction de l'objet Queryflatfile\Where
*
* @method Request whereGroup(\Closure $callable) Alias de la fonction de l'objet Queryflatfile\Where
* @method Request notWhereGroup(\Closure $callable) Alias de la fonction de l'objet Queryflatfile\Where
* @method Request orWhereGroup(\Closure $callable) Alias de la fonction de l'objet Queryflatfile\Where
* @method Request orNotWhereGroup(\Closure $callable) Alias de la fonction de l'objet Queryflatfile\Where
*/
abstract class RequestHandler implements RequestInterface
{
Expand Down Expand Up @@ -187,8 +192,13 @@ public function insertInto(string $table, array $columns)
/**
* {@inheritdoc}
*/
public function leftJoin(string $table, $column, ?string $operator = null, ?string $value = null)
public function leftJoin(string $table, $column, string $operator = '', $value = null)
{
if ($column instanceof \Closure) {
$this->joinGroup(self::JOIN_LEFT, $table, $column);

return $this;
}
$this->join(self::JOIN_LEFT, $table, $column, $operator, $value);

return $this;
Expand Down Expand Up @@ -219,8 +229,13 @@ public function orderBy(string $columns, int $order = SORT_ASC)
/**
* {@inheritdoc}
*/
public function rightJoin(string $table, $column, ?string $operator = null, ?string $value = null)
public function rightJoin(string $table, $column, string $operator = '', $value = null)
{
if ($column instanceof \Closure) {
$this->joinGroup(self::JOIN_RIGHT, $table, $column);

return $this;
}
$this->join(self::JOIN_RIGHT, $table, $column, $operator, $value);

return $this;
Expand Down Expand Up @@ -302,22 +317,23 @@ protected function init()
/**
* Enregistre une jointure.
*
* @param string $type Type de la jointure.
* @param string $table Nom de la table à joindre
* @param string|\Closure $column Nom de la colonne d'une des tables précédentes
* ou une closure pour affiner les conditions.
* @param string|null $operator Opérateur logique ou null pour une closure.
* @param string|null $value Valeur
* ou une colonne de la table jointe (au format nom_table.colonne)
* ou null pour une closure.
* @param string $type Type de la jointure.
* @param string $table Nom de la table à joindre
* @param string $column Nom de la colonne d'une des tables précédentes.
* @param string $operator Opérateur logique ou null pour une closure.
* @param null|scalar $value Valeur ou une colonne de la table jointe (au format nom_table.colonne)
*/
private function join(string $type, string $table, $column, ?string $operator = null, ?string $value = null): void
private function join(string $type, string $table, string $column, string $operator, $value): void
{
$where = new Where();
$where = (new Where())->where($column, $operator, $value);

$column instanceof \Closure
? call_user_func_array($column, [ &$where ])
: $where->where($column, $operator, $value);
$this->joins[] = compact('type', 'table', 'where');
}

private function joinGroup(string $type, string $table, \Closure $callable): void
{
$where = new Where();
call_user_func_array($callable, [ &$where ]);

$this->joins[] = compact('type', 'table', 'where');
}
Expand Down
12 changes: 6 additions & 6 deletions src/RequestInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,29 +70,29 @@ public function from(string $table);
* @param string $table Nom de la table à joindre.
* @param string|\Closure $column Nom de la colonne d'une des tables précédentes
* ou une closure pour affiner les conditions.
* @param string|null $operator Opérateur logique ou null pour une closure.
* @param string|null $value Valeur
* @param string $operator Opérateur logique ou null pour une closure.
* @param scalar|null $value Valeur
* ou une colonne de la table jointe (au format nom_table.colonne)
* ou null pour une closure.
*
* @return $this
*/
public function leftJoin(string $table, $column, ?string $operator = null, ?string $value = null);
public function leftJoin(string $table, $column, string $operator = '', $value = null);

/**
* Enregistre une jointure droite.
*
* @param string $table Nom de la table à joindre
* @param string|\Closure $column Nom de la colonne d'une des tables précédentes
* ou une closure pour affiner les conditions.
* @param string|null $operator Opérateur logique ou null pour une closure.
* @param string|null $value Valeur
* @param string $operator Opérateur logique ou null pour une closure.
* @param scalar|null $value Valeur
* ou une colonne de la table jointe (au format nom_table.colonne)
* ou null pour une closure.
*
* @return $this
*/
public function rightJoin(string $table, $column, ?string $operator = null, ?string $value = null);
public function rightJoin(string $table, $column, string $operator = '', $value = null);

/**
* Enregistre une limitation et un décalage au retour de la requête.
Expand Down
6 changes: 3 additions & 3 deletions src/Where.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public function __toString(): string
);

break;
case 'whereCallback':
case 'whereGroup':
$output .= sprintf('%s(%s) ', $not, $where[ 'value' ]);

break;
Expand Down Expand Up @@ -121,7 +121,7 @@ public function execute(array $row): bool
$output = true;
foreach ($this->where as $key => $value) {
/* Si la clause est standard ou une sous clause. */
$predicate = $value[ 'type' ] === 'whereCallback'
$predicate = $value[ 'type' ] === 'whereGroup'
? $value[ 'value' ]->execute($row)
: self::predicate($row[ $value[ 'column' ] ], $value[ 'condition' ], $value[ 'value' ]);
/* Si la clause est inversé. */
Expand Down Expand Up @@ -161,7 +161,7 @@ public function executeJoin(array $row, array &$rowTable): bool
foreach ($this->where as $key => $value) {
$predicate = true;

if ($value[ 'type' ] === 'whereCallback') {
if ($value[ 'type' ] === 'whereGroup') {
$predicate = $value[ 'value' ]->executeJoin($row, $rowTable);
} else {
$val = $rowTable[ self::getColumn($value[ 'value' ]) ];
Expand Down
82 changes: 49 additions & 33 deletions src/WhereHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,27 +49,21 @@ class WhereHandler
* Ajoute une condition simple pour la requête.
* Si la valeur du champ est égal (non égale, supérieur à, ...) par rapport à une valeur.
*
* @param \Closure|string $column Sous condition ou une colonne.
* @param null|string $operator Type de condition.
* @param null|numeric|string $value Valeur de teste.
* @param string $bool Porte logique de la condition (and|or).
* @param bool $not Inverse la condition.
* @param string $column Nom d'une colonne.
* @param string $operator Type de condition.
* @param null|scalar $value Valeur de teste.
* @param string $bool Porte logique de la condition (and|or).
* @param bool $not Inverse la condition.
*
* @throws OperatorNotFound The condition is not exist.
*/
public function where(
$column,
?string $operator = null,
$value = null,
string $column,
string $operator,
$value,
string $bool = self::EXP_AND,
bool $not = false
): self {
if ($column instanceof \Closure) {
$this->whereCallback($column, $bool, $not);

return $this;
}

$condition = $this->filterOperator($operator);

if (in_array($condition, [ 'like', 'ilike', 'not like', 'not ilike' ])) {
Expand Down Expand Up @@ -97,10 +91,9 @@ public function where(
/**
* Alias inverse de la fonction where().
*
* @param \Closure|string $column Sous condition ou une colonne.
* @param null|numeric|string $value Valeur de teste.
* @param null|scalar $value Valeur de teste.
*/
public function notWhere($column, ?string $operator = null, $value = null): self
public function notWhere(string $column, string $operator, $value): self
{
$this->where($column, $operator, $value, self::EXP_AND, true);

Expand All @@ -110,10 +103,9 @@ public function notWhere($column, ?string $operator = null, $value = null): self
/**
* Alias avec la porte logique 'OR' de la fonction where().
*
* @param \Closure|string $column Sous condition ou une colonne.
* @param null|numeric|string $value Valeur de teste.
* @param null|scalar $value Valeur de teste.
*/
public function orWhere($column, ?string $operator = null, $value = null): self
public function orWhere(string $column, string $operator, $value): self
{
$this->where($column, $operator, $value, self::EXP_OR);

Expand All @@ -123,10 +115,9 @@ public function orWhere($column, ?string $operator = null, $value = null): self
/**
* Alias inverse avec la porte logique 'OR' de la fonction where().
*
* @param \Closure|string $column Sous condition ou une colonne.
* @param null|numeric|string $value Valeur de teste.
* @param null|scalar $value Valeur de teste.
*/
public function orNotWhere($column, ?string $operator = null, $value = null): self
public function orNotWhere(string $column, string $operator, $value): self
{
$this->where($column, $operator, $value, self::EXP_OR, true);

Expand Down Expand Up @@ -358,13 +349,13 @@ public function orNotRegex(string $column, string $pattern): self
/**
* Ajoute une sous-condition pour la requête.
*/
protected function whereCallback(
callable $column,
public function whereGroup(
\Closure $callable,
string $bool = self::EXP_AND,
bool $not = false
): void {
$where = new Where();
call_user_func_array($column, [ &$where ]);
call_user_func_array($callable, [ &$where ]);

$this->where[] = [
'type' => __FUNCTION__,
Expand All @@ -375,6 +366,36 @@ protected function whereCallback(
];
}

/**
* Alias inverse de la fonction whereGroup().
*/
public function notWhereGroup(\Closure $callable): self
{
$this->whereGroup($callable, self::EXP_AND, true);

return $this;
}

/**
* Alias avec la porte logique 'OR' de la fonction whereGroup().
*/
public function orWhereGroup(\Closure $callable): self
{
$this->whereGroup($callable, self::EXP_OR);

return $this;
}

/**
* Alias inverse avec la porte logique 'OR' de la fonction whereGroup().
*/
public function orNotWhereGroup(\Closure $callable): self
{
$this->whereGroup($callable, self::EXP_OR, true);

return $this;
}

/**
* Ajoute une condition like pour la requête.
*
Expand Down Expand Up @@ -413,19 +434,14 @@ protected function like(
/**
* Filtre l'opérateur.
*
* @param string|null $operator
* @param string $operator
*
* @throws QueryException
* @throws OperatorNotFound
*
* @return string
*/
private function filterOperator(?string $operator): string
private function filterOperator(string $operator): string
{
if ($operator === null) {
throw new QueryException();
}

$condition = strtolower($operator);

if (!in_array($condition, self::CONDITION)) {
Expand Down

0 comments on commit 178c549

Please sign in to comment.