Compare commits
5 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
7c71fdf49c | ||
|
|
56d801cc54 | ||
|
|
d6d571cb1a | ||
|
|
7e8dfe0678 | ||
|
|
f42bc7751a |
@@ -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:"^11.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:"11.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,14 +2,16 @@
|
|||||||
|
|
||||||
namespace Rmphp\Storage;
|
namespace Rmphp\Storage;
|
||||||
|
|
||||||
use Rmphp\Storage\Entity\EntityInterface;
|
use Rmphp\ODM\ObjectDataMapper;
|
||||||
use Rmphp\Storage\Exception\RepositoryException;
|
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\AbstractRepository;
|
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;
|
||||||
@@ -20,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, ?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, ?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->isDebug()) {$this->debug($object, $in, $table, ...$this->getDebugExtraData()); 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();
|
||||||
@@ -69,12 +122,14 @@ 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;
|
||||||
@@ -88,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);
|
||||||
@@ -106,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);
|
||||||
@@ -121,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());
|
||||||
@@ -131,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;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -166,7 +214,6 @@ abstract class AbstractMysqlRepository extends AbstractRepository implements Mys
|
|||||||
throw new RepositoryException("Имя таблицы не задано");
|
throw new RepositoryException("Имя таблицы не задано");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return string
|
* @return string
|
||||||
* @throws RepositoryException
|
* @throws RepositoryException
|
||||||
@@ -177,7 +224,6 @@ abstract class AbstractMysqlRepository extends AbstractRepository implements Mys
|
|||||||
throw new RepositoryException("Не указан объект");
|
throw new RepositoryException("Не указан объект");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
@@ -191,7 +237,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()];
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -1,14 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
namespace Rmphp\Storage\Attribute;
|
|
||||||
|
|
||||||
use Attribute;
|
|
||||||
|
|
||||||
#[Attribute(Attribute::TARGET_CLASS)]
|
|
||||||
class Data {
|
|
||||||
|
|
||||||
public function __construct(
|
|
||||||
public bool $ignorEmpty = false,
|
|
||||||
) {}
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -1,8 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
namespace Rmphp\Storage\Attribute;
|
|
||||||
|
|
||||||
use Attribute;
|
|
||||||
|
|
||||||
#[Attribute(Attribute::TARGET_CLASS)]
|
|
||||||
class DataIgnorEmpty {}
|
|
||||||
@@ -1,14 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
namespace Rmphp\Storage\Attribute;
|
|
||||||
|
|
||||||
use Attribute;
|
|
||||||
|
|
||||||
#[Attribute(Attribute::TARGET_CLASS)]
|
|
||||||
class Entity {
|
|
||||||
|
|
||||||
public function __construct(
|
|
||||||
public bool $noReturnIfNull = false,
|
|
||||||
) {}
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -1,8 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
namespace Rmphp\Storage\Attribute;
|
|
||||||
|
|
||||||
use Attribute;
|
|
||||||
|
|
||||||
#[Attribute(Attribute::TARGET_CLASS)]
|
|
||||||
class EntityNoReturnIfNull {}
|
|
||||||
@@ -1,16 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
namespace Rmphp\Storage\Attribute;
|
|
||||||
|
|
||||||
use Attribute;
|
|
||||||
|
|
||||||
#[Attribute(Attribute::TARGET_PROPERTY)]
|
|
||||||
class Property {
|
|
||||||
|
|
||||||
public function __construct(
|
|
||||||
public ?string $keyName = null,
|
|
||||||
public bool $noReturn = false,
|
|
||||||
public bool $noReturnIfNull = false,
|
|
||||||
) {}
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -1,8 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
namespace Rmphp\Storage\Attribute;
|
|
||||||
|
|
||||||
use Attribute;
|
|
||||||
|
|
||||||
#[Attribute(Attribute::TARGET_PROPERTY)]
|
|
||||||
class PropertyNoReturn {}
|
|
||||||
@@ -1,8 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
namespace Rmphp\Storage\Attribute;
|
|
||||||
|
|
||||||
use Attribute;
|
|
||||||
|
|
||||||
#[Attribute(Attribute::TARGET_PROPERTY)]
|
|
||||||
class PropertyNoReturnIfNull {}
|
|
||||||
@@ -1,15 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
namespace Rmphp\Storage\Attribute;
|
|
||||||
|
|
||||||
use Attribute;
|
|
||||||
|
|
||||||
#[Attribute(Attribute::TARGET_CLASS)]
|
|
||||||
class ValueObject {
|
|
||||||
|
|
||||||
public function __construct(
|
|
||||||
public ?string $propertyName = null,
|
|
||||||
public bool $firstProperty = false
|
|
||||||
) {}
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -1,8 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
namespace Rmphp\Storage\Attribute;
|
|
||||||
|
|
||||||
use Attribute;
|
|
||||||
|
|
||||||
#[Attribute(Attribute::TARGET_CLASS)]
|
|
||||||
class ValueObjectFirstProperty {}
|
|
||||||
@@ -1,14 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
namespace Rmphp\Storage\Attribute;
|
|
||||||
|
|
||||||
use Attribute;
|
|
||||||
|
|
||||||
#[Attribute(Attribute::TARGET_CLASS)]
|
|
||||||
class ValueObjectPropertyName {
|
|
||||||
|
|
||||||
public function __construct(
|
|
||||||
public ?string $name = null,
|
|
||||||
) {}
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -1,134 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
namespace Rmphp\Storage\Component;
|
|
||||||
|
|
||||||
use Exception;
|
|
||||||
use ReflectionClass;
|
|
||||||
use ReflectionException;
|
|
||||||
use Rmphp\Storage\Attribute\Data;
|
|
||||||
use Rmphp\Storage\Attribute\DataIgnorEmpty;
|
|
||||||
|
|
||||||
abstract class AbstractDataObject {
|
|
||||||
|
|
||||||
private static array $stack = [];
|
|
||||||
protected static array $constructorEmptyAvailableClasses = [];
|
|
||||||
protected static array $classes = [];
|
|
||||||
protected static array $attributeObjects = [];
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param ReflectionClass $class
|
|
||||||
* @param object $object
|
|
||||||
* @param array $data
|
|
||||||
* @param bool $update
|
|
||||||
* @param bool $withEmpty
|
|
||||||
* @return mixed
|
|
||||||
* @throws Exception
|
|
||||||
*/
|
|
||||||
protected static function fillObject(ReflectionClass $class, object $object, array $data, bool $update = false, bool $withEmpty = true) : mixed {
|
|
||||||
try {
|
|
||||||
if(!isset(self::$attributeObjects[$class->getName()][0])){
|
|
||||||
self::$attributeObjects[$class->getName()][0] = !empty($class->getAttributes(Data::class))
|
|
||||||
? $class->getAttributes(Data::class)[0]->newInstance()
|
|
||||||
: new Data();
|
|
||||||
}
|
|
||||||
/** @var Data $dataAttributes */
|
|
||||||
$dataAttributes = self::$attributeObjects[$class->getName()][0];
|
|
||||||
if(!empty($class->getAttributes(DataIgnorEmpty::class))) $dataAttributes->ignorEmpty = true;
|
|
||||||
|
|
||||||
$value = [];
|
|
||||||
foreach($class->getProperties() as $property){
|
|
||||||
$prop[$property->getName()] = ($property->hasType()) ? $property->getType()->getName() : "";
|
|
||||||
|
|
||||||
// значение в массиве по ключю с именем свойства
|
|
||||||
if(array_key_exists($property->getName(), $data)){
|
|
||||||
$value[$property->getName()] = $data[$property->getName()];
|
|
||||||
}
|
|
||||||
// значение в массиве по ключю с именем свойства в snake case
|
|
||||||
elseif(array_key_exists(strtolower(preg_replace("'([A-Z])'", "_$1", $property->getName())), $data)){
|
|
||||||
$value[$property->getName()] = $data[strtolower(preg_replace("'([A-Z])'", "_$1", $property->getName()))];
|
|
||||||
}
|
|
||||||
elseif($update) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
// если есть внутренний метод (приоритетная обработка)
|
|
||||||
if($class->hasMethod('set'.ucfirst($property->getName()))) {
|
|
||||||
$object->{'set'.ucfirst($property->getName())}($value[$property->getName()] ?? null);
|
|
||||||
$case[$property->getName()] = 'Method set'.ucfirst($property->getName());
|
|
||||||
}
|
|
||||||
// Если тип свойства класс (valueObject)
|
|
||||||
elseif($property->hasType() && class_exists($property->getType()->getName())) {
|
|
||||||
// значение объект
|
|
||||||
if(isset($value[$property->getName()]) && is_object($value[$property->getName()])){
|
|
||||||
$object->{$property->getName()} = $value[$property->getName()];
|
|
||||||
$case[$property->getName()] = 'VO: Object';
|
|
||||||
}
|
|
||||||
// значение не пустое
|
|
||||||
elseif(isset($value[$property->getName()]) && $value[$property->getName()] !== ""){
|
|
||||||
$object->{$property->getName()} = new ($property->getType()->getName())($value[$property->getName()]);
|
|
||||||
$case[$property->getName()] = 'VO: NewInstance';
|
|
||||||
}
|
|
||||||
// Значения нет и VO может быть без параметров
|
|
||||||
elseif(($withEmpty && empty($dataAttributes->ignorEmpty)) && self::isEmptyAvailable($property->getType()->getName())) {
|
|
||||||
$object->{$property->getName()} = new ($property->getType()->getName())();
|
|
||||||
$case[$property->getName()] = 'VO: Without params';
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// Базовые типы при наличии значения
|
|
||||||
elseif(array_key_exists($property->getName(), $value)){
|
|
||||||
if(!$property->hasType()){
|
|
||||||
$object->{$property->getName()} = $value[$property->getName()];
|
|
||||||
$case[$property->getName()] = 'Base: Hasn`t type';
|
|
||||||
}
|
|
||||||
elseif(in_array($property->getType()->getName(), ['float', 'int'])){
|
|
||||||
if(is_numeric($value[$property->getName()])) $object->{$property->getName()} = $value[$property->getName()];
|
|
||||||
$case[$property->getName()] = 'Base: Number';
|
|
||||||
}
|
|
||||||
elseif($property->getType()->getName() == 'bool'){
|
|
||||||
$object->{$property->getName()} = (bool)$value[$property->getName()];
|
|
||||||
$case[$property->getName()] = 'Base: Boolean';
|
|
||||||
}
|
|
||||||
elseif(isset($value[$property->getName()])){
|
|
||||||
$object->{$property->getName()} = $value[$property->getName()];
|
|
||||||
$case[$property->getName()] = 'Base: NotNull';
|
|
||||||
}
|
|
||||||
elseif($property->getType()->allowsNull()){
|
|
||||||
$object->{$property->getName()} = $value[$property->getName()];
|
|
||||||
$case[$property->getName()] = 'Base: Null';
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
self::$stack[$object::class." #".spl_object_id($object)]['properties'] = $prop ?? [];
|
|
||||||
self::$stack[$object::class." #".spl_object_id($object)]['values'] = $value;
|
|
||||||
self::$stack[$object::class." #".spl_object_id($object)]['matchCase'] = $case ?? [];
|
|
||||||
self::$stack[$object::class." #".spl_object_id($object)]['object'] = $object;
|
|
||||||
return $object;
|
|
||||||
}
|
|
||||||
catch (ReflectionException $exception) {
|
|
||||||
throw new Exception($exception->getMessage());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @throws ReflectionException
|
|
||||||
*/
|
|
||||||
private static function isEmptyAvailable(string $class) : bool {
|
|
||||||
if(isset(self::$constructorEmptyAvailableClasses[$class])) return self::$constructorEmptyAvailableClasses[$class];
|
|
||||||
if(!$constructor = (new \ReflectionClass($class))->getConstructor()) return self::$constructorEmptyAvailableClasses[$class] = false;
|
|
||||||
foreach($constructor->getParameters() as $param){
|
|
||||||
if(!$param->isDefaultValueAvailable()){
|
|
||||||
return self::$constructorEmptyAvailableClasses[$class] = false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return self::$constructorEmptyAvailableClasses[$class] = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return array
|
|
||||||
*/
|
|
||||||
protected function getFillObjectStack() : array {
|
|
||||||
return self::$stack;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -8,25 +8,56 @@
|
|||||||
|
|
||||||
namespace Rmphp\Storage\Mysql;
|
namespace Rmphp\Storage\Mysql;
|
||||||
|
|
||||||
use Rmphp\Storage\Entity\EntityInterface;
|
use Rmphp\ODM\ObjectDataMapper;
|
||||||
use Rmphp\Storage\Exception\RepositoryException;
|
use Rmphp\Storage\Repository\EntityInterface;
|
||||||
use Rmphp\Storage\Repository\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 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 string $class
|
||||||
* @param MysqlResultData|null $result
|
* @param MysqlResultData|null $result
|
||||||
* @param callable|null $function
|
* @param callable|null $function
|
||||||
* @return mixed
|
* @return mixed
|
||||||
|
* @throws RepositoryException
|
||||||
*/
|
*/
|
||||||
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
|
||||||
* @param callable|null $function
|
* @param callable|null $function
|
||||||
* @return array
|
* @return array
|
||||||
|
* @throws RepositoryException
|
||||||
*/
|
*/
|
||||||
public function createListFromResult(string $class, ?MysqlResultData $result, callable $function = null): array;
|
public function createListFromResult(string $class, ?MysqlResultData $result, callable $function = null): array;
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace Rmphp\Storage\Entity;
|
namespace Rmphp\Storage\Repository;
|
||||||
|
|
||||||
abstract class AbstractEntity implements EntityInterface {
|
abstract class AbstractEntity implements EntityInterface {
|
||||||
|
|
||||||
@@ -1,169 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
namespace Rmphp\Storage\Repository;
|
|
||||||
|
|
||||||
use Exception;
|
|
||||||
use ReflectionClass;
|
|
||||||
use Rmphp\Storage\Attribute\Entity;
|
|
||||||
use Rmphp\Storage\Attribute\EntityNoReturnIfNull;
|
|
||||||
use Rmphp\Storage\Attribute\Property;
|
|
||||||
use Rmphp\Storage\Attribute\PropertyNoReturn;
|
|
||||||
use Rmphp\Storage\Attribute\PropertyNoReturnIfNull;
|
|
||||||
use Rmphp\Storage\Attribute\ValueObject;
|
|
||||||
use Rmphp\Storage\Attribute\ValueObjectFirstProperty;
|
|
||||||
use Rmphp\Storage\Attribute\ValueObjectPropertyName;
|
|
||||||
use Rmphp\Storage\Component\AbstractDataObject;
|
|
||||||
use Rmphp\Storage\Exception\RepositoryException;
|
|
||||||
|
|
||||||
abstract class AbstractRepository extends AbstractDataObject implements RepositoryInterface {
|
|
||||||
|
|
||||||
/** @inheritDoc */
|
|
||||||
public function getProperties(object $object, callable $method = null) : array {
|
|
||||||
try{
|
|
||||||
$class = get_class($object);
|
|
||||||
if(!isset(self::$classes[$class])) self::$classes[$class] = new ReflectionClass($class);
|
|
||||||
|
|
||||||
if(!isset(self::$attributeObjects[$class][0])){
|
|
||||||
self::$attributeObjects[$class][0] = !empty(self::$classes[$class]->getAttributes(Entity::class))
|
|
||||||
? self::$classes[$class]->getAttributes(Entity::class)[0]->newInstance()
|
|
||||||
: new Entity();
|
|
||||||
}
|
|
||||||
/** @var Entity $entityAttributes */
|
|
||||||
$entityAttributes = self::$attributeObjects[$class][0];
|
|
||||||
if(!empty(self::$classes[$class]->getAttributes(EntityNoReturnIfNull::class))) $entityAttributes->noReturnIfNull = true;
|
|
||||||
|
|
||||||
$fieldValue = [];
|
|
||||||
foreach(self::$classes[$class]->getProperties() as $property){
|
|
||||||
|
|
||||||
if(!isset(self::$attributeObjects[$class][$property->getName()])){
|
|
||||||
self::$attributeObjects[$class][$property->getName()] = !empty($property->getAttributes(Property::class))
|
|
||||||
? $property->getAttributes(Property::class)[0]->newInstance()
|
|
||||||
: new Property();
|
|
||||||
}
|
|
||||||
/** @var Property $propertyAttributes */
|
|
||||||
$propertyAttributes = self::$attributeObjects[$class][$property->getName()];
|
|
||||||
if(!empty($property->getAttributes(PropertyNoReturnIfNull::class))) $propertyAttributes->noReturnIfNull = true;
|
|
||||||
|
|
||||||
if(!empty($property->getAttributes(PropertyNoReturn::class)) || !empty($propertyAttributes->noReturn)) continue;
|
|
||||||
|
|
||||||
if($property->isInitialized($object)) {
|
|
||||||
|
|
||||||
if(is_array($property->getValue($object))) continue;
|
|
||||||
|
|
||||||
if(self::$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())){
|
|
||||||
$valueObjectClass = get_class($property->getValue($object));
|
|
||||||
if(!isset(self::$classes[$valueObjectClass])) self::$classes[$valueObjectClass] = new ReflectionClass($valueObjectClass);
|
|
||||||
|
|
||||||
if(!isset(self::$attributeObjects[$valueObjectClass])){
|
|
||||||
self::$attributeObjects[$valueObjectClass] = !empty(self::$classes[$valueObjectClass]->getAttributes(ValueObject::class))
|
|
||||||
? self::$classes[$valueObjectClass]->getAttributes(ValueObject::class)[0]->newInstance()
|
|
||||||
: new ValueObject();
|
|
||||||
if(!empty(self::$classes[$valueObjectClass]->getAttributes(ValueObjectFirstProperty::class))) {
|
|
||||||
self::$attributeObjects[$valueObjectClass]->firstProperty = true;
|
|
||||||
}
|
|
||||||
if(!empty(self::$classes[$valueObjectClass]->getAttributes(ValueObjectPropertyName::class))) {
|
|
||||||
$propertyName = self::$classes[$valueObjectClass]->getAttributes(ValueObjectPropertyName::class)[0]->newInstance();
|
|
||||||
if(isset($propertyName->name)) self::$attributeObjects[$valueObjectClass]->propertyName = $propertyName->name;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
$valueObjectAttributes = self::$attributeObjects[$valueObjectClass];
|
|
||||||
|
|
||||||
if(!empty($valueObjectAttributes->propertyName) && self::$classes[$valueObjectClass]->hasProperty($valueObjectAttributes->propertyName)){
|
|
||||||
if(self::$classes[$valueObjectClass]->getProperty($valueObjectAttributes->propertyName)->isInitialized($property->getValue($object))){
|
|
||||||
$fieldValue[$property->getName()] = self::$classes[$valueObjectClass]->getProperty($valueObjectAttributes->propertyName)->getValue($property->getValue($object));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
elseif(!empty($valueObjectAttributes->firstProperty) && count(self::$classes[$valueObjectClass]->getProperties()) > 0){
|
|
||||||
if(self::$classes[$valueObjectClass]->getProperties()[0]->isInitialized($property->getValue($object))){
|
|
||||||
$fieldValue[$property->getName()] = self::$classes[$valueObjectClass]->getProperties()[0]->getValue($property->getValue($object));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
elseif(self::$classes[$valueObjectClass]->hasMethod('getValue')){
|
|
||||||
$fieldValue[$property->getName()] = $property->getValue($object)->getValue();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
elseif(is_bool($property->getValue($object))){
|
|
||||||
$fieldValue[$property->getName()] = (int)$property->getValue($object);
|
|
||||||
}
|
|
||||||
else{
|
|
||||||
$fieldValue[$property->getName()] = $property->getValue($object);
|
|
||||||
}
|
|
||||||
|
|
||||||
if(!isset($fieldValue[$property->getName()]) && (!empty($propertyAttributes->noReturnIfNull) || !empty($entityAttributes->noReturnIfNull))) continue;
|
|
||||||
|
|
||||||
if(array_key_exists($property->getName(), $fieldValue) && false !== $fieldValue[$property->getName()]) {
|
|
||||||
$columnName = !empty($propertyAttributes->keyName) ? $propertyAttributes->keyName : strtolower(preg_replace("'([A-Z])'", "_$1", $property->getName()));
|
|
||||||
$out[$columnName] = $fieldValue[$property->getName()];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return (isset($method)) ? array_map($method, $out ?? []) : $out ?? [];
|
|
||||||
}
|
|
||||||
catch (\ReflectionException $exception) {
|
|
||||||
throw new RepositoryException($exception->getMessage());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @inheritDoc
|
|
||||||
* @throws RepositoryException
|
|
||||||
*/
|
|
||||||
public function createFromData(string $class, array|object $data, bool $withEmpty = true) : object {
|
|
||||||
try {
|
|
||||||
if(!isset(self::$classes[$class])) self::$classes[$class] = new ReflectionClass($class);
|
|
||||||
return self::fillObject(self::$classes[$class], new $class, (is_object($data)) ? get_object_vars($data) : $data, false, $withEmpty);
|
|
||||||
}
|
|
||||||
catch (Exception $exception) {
|
|
||||||
throw new RepositoryException($exception->getMessage());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @inheritDoc
|
|
||||||
* @throws RepositoryException
|
|
||||||
*/
|
|
||||||
public function updateFromData(object $object, array|object $data, bool $withEmpty = true) : object {
|
|
||||||
try {
|
|
||||||
$class = get_class($object);
|
|
||||||
if(!isset(self::$classes[$class])) self::$classes[$class] = new ReflectionClass($class);
|
|
||||||
return self::fillObject(self::$classes[$class], clone $object, (is_object($data)) ? get_object_vars($data) : $data, true, $withEmpty);
|
|
||||||
}
|
|
||||||
catch (Exception $exception) {
|
|
||||||
throw new RepositoryException($exception->getMessage());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return array
|
|
||||||
*/
|
|
||||||
public function getRepositoryStack() : array {
|
|
||||||
return $this->getFillObjectStack();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return array
|
|
||||||
*/
|
|
||||||
public function getClassesCache() : array {
|
|
||||||
return self::$classes;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return array
|
|
||||||
*/
|
|
||||||
public function getAttributesObjectsCache() : array {
|
|
||||||
return self::$attributeObjects;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return array
|
|
||||||
*/
|
|
||||||
public function getConstructorEmptyAvailableClassesCache() : array {
|
|
||||||
return self::$constructorEmptyAvailableClasses;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -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\Exception;
|
namespace Rmphp\Storage\Repository;
|
||||||
|
|
||||||
use Throwable;
|
use Throwable;
|
||||||
|
|
||||||
@@ -1,48 +0,0 @@
|
|||||||
<?php
|
|
||||||
/**
|
|
||||||
* Created by PhpStorm.
|
|
||||||
* User: Zuev Yuri
|
|
||||||
* Date: 12.01.2025
|
|
||||||
* Time: 21:48
|
|
||||||
*/
|
|
||||||
|
|
||||||
namespace Rmphp\Storage\Repository;
|
|
||||||
|
|
||||||
|
|
||||||
use Rmphp\Storage\Exception\RepositoryException;
|
|
||||||
|
|
||||||
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;
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace Rmphp\Storage\Entity;
|
namespace Rmphp\Storage\Repository;
|
||||||
|
|
||||||
interface ValueObjectInterface {
|
interface ValueObjectInterface {
|
||||||
|
|
||||||
Reference in New Issue
Block a user