20250622#2

This commit is contained in:
User
2025-06-22 01:23:54 +03:00
parent f42bc7751a
commit 7e8dfe0678
3 changed files with 29 additions and 60 deletions

View File

@@ -2,14 +2,16 @@
namespace Rmphp\Storage; namespace Rmphp\Storage;
use Rmphp\ODM\AbstractRepository;
use Rmphp\ODM\ObjectDataMapper;
use Rmphp\ODM\ODMException;
use Rmphp\Storage\Mysql\MysqlRepositoryInterface; use Rmphp\Storage\Mysql\MysqlRepositoryInterface;
use Rmphp\Storage\Mysql\MysqlResultData; use Rmphp\Storage\Mysql\MysqlResultData;
use Rmphp\Storage\Mysql\MysqlStorageInterface; use Rmphp\Storage\Mysql\MysqlStorageInterface;
use Rmphp\Storage\Repository\EntityInterface; use Rmphp\Storage\Repository\EntityInterface;
use Rmphp\Storage\Repository\RepositoryException; use Rmphp\Storage\Repository\RepositoryException;
abstract class AbstractMysqlRepository extends AbstractRepository implements MysqlRepositoryInterface { abstract class AbstractMysqlRepository implements MysqlRepositoryInterface {
public const DEBUG = false; public const DEBUG = false;
public const TABLE = null; public const TABLE = null;
@@ -20,44 +22,57 @@ abstract class AbstractMysqlRepository extends AbstractRepository implements Mys
private bool $debug; private bool $debug;
public function __construct( 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 { public function createFromResult(string $class, ?MysqlResultData $result, callable $function = null): mixed {
if($result instanceof MysqlResultData) { if($result instanceof MysqlResultData) {
$val = (isset($function)) ? $function($result->fetchOne()) : $result->fetchOne(); $val = (isset($function)) ? $function($result->fetchOne()) : $result->fetchOne();
$out = $this->createFromData($class, $val); $out = $this->mapper->createObjectFromData($class, $val);
} }
return $out ?? null; return $out ?? null;
} }
/** @inheritDoc */ /**
* @inheritDoc
* @throws ODMException
*/
public function createListFromResult(string $class, ?MysqlResultData $result, callable $function = null): array { public function createListFromResult(string $class, ?MysqlResultData $result, callable $function = null): array {
if($result instanceof MysqlResultData) { if($result instanceof MysqlResultData) {
foreach($result->fetch() as $resultValue) { foreach($result->fetch() as $resultValue) {
$val = (isset($function)) ? $function($resultValue) : $resultValue; $val = (isset($function)) ? $function($resultValue) : $resultValue;
$out[] = $this->createFromData($class, $val); $out[] = $this->mapper->createObjectFromData($class, $val);
} }
} }
return $out ?? []; return $out ?? [];
} }
/** @inheritDoc */ /**
* @inheritDoc
* @throws ODMException
*/
public function getEntityById(int $id, string $table = null): mixed { public function getEntityById(int $id, string $table = null): mixed {
if(!isset($table)) $table = $this->getTable(); 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; return $out ?? null;
} }
/** @inheritDoc */ /**
* @inheritDoc
* @throws ODMException
*/
public function saveEntity(EntityInterface $object, string $table = null) : mixed { public function saveEntity(EntityInterface $object, string $table = null) : mixed {
if(!isset($table)) $table = $this->getTable(); 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; return (is_string($value)) ? $this->mysql->escapeStr($value) : $value;
}); });
if($this->isDebug()) {$this->debug($object, $in, $table, ...$this->getDebugExtraData()); exit;} if($this->isDebug()) {$this->debug($object, $in, $table, ...$this->getDebugExtraData()); exit;}
@@ -191,7 +206,7 @@ abstract class AbstractMysqlRepository extends AbstractRepository implements Mys
* @return array * @return array
*/ */
protected function getDebugExtraData() : array { protected function getDebugExtraData() : array {
return [$this->getRepositoryStack(), $this->getClassesCache(), $this->getAttributesObjectsCache()]; return [$this->mapper->getRepositoryStack(), $this->mapper->getClassesCache(), $this->mapper->getAttributesObjectsCache()];
} }
/** /**

View File

@@ -10,9 +10,8 @@ namespace Rmphp\Storage\Mysql;
use Rmphp\Storage\Repository\EntityInterface; use Rmphp\Storage\Repository\EntityInterface;
use Rmphp\Storage\Repository\RepositoryException; use Rmphp\Storage\Repository\RepositoryException;
use Rmphp\Storage\Repository\RepositoryInterface;
interface MysqlRepositoryInterface extends RepositoryInterface { interface MysqlRepositoryInterface {
/** /**
* @param string $class * @param string $class
@@ -22,6 +21,7 @@ interface MysqlRepositoryInterface extends RepositoryInterface {
*/ */
public function createFromResult(string $class, ?MysqlResultData $result, callable $function = null): mixed; public function createFromResult(string $class, ?MysqlResultData $result, callable $function = null): mixed;
/** /**
* @param string $class * @param string $class
* @param MysqlResultData|null $result * @param MysqlResultData|null $result

View File

@@ -1,46 +0,0 @@
<?php
/**
* Created by PhpStorm.
* User: Zuev Yuri
* Date: 12.01.2025
* Time: 21:48
*/
namespace Rmphp\Storage\Repository;
interface RepositoryInterface {
/**
* @param object $object
* @param callable|null $method
* @return array
* @throws RepositoryException
*/
public function getProperties(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;
/**
* @return array
*/
public function getRepositoryStack() : array;
}