Skip to content

Commit

Permalink
traits/table
Browse files Browse the repository at this point in the history
  • Loading branch information
roboxon committed Oct 23, 2023
1 parent 40de199 commit f75b2a5
Show file tree
Hide file tree
Showing 7 changed files with 342 additions and 24 deletions.
38 changes: 38 additions & 0 deletions src/Database/SqlEnumCondition.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

declare(strict_types=1);

/*
* This file is part of PHP CS Fixer.
* (c) Fabien Potencier <[email protected]>
* Dariusz Rumiński <[email protected]>
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

enum SqlEnumCondition: string
{
case Equal = ' = ';

case Bigger = ' > ';

case Less = ' < ';

case BiggerEqual = ' >= ';

case LessEqual = ' =< ';

case IsNull = ' IS NULL ';

case NotNull = 'IS NOT NULL';

case Not = ' != ';

case Like = ' LIKE ';

case Descending = ' DESC ';

case Ascending = ' ASC ';

case Between = ' BETWEEN ';
}
23 changes: 17 additions & 6 deletions src/core/BaseTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,26 @@ public function __construct(?string $connectionName = null)
{
parent::__construct(PdoConnManager::connect($connectionName));
}
public static function safeAssociArrayToObject(array $array, object $TargetClass): object

private function fetchObject(array $row)
{
$instance = new $TargetClass();
foreach ($instance as $key => $value) {
if (isset($array[$key])) {
$instance->$key = $array[$key];
foreach($row as $key => $value)
{
if(property_exists($this, $key)){
$this->$key = $value;
}
}
return $instance;
}

private function fetchAllObjects(array $rows):array
{
$objects = [];
foreach($rows as $row)
{
$obj = new $this();
$objects[] = $obj->fetchObject($row);
}
return $objects;
}

}
Empty file removed src/test
Empty file.
29 changes: 29 additions & 0 deletions src/traits/table/RemoveTrait.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
<?php
namespace GemFramework\Traits\Table;

/**
* @method remove
* @method RemoveConditional
*/
trait removeTrait
{
/**
Expand Down Expand Up @@ -27,4 +32,28 @@ public function remove(): int|null

return null;
}

/**
* NOTE: remove Object compleetly from Database.
* @ in case of success return count removed items
* @Attention: remove Object compleetly from Database
*/
public function RemoveConditional(string $whereColumn, mixed $whereValue, ?string $secondWhereColumn = null, mixed $secondWhereValue = null): int|null
{
$table = $this->setTable();
if(!$table)
{
$this->setError('table is not setted in function setTable');
return null;
}
$query = "DELETE FROM {$table} WHERE {$whereColumn} = :{$whereColumn}";
if ($secondWhereColumn) {
$query .= " AND {$secondWhereColumn} = :{$secondWhereColumn}";
}
$arrayBind[':'.$whereColumn] = $whereValue;
if ($secondWhereColumn) {
$arrayBind[':'.$secondWhereColumn] = $secondWhereValue;
}
return $this->deleteQuery($query, $arrayBind);
}
}
63 changes: 63 additions & 0 deletions src/traits/table/SafeDeleteTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php
namespace GemFramework\Traits\Table;

/**
* @method delete()
* @method restore()
*/
trait SafeDeleteTrait
{
/**
* @param int|null $id
* @return int|null
* @description : if id = null then use $this->id
* return 1 if successful and null if not
*/
public function delete(int $id = null): int|null
{
$table = $this->setTable();
if(!$table)
{
$this->setError('table is not setted in function setTable');
return null;
}
if (!$id) {
$id = $this->id;
}
if(!$id || $id < 1)
{
$this->setError('property id does existed or not setted in object');
return null;
}
$query = "UPDATE $table SET deleted_at = NOW() WHERE id = :id";

return $this->updateQuery($query, [':id' => $id]);
}

/**
* @param int|null $id
* @return int|null
* @description : if id = null then use $this->id
* return 1 if successful and null if not
*/
public function restore(int $id = null): int|null
{
$table = $this->setTable();
if(!$table)
{
$this->setError('table is not setted in function setTable');
return null;
}
if (!$id) {
$id = $this->id;
}
if(!$id || $id < 1)
{
$this->setError('property id does existed or not setted in object');
return null;
}
$query = "UPDATE {$this->table} SET deleted_at = NULL WHERE id = :id";

return $this->updateQuery($query, []);
}
}
165 changes: 149 additions & 16 deletions src/traits/table/SelectTrait.php
Original file line number Diff line number Diff line change
@@ -1,37 +1,59 @@
<?php
namespace GemFramework\Traits\Table;

