Compare commits
5 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
e390bc262e | ||
|
|
2afc439354 | ||
|
|
2cfc92584c | ||
|
|
ac572cc92b | ||
|
|
448128d228 |
@@ -10,12 +10,12 @@ Stable version
|
||||
composer require rmphp/storage
|
||||
```
|
||||
```bash
|
||||
composer require rmphp/storage:"^3.0"
|
||||
composer require rmphp/storage:"^5.0"
|
||||
```
|
||||
|
||||
|
||||
Dev version contains the latest changes
|
||||
|
||||
```bash
|
||||
composer require rmphp/storage:"3.x-dev"
|
||||
composer require rmphp/storage:"5.x-dev"
|
||||
```
|
||||
|
||||
198
src/AbstractMysqlRepository.php
Normal file
198
src/AbstractMysqlRepository.php
Normal file
@@ -0,0 +1,198 @@
|
||||
<?php
|
||||
|
||||
namespace Rmphp\Storage;
|
||||
|
||||
use Rmphp\Storage\Entity\EntityInterface;
|
||||
use Rmphp\Storage\Mysql\MysqlRepositoryInterface;
|
||||
use Rmphp\Storage\Mysql\MysqlResultData;
|
||||
use Rmphp\Storage\Mysql\MysqlStorageInterface;
|
||||
|
||||
abstract class AbstractMysqlRepository extends AbstractRepository implements MysqlRepositoryInterface {
|
||||
|
||||
public const DEBUG = false;
|
||||
public const TABLE = null;
|
||||
public const ENTITY = null;
|
||||
|
||||
private string $table;
|
||||
private string $entity;
|
||||
private bool $debug;
|
||||
|
||||
public function __construct(
|
||||
public readonly MysqlStorageInterface $mysql
|
||||
) {}
|
||||
|
||||
|
||||
/** @inheritDoc */
|
||||
public function createFromResult(string $class, bool|MysqlResultData $result, callable $function = null): mixed {
|
||||
if($result instanceof MysqlResultData) {
|
||||
$val = (isset($function)) ? $function($result->fetchOne()) : $result->fetchOne();
|
||||
$out = $this->createFromData($class, $val);
|
||||
}
|
||||
return $out ?? null;
|
||||
}
|
||||
|
||||
|
||||
/** @inheritDoc */
|
||||
public function createListFromResult(string $class, bool|MysqlResultData $result, callable $function = null): array {
|
||||
if($result instanceof MysqlResultData) {
|
||||
foreach($result->fetch() as $resultValue) {
|
||||
$val = (isset($function)) ? $function($resultValue) : $resultValue;
|
||||
$out[] = $this->createFromData($class, $val);
|
||||
}
|
||||
}
|
||||
return $out ?? [];
|
||||
}
|
||||
|
||||
|
||||
/** @inheritDoc */
|
||||
public function getEntityById(int $id, string $table = null): mixed {
|
||||
if(!isset($table)) $table = $this->getTable();
|
||||
if($result = $this->mysql->findById($table, $id)) $out = $this->createFromData($this->getEntityClass(), $result);
|
||||
return $out ?? null;
|
||||
}
|
||||
|
||||
|
||||
/** @inheritDoc */
|
||||
public function saveEntity(EntityInterface $object, string $table = null) : mixed {
|
||||
if(!isset($table)) $table = $this->getTable();
|
||||
$in = $this->getProperties($object, function ($value){
|
||||
return (is_string($value)) ? $this->mysql->escapeStr($value) : $value;
|
||||
});
|
||||
if($this->getDebug()) {$this->debug($object, $in, $table); exit;}
|
||||
try {
|
||||
if (!empty($object->getId()) && !empty($this->mysql->findById($table, $object->getId()))) {
|
||||
$this->mysql->updateById($table, $in, $object->getId());
|
||||
return $object->getId();
|
||||
} else {
|
||||
$this->mysql->insert($table, $in);
|
||||
return (is_string($object->getId())) ? $object->getId() : $this->mysql->mysql()->insert_id;
|
||||
}
|
||||
} catch (\Throwable $throwable) {throw new RepositoryException($throwable->getMessage());}
|
||||
}
|
||||
|
||||
|
||||
/** @inheritDoc */
|
||||
public function saveData(array $data, string $table = null, string $primaryKey = 'id') : mixed {
|
||||
if(!isset($table)) $table = $this->getTable();
|
||||
$in = array_map(function ($value){
|
||||
return (is_string($value)) ? $this->mysql->escapeStr($value) : $value;
|
||||
}, $data);
|
||||
if($this->getDebug()) {$this->debug($data, $in, $table); exit;}
|
||||
try {
|
||||
if (!empty($data[$primaryKey]) && !empty($this->mysql->findById($table, $data[$primaryKey], $primaryKey))) {
|
||||
$this->mysql->updateById($table, $in, $data[$primaryKey]);
|
||||
return $data[$primaryKey];
|
||||
} else {
|
||||
$this->mysql->insert($table, $in);
|
||||
return (is_string($data[$primaryKey])) ? $data[$primaryKey] : $this->mysql->mysql()->insert_id;
|
||||
}
|
||||
} catch (\Throwable $throwable) {throw new RepositoryException($throwable->getMessage());}
|
||||
}
|
||||
|
||||
|
||||
/** @inheritDoc */
|
||||
public function saveEntityGroup(array $objects, string $table = null): array {
|
||||
try{
|
||||
$this->mysql->mysql()->begin_transaction();
|
||||
foreach($objects as $object) $id[] = $this->saveEntity($object, $table);
|
||||
$this->mysql->mysql()->commit();
|
||||
return $id ?? [];
|
||||
}
|
||||
catch (\Exception $exception){
|
||||
$this->mysql->mysql()->rollback();
|
||||
throw new RepositoryException($exception->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/** @inheritDoc */
|
||||
public function saveDataGroup(array $objects, string $table = null, string $primaryKey = 'id'): array {
|
||||
try{
|
||||
$this->mysql->mysql()->begin_transaction();
|
||||
foreach($objects as $object) $id[] = $this->saveData($object, $table, $primaryKey);
|
||||
$this->mysql->mysql()->commit();
|
||||
return $id ?? [];
|
||||
}
|
||||
catch (\Exception $exception){
|
||||
$this->mysql->mysql()->rollback();
|
||||
throw new RepositoryException($exception->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/** @inheritDoc */
|
||||
public function deleteEntity(EntityInterface $object, string $table = null) : bool {
|
||||
if(!isset($table)) $table = $this->getTable();
|
||||
if(!empty($object->getId())){
|
||||
return $this->mysql->deleteById($table, $object->getId());
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
/** @inheritDoc */
|
||||
public function getStorageLogs() : array {
|
||||
return $this->mysql->getLogs();
|
||||
}
|
||||
|
||||
|
||||
/** @inheritDoc */
|
||||
public function setTable(string $table) : void {
|
||||
$this->table = $table;
|
||||
}
|
||||
|
||||
|
||||
/** @inheritDoc */
|
||||
public function setEntity(string $entity) : void {
|
||||
$this->entity = $entity;
|
||||
}
|
||||
|
||||
|
||||
/** @inheritDoc */
|
||||
public function setDebug(bool $debug) : void {
|
||||
$this->debug = $debug;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return string
|
||||
* @throws RepositoryException
|
||||
*/
|
||||
private function getTable() : string {
|
||||
if(!empty($this->table)) return $this->table;
|
||||
if(!empty(static::TABLE)) return static::TABLE;
|
||||
throw new RepositoryException("Имя таблицы не задано");
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return string
|
||||
* @throws RepositoryException
|
||||
*/
|
||||
private function getEntityClass() : string {
|
||||
if(!empty($this->entity)) return $this->entity;
|
||||
if(!empty(static::ENTITY)) return static::ENTITY;
|
||||
throw new RepositoryException("Не указан объект");
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
private function getDebug(): bool {
|
||||
if(!empty($this->debug)) return $this->debug;
|
||||
if(!empty(static::DEBUG)) return static::DEBUG;
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param ...$arg
|
||||
* @return void
|
||||
*/
|
||||
protected function debug(...$arg) : void {
|
||||
if(function_exists('dd')) dd(...$arg);
|
||||
if(function_exists('vdd')) vdd(...$arg);
|
||||
var_dump(...$arg);
|
||||
}
|
||||
|
||||
}
|
||||
102
src/AbstractRepository.php
Normal file
102
src/AbstractRepository.php
Normal file
@@ -0,0 +1,102 @@
|
||||
<?php
|
||||
|
||||
namespace Rmphp\Storage;
|
||||
|
||||
use ReflectionClass;
|
||||
use ReflectionException;
|
||||
use ReflectionProperty;
|
||||
use Rmphp\Storage\Entity\ValueObjectInterface;
|
||||
|
||||
abstract class AbstractRepository implements RepositoryInterface {
|
||||
|
||||
static array $classes = [];
|
||||
|
||||
/** @inheritDoc */
|
||||
public function createFromData(string $class, $data) : object {
|
||||
try {
|
||||
if(!isset(static::$classes[$class])) static::$classes[$class] = new ReflectionClass($class);
|
||||
return $this->fillObject(static::$classes[$class], new $class, $data);
|
||||
}
|
||||
catch (ReflectionException $exception) {
|
||||
throw new RepositoryException($exception->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/** @inheritDoc */
|
||||
public function updateFromData(object $object, array $data) : object {
|
||||
try {
|
||||
$class = get_class($object);
|
||||
if(!isset(static::$classes[$class])) static::$classes[$class] = new ReflectionClass($class);
|
||||
return $this->fillObject(static::$classes[$class], clone $object, $data, true);
|
||||
}
|
||||
catch (RepositoryException|ReflectionException $exception) {
|
||||
throw new RepositoryException($exception->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param object $object
|
||||
* @param callable|null $method
|
||||
* @return array
|
||||
* @throws RepositoryException
|
||||
*/
|
||||
public function getProperties(object $object, callable $method = null) : array {
|
||||
try{
|
||||
$class = get_class($object);
|
||||
if(!isset(static::$classes[$class])) static::$classes[$class] = new ReflectionClass($class);
|
||||
/** @var ReflectionProperty $property */
|
||||
foreach(static::$classes[$class]->getProperties() as $property){
|
||||
if(!$property->isInitialized($object)) continue;
|
||||
if(static::$classes[$class]->hasMethod('get'.ucfirst($property->getName()))){
|
||||
$fieldValue[$property->getName()] = $object->{'get'.ucfirst($property->getName())}($property->getValue($object));
|
||||
}
|
||||
elseif($property->hasType() && class_exists($property->getType()->getName()) && $property->getValue($object) instanceof ValueObjectInterface){
|
||||
$fieldValue[$property->getName()] = $property->getValue($object)->get();
|
||||
}
|
||||
elseif(is_bool($property->getValue($object))){
|
||||
$fieldValue[$property->getName()] = (int) $property->getValue($object);
|
||||
}
|
||||
else $fieldValue[$property->getName()] = $property->getValue($object);
|
||||
$fieldNameSnakeCase = strtolower(preg_replace("'([A-Z])'", "_$1", $property->getName()));
|
||||
if(false !== $fieldValue[$property->getName()]) $out[$fieldNameSnakeCase] = $fieldValue[$property->getName()];
|
||||
}
|
||||
return (isset($method)) ? array_map($method, $out ?? []) : $out ?? [];
|
||||
}
|
||||
catch (ReflectionException $exception) {
|
||||
throw new RepositoryException($exception->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param ReflectionClass $class
|
||||
* @param object $object
|
||||
* @param array $data
|
||||
* @param bool $update
|
||||
* @return mixed
|
||||
* @throws RepositoryException
|
||||
*/
|
||||
private function fillObject(ReflectionClass $class, object $object, array $data, bool $update = false) : mixed {
|
||||
try {
|
||||
foreach($class->getProperties() as $property){
|
||||
if($update && !array_key_exists($property->getName(), $data) && !array_key_exists(strtolower(preg_replace("'([A-Z])'", "_$1", $property->getName())), $data)) continue;
|
||||
// data[propertyName] ?? data[property_name] ?? null
|
||||
$value = $data[$property->getName()] ?? $data[strtolower(preg_replace("'([A-Z])'", "_$1", $property->getName()))] ?? null;
|
||||
// если есть внутренний метод (приоритетная обработка)
|
||||
if($class->hasMethod('set'.ucfirst($property->getName()))) $object->{'set'.ucfirst($property->getName())}($value);
|
||||
// Если тип свойства класс (valueObject)
|
||||
elseif($property->hasType() && class_exists($property->getType()->getName())) $object->{$property->getName()} = (is_object($value)) ? $value : new ($property->getType()->getName())($value);
|
||||
// если значения не пустое
|
||||
elseif(isset($value)) $object->{$property->getName()} = $value;
|
||||
// если значения может быть пустое
|
||||
elseif($property->getType()->allowsNull()) $object->{$property->getName()} = null;
|
||||
}
|
||||
return $object;
|
||||
}
|
||||
catch (ReflectionException $exception) {
|
||||
throw new RepositoryException($exception->getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
14
src/Entity/AbstractEntity.php
Normal file
14
src/Entity/AbstractEntity.php
Normal file
@@ -0,0 +1,14 @@
|
||||
<?php
|
||||
|
||||
namespace Rmphp\Storage\Entity;
|
||||
|
||||
abstract class AbstractEntity implements EntityInterface {
|
||||
|
||||
/**
|
||||
* @return int|null
|
||||
*/
|
||||
public function getId(): mixed {
|
||||
return (isset($this->id)) ? (($this->id instanceof ValueObjectInterface) ? $this->id->get() : $this->id) : null;
|
||||
}
|
||||
|
||||
}
|
||||
18
src/Entity/EntityInterface.php
Normal file
18
src/Entity/EntityInterface.php
Normal file
@@ -0,0 +1,18 @@
|
||||
<?php
|
||||
/**
|
||||
* Created by PhpStorm.
|
||||
* User: Zuev Yuri
|
||||
* Date: 23.04.2024
|
||||
* Time: 3:58
|
||||
*/
|
||||
|
||||
namespace Rmphp\Storage\Entity;
|
||||
|
||||
interface EntityInterface {
|
||||
|
||||
/**
|
||||
* @return int|null
|
||||
*/
|
||||
public function getId(): mixed;
|
||||
|
||||
}
|
||||
10
src/Entity/ValueObjectInterface.php
Normal file
10
src/Entity/ValueObjectInterface.php
Normal file
@@ -0,0 +1,10 @@
|
||||
<?php
|
||||
|
||||
namespace Rmphp\Storage\Entity;
|
||||
|
||||
interface ValueObjectInterface {
|
||||
|
||||
public function get();
|
||||
public function __toString(): string;
|
||||
|
||||
}
|
||||
@@ -1,8 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Rmphp\Storage\Mysql\Exception;
|
||||
|
||||
|
||||
class MysqlException extends \Exception {
|
||||
|
||||
}
|
||||
108
src/Mysql/MysqlRepositoryInterface.php
Normal file
108
src/Mysql/MysqlRepositoryInterface.php
Normal file
@@ -0,0 +1,108 @@
|
||||
<?php
|
||||
/**
|
||||
* Created by PhpStorm.
|
||||
* User: Zuev Yuri
|
||||
* Date: 12.01.2025
|
||||
* Time: 21:44
|
||||
*/
|
||||
|
||||
namespace Rmphp\Storage\Mysql;
|
||||
|
||||
use Rmphp\Storage\Entity\EntityInterface;
|
||||
use Rmphp\Storage\RepositoryException;
|
||||
use Rmphp\Storage\RepositoryInterface;
|
||||
|
||||
interface MysqlRepositoryInterface extends RepositoryInterface {
|
||||
|
||||
/**
|
||||
* @param string $class
|
||||
* @param bool|MysqlResultData $result
|
||||
* @param callable|null $function
|
||||
* @return mixed
|
||||
* @throws RepositoryException
|
||||
*/
|
||||
public function createFromResult(string $class, bool|MysqlResultData $result, callable $function = null): mixed;
|
||||
|
||||
/**
|
||||
* @param string $class
|
||||
* @param bool|MysqlResultData $result
|
||||
* @param callable|null $function
|
||||
* @return array
|
||||
* @throws RepositoryException
|
||||
*/
|
||||
public function createListFromResult(string $class, bool|MysqlResultData $result, callable $function = null): array;
|
||||
|
||||
/**
|
||||
* @param int $id
|
||||
* @param string|null $table
|
||||
* @return mixed
|
||||
* @throws RepositoryException
|
||||
*/
|
||||
public function getEntityById(int $id, string $table = null): mixed;
|
||||
|
||||
/**
|
||||
* @param EntityInterface $object
|
||||
* @param string|null $table
|
||||
* @return mixed
|
||||
* @throws RepositoryException
|
||||
*/
|
||||
public function saveEntity(EntityInterface $object, string $table = null) : mixed;
|
||||
|
||||
/**
|
||||
* @param array $data
|
||||
* @param string|null $table
|
||||
* @param string $primaryKey
|
||||
* @return mixed
|
||||
* @throws RepositoryException
|
||||
*/
|
||||
public function saveData(array $data, string $table = null, string $primaryKey = 'id') : mixed;
|
||||
|
||||
/**
|
||||
* @param array $objects
|
||||
* @param string|null $table
|
||||
* @return array
|
||||
* @throws RepositoryException
|
||||
*/
|
||||
public function saveEntityGroup(array $objects, string $table = null): array;
|
||||
|
||||
/**
|
||||
* @param array $objects
|
||||
* @param string|null $table
|
||||
* @param string $primaryKey
|
||||
* @return array
|
||||
* @throws RepositoryException
|
||||
*/
|
||||
public function saveDataGroup(array $objects, string $table = null, string $primaryKey = 'id'): array;
|
||||
|
||||
/**
|
||||
* @param EntityInterface $object
|
||||
* @param string|null $table
|
||||
* @return bool
|
||||
* @throws RepositoryException
|
||||
*/
|
||||
public function deleteEntity(EntityInterface $object, string $table = null) : bool;
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function getStorageLogs() : array;
|
||||
|
||||
/**
|
||||
* @param string $table
|
||||
* @return void
|
||||
*/
|
||||
public function setTable(string $table) : void;
|
||||
|
||||
/**
|
||||
* @param string $entity
|
||||
* @return void
|
||||
*/
|
||||
public function setEntity(string $entity) : void;
|
||||
|
||||
/**
|
||||
* @param bool $debug
|
||||
* @return void
|
||||
*/
|
||||
public function setDebug(bool $debug) : void;
|
||||
|
||||
}
|
||||
@@ -3,7 +3,7 @@
|
||||
namespace Rmphp\Storage\Mysql;
|
||||
|
||||
|
||||
class MysqlStorageData {
|
||||
class MysqlResultData {
|
||||
|
||||
private ?\mysqli_result $result;
|
||||
private array $arrayData = [];
|
||||
@@ -39,11 +39,11 @@ class MysqlStorage implements MysqlStorageInterface {
|
||||
try{
|
||||
$result = $this->mysqli->query($sql);
|
||||
if($this->mysqli->errno) throw new Exception();
|
||||
$this->addLog("OK - ".$sql);
|
||||
$this->addLog("OK - $sql");
|
||||
return $result;
|
||||
}
|
||||
catch (Exception $exception){
|
||||
$this->addLog("Err - SQL: ".$sql." | error: ".$this->mysqli->error);
|
||||
$this->addLog("Err - SQL: $sql | error: ".$this->mysqli->error);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -72,14 +72,15 @@ class MysqlStorage implements MysqlStorageInterface {
|
||||
}
|
||||
|
||||
/** @inheritDoc */
|
||||
public function updateById(string $table, array $data, int $id, array $modifier = []) : bool {
|
||||
public function updateById(string $table, array $data, mixed $id, array $modifier = []) : bool {
|
||||
$chunks = $this->getUpdateValue($data);
|
||||
$sql = "update low_priority ".implode(" ", $modifier)." ".$this->escapeStr($table)." set ".implode(",", $chunks)." where id = '".$id."'";
|
||||
$id = (is_numeric($id)) ? (int) $id : $this->escapeStr($id);
|
||||
$sql = "update low_priority ".implode(" ", $modifier)." ".$this->escapeStr($table)." set ".implode(",", $chunks)." where id = '$id'";
|
||||
return $this->query($sql);
|
||||
}
|
||||
|
||||
/** @inheritDoc */
|
||||
public function updateByParam(string $table, array $data, string $case, array $modifier = []) : bool {
|
||||
public function updateByParam(string $table, array $data, string $case, array $modifier = []) : bool {
|
||||
$chunks = $this->getUpdateValue($data);
|
||||
$sql = "update low_priority ".implode(" ", $modifier)." ".$this->escapeStr($table)." set ". implode(",", $chunks)." where ".$case;
|
||||
return $this->query($sql);
|
||||
@@ -94,8 +95,9 @@ class MysqlStorage implements MysqlStorageInterface {
|
||||
|
||||
|
||||
/** @inheritDoc */
|
||||
public function deleteById(string $table, int $id) : bool {
|
||||
$sql = "delete low_priority from ".$this->escapeStr($table)." where id='.$id.'";
|
||||
public function deleteById(string $table, mixed $id) : bool {
|
||||
$id = (is_numeric($id)) ? (int) $id : $this->escapeStr($id);
|
||||
$sql = "delete low_priority from ".$this->escapeStr($table)." where id='$id'";
|
||||
// возвращаем число затронутых строк/false
|
||||
return $this->query($sql);
|
||||
}
|
||||
@@ -109,7 +111,7 @@ class MysqlStorage implements MysqlStorageInterface {
|
||||
|
||||
|
||||
/** @inheritDoc */
|
||||
public function find(string $sql, int $ln=0, int $numPage=1, int $count=0): bool|MysqlStorageData {
|
||||
public function find(string $sql, int $ln=0, int $numPage=1, int $count=0): bool|MysqlResultData {
|
||||
|
||||
if ($ln > 1) {
|
||||
$cnts = (!empty($count)) ? $count : $this->query($sql)->num_rows;
|
||||
@@ -124,25 +126,25 @@ class MysqlStorage implements MysqlStorageInterface {
|
||||
$result = $this->query($sql.$limit);
|
||||
if (!$result || $result->num_rows == 0) return false;
|
||||
|
||||
$data = new MysqlStorageData($result);
|
||||
$data = new MysqlResultData($result);
|
||||
$data->count = $cnts ?? 0;
|
||||
$data->hex = md5($sql);
|
||||
return $data;
|
||||
}
|
||||
|
||||
/** @inheritDoc */
|
||||
public function findOne(string $sql) : bool|array {
|
||||
public function findOne(string $sql) : bool|MysqlResultData {
|
||||
$result = $this->query($sql." limit 0, 1");
|
||||
if (!$result || $result->num_rows == 0) return false;
|
||||
$data = new MysqlStorageData($result);
|
||||
return $data->fetchOne();
|
||||
return new MysqlResultData($result);
|
||||
}
|
||||
|
||||
/** @inheritDoc */
|
||||
public function findById(string $table, int $id, string $name = 'id') : bool|array {
|
||||
$result = $this->query("select * from ".$table." where `".$name."`=".$id." limit 0, 1");
|
||||
public function findById(string $table, mixed $id, string $name = 'id') : bool|array {
|
||||
$id = (is_numeric($id)) ? (int) $id : $this->escapeStr($id);
|
||||
$result = $this->query("select * from ".$this->escapeStr($table)." where `$name`='$id' limit 0, 1");
|
||||
if (!$result || $result->num_rows == 0) return false;
|
||||
$data = new MysqlStorageData($result);
|
||||
$data = new MysqlResultData($result);
|
||||
return $data->fetchOne();
|
||||
}
|
||||
|
||||
|
||||
@@ -2,19 +2,22 @@
|
||||
|
||||
namespace Rmphp\Storage\Mysql;
|
||||
|
||||
use Mysqli;
|
||||
use mysqli_result;
|
||||
|
||||
interface MysqlStorageInterface {
|
||||
|
||||
/**
|
||||
* @return \Mysqli
|
||||
* @return Mysqli
|
||||
*/
|
||||
public function mysql() : \Mysqli;
|
||||
public function mysql() : Mysqli;
|
||||
|
||||
/**
|
||||
* Метод прямого запроса к текущей БД
|
||||
* @param string $sql
|
||||
* @return bool|\mysqli_result
|
||||
* @return bool|mysqli_result
|
||||
*/
|
||||
public function query(string $sql) : bool|\mysqli_result;
|
||||
public function query(string $sql) : bool|mysqli_result;
|
||||
|
||||
/**
|
||||
* Метод добавления записи в текущую БД
|
||||
@@ -36,11 +39,11 @@ interface MysqlStorageInterface {
|
||||
* Метод редактирования записи в текущей БД по ID
|
||||
* @param string $table
|
||||
* @param array $data
|
||||
* @param int $id
|
||||
* @param mixed $id
|
||||
* @param array $modifier
|
||||
* @return bool
|
||||
*/
|
||||
public function updateById(string $table, array $data, int $id, array $modifier = []) : bool;
|
||||
public function updateById(string $table, array $data, mixed $id, array $modifier = []) : bool;
|
||||
|
||||
/**
|
||||
* @param string $table
|
||||
@@ -60,10 +63,10 @@ interface MysqlStorageInterface {
|
||||
|
||||
/**
|
||||
* @param string $table
|
||||
* @param int $id
|
||||
* @param mixed $id
|
||||
* @return bool
|
||||
*/
|
||||
public function deleteById(string $table, int $id) : bool;
|
||||
public function deleteById(string $table, mixed $id) : bool;
|
||||
|
||||
/**
|
||||
* @param string $table
|
||||
@@ -77,23 +80,23 @@ interface MysqlStorageInterface {
|
||||
* @param int $ln
|
||||
* @param int $numPage
|
||||
* @param int $count
|
||||
* @return bool|MysqlStorageData
|
||||
* @return bool|MysqlResultData
|
||||
*/
|
||||
public function find(string $sql, int $ln = 0, int $numPage = 1, int $count=0) : bool|MysqlStorageData;
|
||||
public function find(string $sql, int $ln = 0, int $numPage = 1, int $count=0) : bool|MysqlResultData;
|
||||
|
||||
/**
|
||||
* @param string $sql
|
||||
* @return bool|array
|
||||
*/
|
||||
public function findOne(string $sql) : bool|array;
|
||||
public function findOne(string $sql) : bool|MysqlResultData;
|
||||
|
||||
/**
|
||||
* @param string $table
|
||||
* @param int $id
|
||||
* @param mixed $id
|
||||
* @param string $name
|
||||
* @return bool|array
|
||||
*/
|
||||
public function findById(string $table, int $id, string $name = 'id') : bool|array;
|
||||
public function findById(string $table, mixed $id, string $name = 'id') : bool|array;
|
||||
|
||||
/**
|
||||
* Метод экранирования данных с учетом текущего подключения в т.ч для LIKE
|
||||
|
||||
16
src/RepositoryException.php
Normal file
16
src/RepositoryException.php
Normal file
@@ -0,0 +1,16 @@
|
||||
<?php
|
||||
|
||||
namespace Rmphp\Storage;
|
||||
|
||||
use Throwable;
|
||||
|
||||
class RepositoryException extends \Exception {
|
||||
|
||||
public array $data;
|
||||
|
||||
public function __construct($message="", $code=0, array $data = [], Throwable $previous=null) {
|
||||
parent::__construct($message, $code, $previous);
|
||||
$this->data = $data;
|
||||
}
|
||||
|
||||
}
|
||||
42
src/RepositoryInterface.php
Normal file
42
src/RepositoryInterface.php
Normal file
@@ -0,0 +1,42 @@
|
||||
<?php
|
||||
/**
|
||||
* Created by PhpStorm.
|
||||
* User: Zuev Yuri
|
||||
* Date: 12.01.2025
|
||||
* Time: 21:48
|
||||
*/
|
||||
|
||||
namespace Rmphp\Storage;
|
||||
|
||||
use ReflectionException;
|
||||
use Rmphp\Storage\Entity\EntityInterface;
|
||||
|
||||
interface RepositoryInterface {
|
||||
|
||||
/**
|
||||
* @param string $class
|
||||
* @param $data
|
||||
* @return object
|
||||
* @throws RepositoryException
|
||||
*/
|
||||
public function createFromData(string $class, $data) : mixed;
|
||||
|
||||
|
||||
/**
|
||||
* @param object $object
|
||||
* @param array $data
|
||||
* @return mixed
|
||||
* @throws RepositoryException
|
||||
*/
|
||||
public function updateFromData(object $object, array $data) : mixed;
|
||||
|
||||
|
||||
/**
|
||||
* @param object $object
|
||||
* @param callable|null $method
|
||||
* @return array
|
||||
* @throws RepositoryException
|
||||
*/
|
||||
public function getProperties(object $object, callable $method = null) : array;
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user