From 7e8dfe0678a57b60d0fd63bc37a7c0b99b0cd672 Mon Sep 17 00:00:00 2001 From: User Date: Sun, 22 Jun 2025 01:23:54 +0300 Subject: [PATCH] 20250622#2 --- src/AbstractMysqlRepository.php | 39 +++++++++++++++------- src/Mysql/MysqlRepositoryInterface.php | 4 +-- src/Repository/RepositoryInterface.php | 46 -------------------------- 3 files changed, 29 insertions(+), 60 deletions(-) delete mode 100644 src/Repository/RepositoryInterface.php diff --git a/src/AbstractMysqlRepository.php b/src/AbstractMysqlRepository.php index 2d79eda..4c79c70 100644 --- a/src/AbstractMysqlRepository.php +++ b/src/AbstractMysqlRepository.php @@ -2,14 +2,16 @@ namespace Rmphp\Storage; -use Rmphp\ODM\AbstractRepository; + +use Rmphp\ODM\ObjectDataMapper; +use Rmphp\ODM\ODMException; use Rmphp\Storage\Mysql\MysqlRepositoryInterface; use Rmphp\Storage\Mysql\MysqlResultData; use Rmphp\Storage\Mysql\MysqlStorageInterface; use Rmphp\Storage\Repository\EntityInterface; use Rmphp\Storage\Repository\RepositoryException; -abstract class AbstractMysqlRepository extends AbstractRepository implements MysqlRepositoryInterface { +abstract class AbstractMysqlRepository implements MysqlRepositoryInterface { public const DEBUG = false; public const TABLE = null; @@ -20,44 +22,57 @@ abstract class AbstractMysqlRepository extends AbstractRepository implements Mys private bool $debug; public function __construct( - public readonly MysqlStorageInterface $mysql + public readonly MysqlStorageInterface $mysql, + public readonly ObjectDataMapper $mapper ) {} - /** @inheritDoc */ + /** + * @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->createFromData($class, $val); + $out = $this->mapper->createObjectFromData($class, $val); } return $out ?? null; } - /** @inheritDoc */ + /** + * @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->createFromData($class, $val); + $out[] = $this->mapper->createObjectFromData($class, $val); } } return $out ?? []; } - /** @inheritDoc */ + /** + * @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->createFromData($this->getEntityClass(), $result); + if($result = $this->mysql->findById($table, $id)) $out = $this->mapper->createObjectFromData($this->getEntityClass(), $result); return $out ?? null; } - /** @inheritDoc */ + /** + * @inheritDoc + * @throws ODMException + */ public function saveEntity(EntityInterface $object, string $table = null) : mixed { if(!isset($table)) $table = $this->getTable(); - $in = $this->getProperties($object, function ($value){ + $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;} @@ -191,7 +206,7 @@ abstract class AbstractMysqlRepository extends AbstractRepository implements Mys * @return array */ protected function getDebugExtraData() : array { - return [$this->getRepositoryStack(), $this->getClassesCache(), $this->getAttributesObjectsCache()]; + return [$this->mapper->getRepositoryStack(), $this->mapper->getClassesCache(), $this->mapper->getAttributesObjectsCache()]; } /** diff --git a/src/Mysql/MysqlRepositoryInterface.php b/src/Mysql/MysqlRepositoryInterface.php index e65b1b5..a3c6777 100644 --- a/src/Mysql/MysqlRepositoryInterface.php +++ b/src/Mysql/MysqlRepositoryInterface.php @@ -10,9 +10,8 @@ namespace Rmphp\Storage\Mysql; use Rmphp\Storage\Repository\EntityInterface; use Rmphp\Storage\Repository\RepositoryException; -use Rmphp\Storage\Repository\RepositoryInterface; -interface MysqlRepositoryInterface extends RepositoryInterface { +interface MysqlRepositoryInterface { /** * @param string $class @@ -22,6 +21,7 @@ interface MysqlRepositoryInterface extends RepositoryInterface { */ public function createFromResult(string $class, ?MysqlResultData $result, callable $function = null): mixed; + /** * @param string $class * @param MysqlResultData|null $result diff --git a/src/Repository/RepositoryInterface.php b/src/Repository/RepositoryInterface.php deleted file mode 100644 index 16b60af..0000000 --- a/src/Repository/RepositoryInterface.php +++ /dev/null @@ -1,46 +0,0 @@ -