Compare commits
7 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
35026bb223 | ||
|
|
e2727c34e8 | ||
|
|
e16700fe8d | ||
|
|
335c4d240c | ||
|
|
91af1ea14f | ||
|
|
41523e6899 | ||
|
|
4a56e42af4 |
@@ -10,12 +10,12 @@ Stable version
|
|||||||
composer require rmphp/storage
|
composer require rmphp/storage
|
||||||
```
|
```
|
||||||
```bash
|
```bash
|
||||||
composer require rmphp/storage:"^9.0"
|
composer require rmphp/storage:"^11.0"
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
Dev version contains the latest changes
|
Dev version contains the latest changes
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
composer require rmphp/storage:"9.x-dev"
|
composer require rmphp/storage:"11.x-dev"
|
||||||
```
|
```
|
||||||
|
|||||||
@@ -25,7 +25,7 @@ abstract class AbstractMysqlRepository extends AbstractRepository implements Mys
|
|||||||
|
|
||||||
|
|
||||||
/** @inheritDoc */
|
/** @inheritDoc */
|
||||||
public function createFromResult(string $class, bool|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->createFromData($class, $val);
|
||||||
@@ -35,7 +35,7 @@ abstract class AbstractMysqlRepository extends AbstractRepository implements Mys
|
|||||||
|
|
||||||
|
|
||||||
/** @inheritDoc */
|
/** @inheritDoc */
|
||||||
public function createListFromResult(string $class, bool|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;
|
||||||
|
|||||||
@@ -8,8 +8,8 @@ use Attribute;
|
|||||||
class ValueObject {
|
class ValueObject {
|
||||||
|
|
||||||
public function __construct(
|
public function __construct(
|
||||||
public bool $autoPropertyName = false,
|
|
||||||
public ?string $propertyName = null,
|
public ?string $propertyName = null,
|
||||||
|
public bool $firstProperty = false
|
||||||
) {}
|
) {}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,4 +5,4 @@ namespace Rmphp\Storage\Attribute;
|
|||||||
use Attribute;
|
use Attribute;
|
||||||
|
|
||||||
#[Attribute(Attribute::TARGET_CLASS)]
|
#[Attribute(Attribute::TARGET_CLASS)]
|
||||||
class ValueObjectAutoPropertyName {}
|
class ValueObjectFirstProperty {}
|
||||||
14
src/Attribute/ValueObjectPropertyName.php
Normal file
14
src/Attribute/ValueObjectPropertyName.php
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Rmphp\Storage\Attribute;
|
||||||
|
|
||||||
|
use Attribute;
|
||||||
|
|
||||||
|
#[Attribute(Attribute::TARGET_CLASS)]
|
||||||
|
class ValueObjectPropertyName {
|
||||||
|
|
||||||
|
public function __construct(
|
||||||
|
public ?string $name = null,
|
||||||
|
) {}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -59,7 +59,7 @@ abstract class AbstractDataObject {
|
|||||||
// Если тип свойства класс (valueObject)
|
// Если тип свойства класс (valueObject)
|
||||||
elseif($property->hasType() && class_exists($property->getType()->getName())) {
|
elseif($property->hasType() && class_exists($property->getType()->getName())) {
|
||||||
// значение объект
|
// значение объект
|
||||||
if(is_object($value[$property->getName()])){
|
if(isset($value[$property->getName()]) && is_object($value[$property->getName()])){
|
||||||
$object->{$property->getName()} = $value[$property->getName()];
|
$object->{$property->getName()} = $value[$property->getName()];
|
||||||
$case[$property->getName()] = 'VO: Object';
|
$case[$property->getName()] = 'VO: Object';
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -25,7 +25,7 @@ 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;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -4,7 +4,6 @@ namespace Rmphp\Storage\Entity;
|
|||||||
|
|
||||||
interface ValueObjectInterface {
|
interface ValueObjectInterface {
|
||||||
|
|
||||||
public function getValue();
|
|
||||||
public function __toString(): string;
|
public function __toString(): string;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -16,21 +16,19 @@ interface MysqlRepositoryInterface extends RepositoryInterface {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $class
|
* @param string $class
|
||||||
* @param bool|MysqlResultData $result
|
* @param MysqlResultData|null $result
|
||||||
* @param callable|null $function
|
* @param callable|null $function
|
||||||
* @return mixed
|
* @return mixed
|
||||||
* @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
|
|
||||||
*/
|
*/
|
||||||
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,26 +13,23 @@ class MysqlStorage implements MysqlStorageInterface {
|
|||||||
private Mysqli $mysqli;
|
private Mysqli $mysqli;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Внутренний конструктор подключения к БД
|
* @param array $paramsArray
|
||||||
* Mysql constructor.
|
|
||||||
* @param object $params
|
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public function __construct(
|
public function __construct(array $paramsArray) {
|
||||||
private readonly object $params
|
$params = (object) $paramsArray;
|
||||||
) {
|
|
||||||
$this->mysqli = new mysqli(
|
$this->mysqli = new mysqli(
|
||||||
$this->params->host,
|
$params->host,
|
||||||
$this->params->user,
|
$params->user,
|
||||||
$this->params->pass,
|
$params->pass,
|
||||||
$this->params->base
|
$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($this->params->logsEnable)) $this->logsEnabled = true;
|
if(!empty($params->logs)) $this->logsEnabled = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** @inheritDoc */
|
/** @inheritDoc */
|
||||||
@@ -118,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;
|
||||||
@@ -131,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;
|
||||||
@@ -140,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
|
||||||
|
|||||||
@@ -10,7 +10,8 @@ use Rmphp\Storage\Attribute\Property;
|
|||||||
use Rmphp\Storage\Attribute\PropertyNoReturn;
|
use Rmphp\Storage\Attribute\PropertyNoReturn;
|
||||||
use Rmphp\Storage\Attribute\PropertyNoReturnIfNull;
|
use Rmphp\Storage\Attribute\PropertyNoReturnIfNull;
|
||||||
use Rmphp\Storage\Attribute\ValueObject;
|
use Rmphp\Storage\Attribute\ValueObject;
|
||||||
use Rmphp\Storage\Attribute\ValueObjectAutoPropertyName;
|
use Rmphp\Storage\Attribute\ValueObjectFirstProperty;
|
||||||
|
use Rmphp\Storage\Attribute\ValueObjectPropertyName;
|
||||||
use Rmphp\Storage\Component\AbstractDataObject;
|
use Rmphp\Storage\Component\AbstractDataObject;
|
||||||
use Rmphp\Storage\Exception\RepositoryException;
|
use Rmphp\Storage\Exception\RepositoryException;
|
||||||
|
|
||||||
@@ -60,16 +61,22 @@ abstract class AbstractRepository extends AbstractDataObject implements Reposito
|
|||||||
self::$attributeObjects[$valueObjectClass] = !empty(self::$classes[$valueObjectClass]->getAttributes(ValueObject::class))
|
self::$attributeObjects[$valueObjectClass] = !empty(self::$classes[$valueObjectClass]->getAttributes(ValueObject::class))
|
||||||
? self::$classes[$valueObjectClass]->getAttributes(ValueObject::class)[0]->newInstance()
|
? self::$classes[$valueObjectClass]->getAttributes(ValueObject::class)[0]->newInstance()
|
||||||
: new ValueObject();
|
: 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];
|
$valueObjectAttributes = self::$attributeObjects[$valueObjectClass];
|
||||||
if(!empty(self::$classes[$valueObjectClass]->getAttributes(ValueObjectAutoPropertyName::class))) $valueObjectAttributes->autoPropertyName = true;
|
|
||||||
|
|
||||||
if(!empty($valueObjectAttributes->propertyName) && self::$classes[$valueObjectClass]->hasProperty($valueObjectAttributes->propertyName)){
|
if(!empty($valueObjectAttributes->propertyName) && self::$classes[$valueObjectClass]->hasProperty($valueObjectAttributes->propertyName)){
|
||||||
if(self::$classes[$valueObjectClass]->getProperty($valueObjectAttributes->propertyName)->isInitialized($property->getValue($object))){
|
if(self::$classes[$valueObjectClass]->getProperty($valueObjectAttributes->propertyName)->isInitialized($property->getValue($object))){
|
||||||
$fieldValue[$property->getName()] = self::$classes[$valueObjectClass]->getProperty($valueObjectAttributes->propertyName)->getValue($property->getValue($object));
|
$fieldValue[$property->getName()] = self::$classes[$valueObjectClass]->getProperty($valueObjectAttributes->propertyName)->getValue($property->getValue($object));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
elseif(!empty($valueObjectAttributes->autoPropertyName) && count(self::$classes[$valueObjectClass]->getProperties()) === 1){
|
elseif(!empty($valueObjectAttributes->firstProperty) && count(self::$classes[$valueObjectClass]->getProperties()) > 0){
|
||||||
if(self::$classes[$valueObjectClass]->getProperties()[0]->isInitialized($property->getValue($object))){
|
if(self::$classes[$valueObjectClass]->getProperties()[0]->isInitialized($property->getValue($object))){
|
||||||
$fieldValue[$property->getName()] = self::$classes[$valueObjectClass]->getProperties()[0]->getValue($property->getValue($object));
|
$fieldValue[$property->getName()] = self::$classes[$valueObjectClass]->getProperties()[0]->getValue($property->getValue($object));
|
||||||
}
|
}
|
||||||
@@ -77,11 +84,6 @@ abstract class AbstractRepository extends AbstractDataObject implements Reposito
|
|||||||
elseif(self::$classes[$valueObjectClass]->hasMethod('getValue')){
|
elseif(self::$classes[$valueObjectClass]->hasMethod('getValue')){
|
||||||
$fieldValue[$property->getName()] = $property->getValue($object)->getValue();
|
$fieldValue[$property->getName()] = $property->getValue($object)->getValue();
|
||||||
}
|
}
|
||||||
elseif(count(self::$classes[$valueObjectClass]->getProperties()) === 1){
|
|
||||||
if(self::$classes[$valueObjectClass]->getProperties()[0]->isInitialized($property->getValue($object))){
|
|
||||||
$fieldValue[$property->getName()] = self::$classes[$valueObjectClass]->getProperties()[0]->getValue($property->getValue($object));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
elseif(is_bool($property->getValue($object))){
|
elseif(is_bool($property->getValue($object))){
|
||||||
$fieldValue[$property->getName()] = (int)$property->getValue($object);
|
$fieldValue[$property->getName()] = (int)$property->getValue($object);
|
||||||
|
|||||||
Reference in New Issue
Block a user