/**
* @method id
* @method ids
* @method firstRows
* @method lastRows
* @method columnSelect
* select object with given id or array of objects by giving ids
*/
trait SelectTrait{

public function getById(int $id): void
/**
* @param int $id
* @return bool
* set $this value and return true if found, false otherwise
*/
public function id(int $id): bool
{
$table = $this->setTable();
if(!$table)
{
$this->setError('table is not setted in function setTable');
return false;
}
$row = $this->selectQuery("SELECT * FROM $table WHERE id = :id", [':id' => $id]);
if (null !== $row && isset($row[0]) && \is_array($row[0])) {
$this->fetchObject($row[0]);
return true;
} else {
$this->setError('Object with given id dose not Existed');
}
return false;
}


/**
* @param array<int> $ids
*
* @return null|array<$this>
* in case of failure return null
*/
public function ids(array $ids): array|null
{
$table = $this->setTable();
if(!$table)
{
$this->setError('table is not setted in function setTable');
return false;
}
$stringIds = '';
foreach ($ids as $id) {
$stringIds .= $id.',';
}
$stringIds = rtrim($stringIds, ',');
$query = "SELECT * FROM {$this->table} WHERE id IN ({$stringIds})";
$query = "SELECT * FROM {$table} WHERE id IN ({$stringIds})";
$queryResult = $this->selectQuery($query, []);
if(is_array($queryResult))
{
Expand All @@ -41,25 +63,136 @@ public function ids(array $ids): array|null
}


private function fetchObject(array $row)
{
foreach($row as $key => $value)
/**
* @return null|array<$this>
*/
public function firstRows(
int $countRows,
string $whereColumn,
\SqlEnumCondition $whereCondition,
mixed $whereValue,
?string $orderByColumnName = null
): null|array {
$table = $this->setTable();
if(!$table)
{
if(property_exists($this, $key)){
$this->$key = $value;
}
$this->setError('table is not setted in function setTable');
return false;
}
$arrayBindValue = [];
$where = " WHERE {$whereColumn} ".$whereCondition->value." :{$whereColumn}";
$arrayBindValue[':'.$whereColumn] = $whereValue;
$query = ($orderByColumnName) ?
"SELECT * FROM {$table} ORDER BY {$orderByColumnName} {$where} LIMIT {$countRows}" :
"SELECT * FROM {$table} {$where} LIMIT {$countRows}";
$queryResult = $this->selectQuery($query, $arrayBindValue);
if(is_array($queryResult))
{
return $this->fetchAllObjects($queryResult);
}
return null;
}

private function fetchAllObjects(array $rows):array

/**
* @return null|array<$this>
*/
public function lastRows(int $countRows, string $orderByColumnName, ?string $whereColumn = null, ?\SqlEnumCondition $whereCondition = null, int|string|bool $whereValue = null): null|array
{
$objects = [];
foreach($rows as $row)
{
$obj = new $this;
$objects[] = $obj->fetchObject($row);
$table = $this->setTable();
if(!$table)
{
$this->setError('table is not setted in function setTable');
return false;
}
$arrayBindValue = [];
$where = '';
if (null !== $whereColumn && null !== $whereCondition) {
$where = " WHERE {$whereColumn} ".$whereCondition->value;
}
if (null !== $whereValue) {
$where .= " :{$whereValue}";
$arrayBindValue[':'.$whereValue] = $whereValue;
}

$query = "SELECT * FROM {$table} ORDER BY {$orderByColumnName} DESC {$where} LIMIT {$countRows}";

$queryResult = $this->selectQuery($query, $arrayBindValue);
if(is_array($queryResult))
{
return $this->fetchAllObjects($queryResult);
}
return null;
}


/**
* @return null|array<$this>
*/
public function columnSelect(
?string $firstColumn = null,
?\SqlEnumCondition $firstCondition = null,
mixed $firstValue = null,
?string $secondColumn = null,
?\SqlEnumCondition $secondCondition = null,
mixed $secondValue = null,
?string $orderBy = null,
?string $ASC_DES = null,
?int $limit_count = null,
?int $limit_offset = null,
?bool $isDel = null,
?bool $deactives = null,
?bool $actives = null

): null|array {
$table = $this->setTable();
if(!$table)
{
$this->setError('table is not setted in function setTable');
return false;
}
$limit = '';
$arrayBindValue = [];

$isDel ? ' AND deleted_at IS NOT NULL ' : '';
$actives ? ' AND isActive = 1 ' : '';

$deactives ? ' AND isActive IS NOT 1 ' : '';
if ($orderBy) {
$orderBy = " ORDER BY {$orderBy} {$ASC_DES}";
}
if ($limit_count) {
$limit = " LIMIT {$limit_count}";
if ($limit_offset) {
$limit = " LIMIT {$limit_offset} , {$limit_count}";
}
return $objects;
}

$query = "SELECT * FROM {$table} ";
$firstColumnQuery = null;
$secondColumnQuery = null;

if(null !== $firstColumn && null !== $firstCondition)
{
$firstValue = (' LIKE ' === (string) $firstCondition->value) ? '%'.$firstValue : $firstValue;
$firstColumnQuery = " {$firstColumn} {$firstCondition->value} :{$firstColumn}";
$arrayBindValue[':'.$firstColumn] = $firstValue;
}

if (null !== $secondColumn && null !== $secondCondition) {
$secondColumnQuery = " AND {$secondColumn} {$secondCondition->value} :{$secondColumn}";
$secondValue = (' LIKE ' === $secondCondition->value) ? '%'.$secondValue : $secondValue;
$arrayBindValue[':'.$secondColumn] = $secondValue;
}
$query .= "WHERE {$firstColumnQuery} {$secondColumnQuery} {$isDel} {$actives} {$deactives} {$orderBy} {$limit}";
$query = trim($query);
//echo $query;
$queryResult = $this->selectQuery($query, $arrayBindValue);
if(is_array($queryResult))
{
return $this->fetchAllObjects($queryResult);
}
return null;
}

}
Loading

0 comments on commit f75b2a5

Please sign in to comment.