Compare commits
34 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
7c71fdf49c | ||
|
|
56d801cc54 | ||
|
|
d6d571cb1a | ||
|
|
7e8dfe0678 | ||
|
|
f42bc7751a | ||
|
|
35026bb223 | ||
|
|
e2727c34e8 | ||
|
|
e16700fe8d | ||
|
|
335c4d240c | ||
|
|
91af1ea14f | ||
|
|
41523e6899 | ||
|
|
4a56e42af4 | ||
|
|
4b1c0d7044 | ||
|
|
d300a6b628 | ||
|
|
09e8520ded | ||
|
|
e6fd18fa2f | ||
|
|
9c1f6be326 | ||
|
|
f3458d1870 | ||
|
|
1ce2a09e01 | ||
|
|
fd996d40c8 | ||
|
|
96ee46476c | ||
|
|
d8991dcef5 | ||
|
|
30a20d427b | ||
|
|
10f6d62b43 | ||
|
|
2beff209de | ||
|
|
a39c0a6bf1 | ||
|
|
c516546b5d | ||
|
|
945c95441f | ||
|
|
d0e38875e6 | ||
|
|
73c9af71ab | ||
|
|
5b0c0bc7b4 | ||
|
|
dfbcfdf321 | ||
|
|
a54b55cc4e | ||
|
|
612132f938 |
@@ -1,6 +1,6 @@
|
|||||||
## Rmphp/Storage
|
## Rmphp/Storage
|
||||||
|
|
||||||
DB component for **Rmphp**
|
Storage component for **Rmphp**
|
||||||
|
|
||||||
## Install
|
## Install
|
||||||
|
|
||||||
@@ -10,12 +10,12 @@ Stable version
|
|||||||
composer require rmphp/storage
|
composer require rmphp/storage
|
||||||
```
|
```
|
||||||
```bash
|
```bash
|
||||||
composer require rmphp/storage:"^5.0"
|
composer require rmphp/storage:"^12.0"
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
Dev version contains the latest changes
|
Dev version contains the latest changes
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
composer require rmphp/storage:"5.x-dev"
|
composer require rmphp/storage:"12.x-dev"
|
||||||
```
|
```
|
||||||
|
|||||||
@@ -9,6 +9,7 @@
|
|||||||
],
|
],
|
||||||
"require": {
|
"require": {
|
||||||
"php": "^8.1",
|
"php": "^8.1",
|
||||||
|
"rmphp/odm": "^1.0",
|
||||||
"ext-mysqli": "*"
|
"ext-mysqli": "*"
|
||||||
},
|
},
|
||||||
"autoload": {
|
"autoload": {
|
||||||
|
|||||||
@@ -2,12 +2,16 @@
|
|||||||
|
|
||||||
namespace Rmphp\Storage;
|
namespace Rmphp\Storage;
|
||||||
|
|
||||||
use Rmphp\Storage\Entity\EntityInterface;
|
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\RepositoryException;
|
||||||
|
use Throwable;
|
||||||
|
|
||||||
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;
|
||||||
@@ -18,48 +22,99 @@ 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
|
||||||
public function createFromResult(string $class, bool|MysqlResultData $result, callable $function = null): mixed {
|
*/
|
||||||
if($result instanceof MysqlResultData) {
|
final public function getData(object $object, callable $method = null) : array {
|
||||||
$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 {
|
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()))) {
|
if (!empty($object->getId()) && !empty($this->mysql->findById($table, $object->getId()))) {
|
||||||
$this->mysql->updateById($table, $in, $object->getId());
|
$this->mysql->updateById($table, $in, $object->getId());
|
||||||
return $object->getId();
|
return $object->getId();
|
||||||
@@ -67,17 +122,19 @@ abstract class AbstractMysqlRepository extends AbstractRepository implements Mys
|
|||||||
$this->mysql->insert($table, $in);
|
$this->mysql->insert($table, $in);
|
||||||
return (is_string($object->getId())) ? $object->getId() : $this->mysql->mysql()->insert_id;
|
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 */
|
/** @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();
|
if(!isset($table)) $table = $this->getTable();
|
||||||
$in = array_map(function ($value){
|
$in = array_map(function ($value){
|
||||||
return (is_string($value)) ? $this->mysql->escapeStr($value) : $value;
|
return (is_string($value)) ? $this->mysql->escapeStr($value) : $value;
|
||||||
}, $data);
|
}, $data);
|
||||||
if($this->getDebug()) {$this->debug($data, $in, $table); exit;}
|
if($this->isDebug()) {$this->debug($data, $in, $table, ...$this->getDebugExtraData()); exit;}
|
||||||
try {
|
try {
|
||||||
if (!empty($data[$primaryKey]) && !empty($this->mysql->findById($table, $data[$primaryKey], $primaryKey))) {
|
if (!empty($data[$primaryKey]) && !empty($this->mysql->findById($table, $data[$primaryKey], $primaryKey))) {
|
||||||
$this->mysql->updateById($table, $in, $data[$primaryKey]);
|
$this->mysql->updateById($table, $in, $data[$primaryKey]);
|
||||||
@@ -86,12 +143,11 @@ abstract class AbstractMysqlRepository extends AbstractRepository implements Mys
|
|||||||
$this->mysql->insert($table, $in);
|
$this->mysql->insert($table, $in);
|
||||||
return (is_string($data[$primaryKey])) ? $data[$primaryKey] : $this->mysql->mysql()->insert_id;
|
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 */
|
/** @inheritDoc */
|
||||||
public function saveEntityGroup(array $objects, string $table = null): array {
|
final public function saveEntityGroup(array $objects, string $table = null): array {
|
||||||
try{
|
try{
|
||||||
$this->mysql->mysql()->begin_transaction();
|
$this->mysql->mysql()->begin_transaction();
|
||||||
foreach($objects as $object) $id[] = $this->saveEntity($object, $table);
|
foreach($objects as $object) $id[] = $this->saveEntity($object, $table);
|
||||||
@@ -104,9 +160,8 @@ abstract class AbstractMysqlRepository extends AbstractRepository implements Mys
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/** @inheritDoc */
|
/** @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{
|
try{
|
||||||
$this->mysql->mysql()->begin_transaction();
|
$this->mysql->mysql()->begin_transaction();
|
||||||
foreach($objects as $object) $id[] = $this->saveData($object, $table, $primaryKey);
|
foreach($objects as $object) $id[] = $this->saveData($object, $table, $primaryKey);
|
||||||
@@ -119,9 +174,8 @@ abstract class AbstractMysqlRepository extends AbstractRepository implements Mys
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/** @inheritDoc */
|
/** @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(!isset($table)) $table = $this->getTable();
|
||||||
if(!empty($object->getId())){
|
if(!empty($object->getId())){
|
||||||
return $this->mysql->deleteById($table, $object->getId());
|
return $this->mysql->deleteById($table, $object->getId());
|
||||||
@@ -129,27 +183,23 @@ abstract class AbstractMysqlRepository extends AbstractRepository implements Mys
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/** @inheritDoc */
|
/** @inheritDoc */
|
||||||
public function getStorageLogs() : array {
|
final public function getStorageLogs() : array {
|
||||||
return $this->mysql->getLogs();
|
return $this->mysql->getLogs();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/** @inheritDoc */
|
/** @inheritDoc */
|
||||||
public function setTable(string $table) : void {
|
final public function setTable(string $table) : void {
|
||||||
$this->table = $table;
|
$this->table = $table;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/** @inheritDoc */
|
/** @inheritDoc */
|
||||||
public function setEntity(string $entity) : void {
|
final public function setEntity(string $entity) : void {
|
||||||
$this->entity = $entity;
|
$this->entity = $entity;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/** @inheritDoc */
|
/** @inheritDoc */
|
||||||
public function setDebug(bool $debug) : void {
|
final public function setDebug(bool $debug) : void {
|
||||||
$this->debug = $debug;
|
$this->debug = $debug;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -164,7 +214,6 @@ abstract class AbstractMysqlRepository extends AbstractRepository implements Mys
|
|||||||
throw new RepositoryException("Имя таблицы не задано");
|
throw new RepositoryException("Имя таблицы не задано");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return string
|
* @return string
|
||||||
* @throws RepositoryException
|
* @throws RepositoryException
|
||||||
@@ -175,16 +224,22 @@ abstract class AbstractMysqlRepository extends AbstractRepository implements Mys
|
|||||||
throw new RepositoryException("Не указан объект");
|
throw new RepositoryException("Не указан объект");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
private function getDebug(): bool {
|
private function isDebug(): bool {
|
||||||
if(!empty($this->debug)) return $this->debug;
|
if(!empty($this->debug)) return $this->debug;
|
||||||
if(!empty(static::DEBUG)) return static::DEBUG;
|
if(!empty(static::DEBUG)) return static::DEBUG;
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
protected function getDebugExtraData() : array {
|
||||||
|
return [$this->mapper->getRepositoryStack(), $this->mapper->getClassesCache(), $this->mapper->getAttributesObjectsCache()];
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param ...$arg
|
* @param ...$arg
|
||||||
* @return void
|
* @return void
|
||||||
|
|||||||
@@ -1,102 +0,0 @@
|
|||||||
<?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) || is_array($property->getValue($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());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -8,29 +8,58 @@
|
|||||||
|
|
||||||
namespace Rmphp\Storage\Mysql;
|
namespace Rmphp\Storage\Mysql;
|
||||||
|
|
||||||
use Rmphp\Storage\Entity\EntityInterface;
|
use Rmphp\ODM\ObjectDataMapper;
|
||||||
use Rmphp\Storage\RepositoryException;
|
use Rmphp\Storage\Repository\EntityInterface;
|
||||||
use Rmphp\Storage\RepositoryInterface;
|
use Rmphp\Storage\Repository\RepositoryException;
|
||||||
|
|
||||||
interface MysqlRepositoryInterface extends RepositoryInterface {
|
/**
|
||||||
|
* @property MysqlStorageInterface $mysql
|
||||||
|
* @property ObjectDataMapper $mapper
|
||||||
|
*/
|
||||||
|
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 string $class
|
||||||
* @param bool|MysqlResultData $result
|
* @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
|
* @param callable|null $function
|
||||||
* @return mixed
|
* @return mixed
|
||||||
* @throws RepositoryException
|
* @throws RepositoryException
|
||||||
*/
|
*/
|
||||||
public function createFromResult(string $class, bool|MysqlResultData $result, callable $function = null): mixed;
|
public function createFromResult(string $class, ?MysqlResultData $result, callable $function = null): mixed;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $class
|
* @param string $class
|
||||||
* @param bool|MysqlResultData $result
|
* @param MysqlResultData|null $result
|
||||||
* @param callable|null $function
|
* @param callable|null $function
|
||||||
* @return array
|
* @return array
|
||||||
* @throws RepositoryException
|
* @throws RepositoryException
|
||||||
*/
|
*/
|
||||||
public function createListFromResult(string $class, bool|MysqlResultData $result, callable $function = null): array;
|
public function createListFromResult(string $class, ?MysqlResultData $result, callable $function = null): array;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param int $id
|
* @param int $id
|
||||||
|
|||||||
@@ -13,19 +13,23 @@ class MysqlStorage implements MysqlStorageInterface {
|
|||||||
private Mysqli $mysqli;
|
private Mysqli $mysqli;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Внутренний конструктор подключения к БД
|
* @param array $paramsArray
|
||||||
* Mysql constructor.
|
|
||||||
* @param array $params
|
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public function __construct(array $params) {
|
public function __construct(array $paramsArray) {
|
||||||
$this->mysqli = new mysqli($params['host'], $params['user'], $params['pass'], $params['base']);
|
$params = (object) $paramsArray;
|
||||||
|
$this->mysqli = new mysqli(
|
||||||
|
$params->host,
|
||||||
|
$params->user,
|
||||||
|
$params->pass,
|
||||||
|
$params->base
|
||||||
|
);
|
||||||
// выводим ошибку при неудачном подключении
|
// выводим ошибку при неудачном подключении
|
||||||
if ($this->mysqli->connect_errno) {
|
if ($this->mysqli->connect_errno) {
|
||||||
throw new Exception($this->mysqli->connect_errno);
|
throw new Exception($this->mysqli->connect_errno);
|
||||||
}
|
}
|
||||||
$this->mysqli->set_charset("utf8");
|
$this->mysqli->set_charset($params->chartset ?? "utf8");
|
||||||
if(!empty($params['logsEnable'])) $this->logsEnabled = true;
|
if(!empty($params->logs)) $this->logsEnabled = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** @inheritDoc */
|
/** @inheritDoc */
|
||||||
@@ -111,7 +115,7 @@ class MysqlStorage implements MysqlStorageInterface {
|
|||||||
|
|
||||||
|
|
||||||
/** @inheritDoc */
|
/** @inheritDoc */
|
||||||
public function find(string $sql, int $ln=0, int $numPage=1, int $count=0): bool|MysqlResultData {
|
public function find(string $sql, int $ln=0, int $numPage=1, int $count=0): ?MysqlResultData {
|
||||||
|
|
||||||
if ($ln > 1) {
|
if ($ln > 1) {
|
||||||
$cnts = (!empty($count)) ? $count : $this->query($sql)->num_rows;
|
$cnts = (!empty($count)) ? $count : $this->query($sql)->num_rows;
|
||||||
@@ -124,7 +128,7 @@ class MysqlStorage implements MysqlStorageInterface {
|
|||||||
}
|
}
|
||||||
|
|
||||||
$result = $this->query($sql.$limit);
|
$result = $this->query($sql.$limit);
|
||||||
if (!$result || $result->num_rows == 0) return false;
|
if (!$result || $result->num_rows == 0) return null;
|
||||||
|
|
||||||
$data = new MysqlResultData($result);
|
$data = new MysqlResultData($result);
|
||||||
$data->count = $cnts ?? 0;
|
$data->count = $cnts ?? 0;
|
||||||
@@ -133,17 +137,17 @@ class MysqlStorage implements MysqlStorageInterface {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/** @inheritDoc */
|
/** @inheritDoc */
|
||||||
public function findOne(string $sql) : bool|MysqlResultData {
|
public function findOne(string $sql) : ?MysqlResultData {
|
||||||
$result = $this->query($sql." limit 0, 1");
|
$result = $this->query($sql." limit 0, 1");
|
||||||
if (!$result || $result->num_rows == 0) return false;
|
if (!$result || $result->num_rows == 0) return null;
|
||||||
return new MysqlResultData($result);
|
return new MysqlResultData($result);
|
||||||
}
|
}
|
||||||
|
|
||||||
/** @inheritDoc */
|
/** @inheritDoc */
|
||||||
public function findById(string $table, mixed $id, string $name = 'id') : bool|array {
|
public function findById(string $table, mixed $id, string $name = 'id') : ?array {
|
||||||
$id = (is_numeric($id)) ? (int) $id : $this->escapeStr($id);
|
$id = (is_numeric($id)) ? (int) $id : $this->escapeStr($id);
|
||||||
$result = $this->query("select * from ".$this->escapeStr($table)." where `$name`='$id' limit 0, 1");
|
$result = $this->query("select * from ".$this->escapeStr($table)." where `$name`='$id' limit 0, 1");
|
||||||
if (!$result || $result->num_rows == 0) return false;
|
if (!$result || $result->num_rows == 0) return null;
|
||||||
$data = new MysqlResultData($result);
|
$data = new MysqlResultData($result);
|
||||||
return $data->fetchOne();
|
return $data->fetchOne();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -82,13 +82,13 @@ interface MysqlStorageInterface {
|
|||||||
* @param int $count
|
* @param int $count
|
||||||
* @return bool|MysqlResultData
|
* @return bool|MysqlResultData
|
||||||
*/
|
*/
|
||||||
public function find(string $sql, int $ln = 0, int $numPage = 1, int $count=0) : bool|MysqlResultData;
|
public function find(string $sql, int $ln = 0, int $numPage = 1, int $count=0) : ?MysqlResultData;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $sql
|
* @param string $sql
|
||||||
* @return bool|array
|
* @return bool|array
|
||||||
*/
|
*/
|
||||||
public function findOne(string $sql) : bool|MysqlResultData;
|
public function findOne(string $sql) : ?MysqlResultData;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $table
|
* @param string $table
|
||||||
@@ -96,7 +96,7 @@ interface MysqlStorageInterface {
|
|||||||
* @param string $name
|
* @param string $name
|
||||||
* @return bool|array
|
* @return bool|array
|
||||||
*/
|
*/
|
||||||
public function findById(string $table, mixed $id, string $name = 'id') : bool|array;
|
public function findById(string $table, mixed $id, string $name = 'id') : ?array;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Метод экранирования данных с учетом текущего подключения в т.ч для LIKE
|
* Метод экранирования данных с учетом текущего подключения в т.ч для LIKE
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace Rmphp\Storage\Entity;
|
namespace Rmphp\Storage\Repository;
|
||||||
|
|
||||||
abstract class AbstractEntity implements EntityInterface {
|
abstract class AbstractEntity implements EntityInterface {
|
||||||
|
|
||||||
@@ -8,7 +8,7 @@ abstract class AbstractEntity implements EntityInterface {
|
|||||||
* @return mixed
|
* @return mixed
|
||||||
*/
|
*/
|
||||||
public function getId(): mixed {
|
public function getId(): mixed {
|
||||||
return (isset($this->id)) ? (($this->id instanceof ValueObjectInterface) ? $this->id->get() : $this->id) : null;
|
return (isset($this->id)) ? (($this->id instanceof ValueObjectInterface) ? $this->id->getValue() : $this->id) : null;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -25,7 +25,15 @@ abstract class AbstractEntity implements EntityInterface {
|
|||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
public function __get(string $name) {
|
public function __get(string $name) {
|
||||||
return $this->$name ?? "";
|
return $this->$name ?? null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $name
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
public function __isset(string $name): bool {
|
||||||
|
return isset($this->$name);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -6,7 +6,7 @@
|
|||||||
* Time: 3:58
|
* Time: 3:58
|
||||||
*/
|
*/
|
||||||
|
|
||||||
namespace Rmphp\Storage\Entity;
|
namespace Rmphp\Storage\Repository;
|
||||||
|
|
||||||
interface EntityInterface {
|
interface EntityInterface {
|
||||||
|
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace Rmphp\Storage;
|
namespace Rmphp\Storage\Repository;
|
||||||
|
|
||||||
use Throwable;
|
use Throwable;
|
||||||
|
|
||||||
@@ -1,10 +1,9 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace Rmphp\Storage\Entity;
|
namespace Rmphp\Storage\Repository;
|
||||||
|
|
||||||
interface ValueObjectInterface {
|
interface ValueObjectInterface {
|
||||||
|
|
||||||
public function get();
|
|
||||||
public function __toString(): string;
|
public function __toString(): string;
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -1,42 +0,0 @@
|
|||||||
<?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