Compare commits
3 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
335c4d240c | ||
|
|
91af1ea14f | ||
|
|
41523e6899 |
@@ -10,12 +10,12 @@ Stable version
|
||||
composer require rmphp/storage
|
||||
```
|
||||
```bash
|
||||
composer require rmphp/storage:"^9.0"
|
||||
composer require rmphp/storage:"^10.0"
|
||||
```
|
||||
|
||||
|
||||
Dev version contains the latest changes
|
||||
|
||||
```bash
|
||||
composer require rmphp/storage:"9.x-dev"
|
||||
composer require rmphp/storage:"10.x-dev"
|
||||
```
|
||||
|
||||
@@ -8,8 +8,8 @@ use Attribute;
|
||||
class ValueObject {
|
||||
|
||||
public function __construct(
|
||||
public bool $autoPropertyName = false,
|
||||
public ?string $propertyName = null,
|
||||
public bool $firstProperty = false
|
||||
) {}
|
||||
|
||||
}
|
||||
|
||||
@@ -5,4 +5,4 @@ namespace Rmphp\Storage\Attribute;
|
||||
use Attribute;
|
||||
|
||||
#[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,
|
||||
) {}
|
||||
|
||||
}
|
||||
@@ -4,7 +4,6 @@ namespace Rmphp\Storage\Entity;
|
||||
|
||||
interface ValueObjectInterface {
|
||||
|
||||
public function getValue();
|
||||
public function __toString(): string;
|
||||
|
||||
}
|
||||
|
||||
@@ -13,26 +13,23 @@ class MysqlStorage implements MysqlStorageInterface {
|
||||
private Mysqli $mysqli;
|
||||
|
||||
/**
|
||||
* Внутренний конструктор подключения к БД
|
||||
* Mysql constructor.
|
||||
* @param object $params
|
||||
* @param array $paramsArray
|
||||
* @throws Exception
|
||||
*/
|
||||
public function __construct(
|
||||
private readonly object $params
|
||||
) {
|
||||
public function __construct(array $paramsArray) {
|
||||
$params = (object) $paramsArray;
|
||||
$this->mysqli = new mysqli(
|
||||
$this->params->host,
|
||||
$this->params->user,
|
||||
$this->params->pass,
|
||||
$this->params->base
|
||||
$params->host,
|
||||
$params->user,
|
||||
$params->pass,
|
||||
$params->base
|
||||
);
|
||||
// выводим ошибку при неудачном подключении
|
||||
if ($this->mysqli->connect_errno) {
|
||||
throw new Exception($this->mysqli->connect_errno);
|
||||
}
|
||||
$this->mysqli->set_charset("utf8");
|
||||
if(!empty($this->params->logsEnable)) $this->logsEnabled = true;
|
||||
$this->mysqli->set_charset($params->chartset ?? "utf8");
|
||||
if(!empty($params->logs)) $this->logsEnabled = true;
|
||||
}
|
||||
|
||||
/** @inheritDoc */
|
||||
|
||||
@@ -10,7 +10,8 @@ use Rmphp\Storage\Attribute\Property;
|
||||
use Rmphp\Storage\Attribute\PropertyNoReturn;
|
||||
use Rmphp\Storage\Attribute\PropertyNoReturnIfNull;
|
||||
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\Exception\RepositoryException;
|
||||
|
||||
@@ -60,16 +61,22 @@ abstract class AbstractRepository extends AbstractDataObject implements Reposito
|
||||
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(self::$classes[$valueObjectClass]->getAttributes(ValueObjectAutoPropertyName::class))) $valueObjectAttributes->autoPropertyName = true;
|
||||
|
||||
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->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))){
|
||||
$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')){
|
||||
$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))){
|
||||
$fieldValue[$property->getName()] = (int)$property->getValue($object);
|
||||
|
||||
Reference in New Issue
Block a user