From d6d571cb1a5fa4f1f418efdbb6608968019c38ba Mon Sep 17 00:00:00 2001 From: User Date: Sun, 22 Jun 2025 02:49:36 +0300 Subject: [PATCH] 20250622#3 --- src/AbstractMysqlRepository.php | 169 +++++++++++++++---------- src/Mysql/MysqlRepositoryInterface.php | 28 ++++ 2 files changed, 128 insertions(+), 69 deletions(-) diff --git a/src/AbstractMysqlRepository.php b/src/AbstractMysqlRepository.php index 4c79c70..c0f9f08 100644 --- a/src/AbstractMysqlRepository.php +++ b/src/AbstractMysqlRepository.php @@ -2,7 +2,6 @@ namespace Rmphp\Storage; - use Rmphp\ODM\ObjectDataMapper; use Rmphp\ODM\ODMException; use Rmphp\Storage\Mysql\MysqlRepositoryInterface; @@ -10,6 +9,7 @@ use Rmphp\Storage\Mysql\MysqlResultData; use Rmphp\Storage\Mysql\MysqlStorageInterface; use Rmphp\Storage\Repository\EntityInterface; use Rmphp\Storage\Repository\RepositoryException; +use Throwable; abstract class AbstractMysqlRepository implements MysqlRepositoryInterface { @@ -23,60 +23,98 @@ abstract class AbstractMysqlRepository implements MysqlRepositoryInterface { public function __construct( public readonly MysqlStorageInterface $mysql, - public readonly ObjectDataMapper $mapper + protected readonly ObjectDataMapper $mapper ) {} - /** * @inheritDoc - * @throws ODMException */ - public function createFromResult(string $class, ?MysqlResultData $result, callable $function = null): mixed { - if($result instanceof MysqlResultData) { - $val = (isset($function)) ? $function($result->fetchOne()) : $result->fetchOne(); - $out = $this->mapper->createObjectFromData($class, $val); - } - return $out ?? null; - } - - - /** - * @inheritDoc - * @throws ODMException - */ - public function createListFromResult(string $class, ?MysqlResultData $result, callable $function = null): array { - if($result instanceof MysqlResultData) { - foreach($result->fetch() as $resultValue) { - $val = (isset($function)) ? $function($resultValue) : $resultValue; - $out[] = $this->mapper->createObjectFromData($class, $val); - } - } - return $out ?? []; - } - - - /** - * @inheritDoc - * @throws ODMException - */ - public function getEntityById(int $id, string $table = null): mixed { - if(!isset($table)) $table = $this->getTable(); - if($result = $this->mysql->findById($table, $id)) $out = $this->mapper->createObjectFromData($this->getEntityClass(), $result); - return $out ?? null; - } - - - /** - * @inheritDoc - * @throws ODMException - */ - public function saveEntity(EntityInterface $object, string $table = null) : mixed { - if(!isset($table)) $table = $this->getTable(); - $in = $this->mapper->getDataFromObject($object, function ($value){ - return (is_string($value)) ? $this->mysql->escapeStr($value) : $value; - }); - if($this->isDebug()) {$this->debug($object, $in, $table, ...$this->getDebugExtraData()); exit;} + final public function getData(object $object, callable $method = null) : array { try { + return $this->mapper->getDataFromObject($object, $method); + } catch(ODMException $odmException) { + throw new RepositoryException($odmException->getMessage(), $odmException->getCode()); + } + } + + /** + * @inheritDoc + */ + final public function createFromData(string $class, array|object $data, bool $withEmpty = true) : object { + try{ + return $this->mapper->createObjectFromData($class, $data, $withEmpty); + } catch(ODMException $odmException) { + throw new RepositoryException($odmException->getMessage(), $odmException->getCode()); + } + } + + /** + * @inheritDoc + */ + final public function updateFromData(object $object, array|object $data, bool $withEmpty = true) : object { + try{ + return $this->mapper->updateObjectFromData($object, $data, $withEmpty); + } catch(ODMException $odmException) { + throw new RepositoryException($odmException->getMessage(), $odmException->getCode()); + } + } + + /** + * @inheritDoc + */ + final public function createFromResult(string $class, ?MysqlResultData $result, callable $function = null): mixed { + try { + if($result instanceof MysqlResultData) { + $val = (isset($function)) ? $function($result->fetchOne()) : $result->fetchOne(); + $out = $this->mapper->createObjectFromData($class, $val); + } + return $out ?? null; + } catch(ODMException $odmException) { + throw new RepositoryException($odmException->getMessage(), $odmException->getCode()); + } + } + + /** + * @inheritDoc + */ + final public function createListFromResult(string $class, ?MysqlResultData $result, callable $function = null): array { + try { + if($result instanceof MysqlResultData) { + foreach($result->fetch() as $resultValue) { + $val = (isset($function)) ? $function($resultValue) : $resultValue; + $out[] = $this->mapper->createObjectFromData($class, $val); + } + } + return $out ?? []; + } catch(ODMException $odmException) { + throw new RepositoryException($odmException->getMessage(), $odmException->getCode()); + } + } + + /** + * @inheritDoc + */ + final public function getEntityById(int $id, string $table = null): mixed { + try { + if(!isset($table)) $table = $this->getTable(); + if($result = $this->mysql->findById($table, $id)) $out = $this->mapper->createObjectFromData($this->getEntityClass(), $result); + return $out ?? null; + } catch(ODMException $odmException) { + throw new RepositoryException($odmException->getMessage(), $odmException->getCode()); + } + } + + /** + * @inheritDoc + */ + final public function saveEntity(EntityInterface $object, string $table = null) : mixed { + try { + if(!isset($table)) $table = $this->getTable(); + $in = $this->mapper->getDataFromObject($object, function ($value){ + return (is_string($value)) ? $this->mysql->escapeStr($value) : $value; + }); + if($this->isDebug()) {$this->debug($object, $in, $table, ...$this->getDebugExtraData()); exit;} + if (!empty($object->getId()) && !empty($this->mysql->findById($table, $object->getId()))) { $this->mysql->updateById($table, $in, $object->getId()); return $object->getId(); @@ -84,12 +122,14 @@ abstract class AbstractMysqlRepository implements MysqlRepositoryInterface { $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());} + } + catch (ODMException|Throwable $throwable) { + throw new RepositoryException($throwable->getMessage()); + } } - /** @inheritDoc */ - public function saveData(array $data, string $table = null, string $primaryKey = 'id') : mixed { + final 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; @@ -103,12 +143,11 @@ abstract class AbstractMysqlRepository implements MysqlRepositoryInterface { $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());} + } catch (Throwable $throwable) {throw new RepositoryException($throwable->getMessage());} } - /** @inheritDoc */ - public function saveEntityGroup(array $objects, string $table = null): array { + final public function saveEntityGroup(array $objects, string $table = null): array { try{ $this->mysql->mysql()->begin_transaction(); foreach($objects as $object) $id[] = $this->saveEntity($object, $table); @@ -121,9 +160,8 @@ abstract class AbstractMysqlRepository implements MysqlRepositoryInterface { } } - /** @inheritDoc */ - public function saveDataGroup(array $objects, string $table = null, string $primaryKey = 'id'): array { + final 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); @@ -136,9 +174,8 @@ abstract class AbstractMysqlRepository implements MysqlRepositoryInterface { } } - /** @inheritDoc */ - public function deleteEntity(EntityInterface $object, string $table = null) : bool { + final 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()); @@ -146,27 +183,23 @@ abstract class AbstractMysqlRepository implements MysqlRepositoryInterface { return false; } - /** @inheritDoc */ - public function getStorageLogs() : array { + final public function getStorageLogs() : array { return $this->mysql->getLogs(); } - /** @inheritDoc */ - public function setTable(string $table) : void { + final public function setTable(string $table) : void { $this->table = $table; } - /** @inheritDoc */ - public function setEntity(string $entity) : void { + final public function setEntity(string $entity) : void { $this->entity = $entity; } - /** @inheritDoc */ - public function setDebug(bool $debug) : void { + final public function setDebug(bool $debug) : void { $this->debug = $debug; } @@ -181,7 +214,6 @@ abstract class AbstractMysqlRepository implements MysqlRepositoryInterface { throw new RepositoryException("Имя таблицы не задано"); } - /** * @return string * @throws RepositoryException @@ -192,7 +224,6 @@ abstract class AbstractMysqlRepository implements MysqlRepositoryInterface { throw new RepositoryException("Не указан объект"); } - /** * @return bool */ diff --git a/src/Mysql/MysqlRepositoryInterface.php b/src/Mysql/MysqlRepositoryInterface.php index a3c6777..7ce5a8e 100644 --- a/src/Mysql/MysqlRepositoryInterface.php +++ b/src/Mysql/MysqlRepositoryInterface.php @@ -8,16 +8,43 @@ namespace Rmphp\Storage\Mysql; +use Rmphp\ODM\ObjectDataMapper; use Rmphp\Storage\Repository\EntityInterface; use Rmphp\Storage\Repository\RepositoryException; + interface MysqlRepositoryInterface { + /** + * @param object $object + * @param callable|null $method + * @return array + * @throws RepositoryException + */ + public function getData(object $object, callable $method = null) : array; + + /** + * @param string $class + * @param array|object $data + * @param bool $withEmpty + * @return mixed + */ + public function createFromData(string $class, array|object $data, bool $withEmpty = true) : mixed; + + /** + * @param object $object + * @param array|object $data + * @param bool $withEmpty + * @return mixed + */ + public function updateFromData(object $object, array|object $data, bool $withEmpty = true) : mixed; + /** * @param string $class * @param MysqlResultData|null $result * @param callable|null $function * @return mixed + * @throws RepositoryException */ public function createFromResult(string $class, ?MysqlResultData $result, callable $function = null): mixed; @@ -27,6 +54,7 @@ interface MysqlRepositoryInterface { * @param MysqlResultData|null $result * @param callable|null $function * @return array + * @throws RepositoryException */ public function createListFromResult(string $class, ?MysqlResultData $result, callable $function = null): array;