Compare commits
8 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
96ee46476c | ||
|
|
d8991dcef5 | ||
|
|
30a20d427b | ||
|
|
10f6d62b43 | ||
|
|
2beff209de | ||
|
|
a39c0a6bf1 | ||
|
|
c516546b5d | ||
|
|
945c95441f |
@@ -10,12 +10,12 @@ Stable version
|
||||
composer require rmphp/storage
|
||||
```
|
||||
```bash
|
||||
composer require rmphp/storage:"^6.0"
|
||||
composer require rmphp/storage:"^8.0"
|
||||
```
|
||||
|
||||
|
||||
Dev version contains the latest changes
|
||||
|
||||
```bash
|
||||
composer require rmphp/storage:"6.x-dev"
|
||||
composer require rmphp/storage:"8.x-dev"
|
||||
```
|
||||
|
||||
@@ -16,10 +16,11 @@ class AbstractDataObject {
|
||||
* @param object $object
|
||||
* @param array $data
|
||||
* @param bool $update
|
||||
* @param bool $withNull
|
||||
* @return mixed
|
||||
* @throws Exception
|
||||
*/
|
||||
protected static function fillObject(ReflectionClass $class, object $object, array $data, bool $update = false) : mixed {
|
||||
protected static function fillObject(ReflectionClass $class, object $object, array $data, bool $update = false, bool $withNull = true) : mixed {
|
||||
try {
|
||||
$value = [];
|
||||
foreach($class->getProperties() as $property){
|
||||
@@ -45,11 +46,14 @@ class AbstractDataObject {
|
||||
if(is_object($value[$property->getName()])){
|
||||
$object->{$property->getName()} = $value[$property->getName()];
|
||||
}
|
||||
elseif(isset($value[$property->getName()])){
|
||||
elseif(isset($value[$property->getName()]) && $value[$property->getName()] !== ""){
|
||||
$object->{$property->getName()} = new ($property->getType()->getName())($value[$property->getName()]);
|
||||
}
|
||||
elseif($withNull && isset($value[$property->getName()])){
|
||||
$object->{$property->getName()} = new ($property->getType()->getName())($value[$property->getName()]);
|
||||
}
|
||||
// Значения нет и VO может быть без параметров
|
||||
elseif(self::isEmptyAvailable($property->getType()->getName())) {
|
||||
elseif($withNull && self::isEmptyAvailable($property->getType()->getName())) {
|
||||
$object->{$property->getName()} = new ($property->getType()->getName())();
|
||||
}
|
||||
}
|
||||
@@ -64,7 +68,10 @@ class AbstractDataObject {
|
||||
elseif($property->getType()->getName() == 'bool'){
|
||||
$object->{$property->getName()} = (bool)$value[$property->getName()];
|
||||
}
|
||||
elseif(isset($value[$property->getName()])){
|
||||
elseif(isset($value[$property->getName()]) && $value[$property->getName()] !== ""){
|
||||
$object->{$property->getName()} = $value[$property->getName()];
|
||||
}
|
||||
elseif($withNull && isset($value[$property->getName()])){
|
||||
$object->{$property->getName()} = $value[$property->getName()];
|
||||
}
|
||||
}
|
||||
@@ -101,4 +108,5 @@ class AbstractDataObject {
|
||||
protected function getFillObjectStack() : array {
|
||||
return self::$stack;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -28,4 +28,12 @@ abstract class AbstractEntity implements EntityInterface {
|
||||
return $this->$name ?? "";
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $name
|
||||
* @return bool
|
||||
*/
|
||||
public function __isset(string $name): bool {
|
||||
return isset($this->$name);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -15,17 +15,24 @@ class MysqlStorage implements MysqlStorageInterface {
|
||||
/**
|
||||
* Внутренний конструктор подключения к БД
|
||||
* Mysql constructor.
|
||||
* @param array $params
|
||||
* @param object $params
|
||||
* @throws Exception
|
||||
*/
|
||||
public function __construct(array $params) {
|
||||
$this->mysqli = new mysqli($params['host'], $params['user'], $params['pass'], $params['base']);
|
||||
public function __construct(
|
||||
private readonly object $params
|
||||
) {
|
||||
$this->mysqli = new mysqli(
|
||||
$this->params->host,
|
||||
$this->params->user,
|
||||
$this->params->pass,
|
||||
$this->params->base
|
||||
);
|
||||
// выводим ошибку при неудачном подключении
|
||||
if ($this->mysqli->connect_errno) {
|
||||
throw new Exception($this->mysqli->connect_errno);
|
||||
}
|
||||
$this->mysqli->set_charset("utf8");
|
||||
if(!empty($params['logsEnable'])) $this->logsEnabled = true;
|
||||
if(!empty($this->params->logsEnable)) $this->logsEnabled = true;
|
||||
}
|
||||
|
||||
/** @inheritDoc */
|
||||
|
||||
@@ -27,8 +27,7 @@ abstract class AbstractRepository extends AbstractDataObject implements Reposito
|
||||
$fieldValue[$property->getName()] = $object->{'get'.ucfirst($property->getName())}($property->getValue($object));
|
||||
}
|
||||
elseif($property->hasType() && class_exists($property->getType()->getName()) && $property->getValue($object) instanceof ValueObjectInterface){
|
||||
$value = $property->getValue($object)->getValue();
|
||||
if(isset($value)) $fieldValue[$property->getName()] = $value;
|
||||
$fieldValue[$property->getName()] = $property->getValue($object)->getValue();
|
||||
}
|
||||
elseif(is_bool($property->getValue($object))){
|
||||
$fieldValue[$property->getName()] = (int) $property->getValue($object);
|
||||
@@ -49,11 +48,14 @@ abstract class AbstractRepository extends AbstractDataObject implements Reposito
|
||||
}
|
||||
|
||||
|
||||
/** @inheritDoc */
|
||||
public function createFromData(string $class, array|object $data) : object {
|
||||
/**
|
||||
* @inheritDoc
|
||||
* @throws RepositoryException
|
||||
*/
|
||||
public function createFromData(string $class, array|object $data, bool $withNull = 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);
|
||||
return self::fillObject(self::$classes[$class], new $class, (is_object($data)) ? get_object_vars($data) : $data, false, $withNull);
|
||||
}
|
||||
catch (Exception $exception) {
|
||||
throw new RepositoryException($exception->getMessage());
|
||||
@@ -61,12 +63,15 @@ abstract class AbstractRepository extends AbstractDataObject implements Reposito
|
||||
}
|
||||
|
||||
|
||||
/** @inheritDoc */
|
||||
public function updateFromData(object $object, array|object $data) : object {
|
||||
/**
|
||||
* @inheritDoc
|
||||
* @throws RepositoryException
|
||||
*/
|
||||
public function updateFromData(object $object, array|object $data, bool $withNull = 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);
|
||||
return self::fillObject(self::$classes[$class], clone $object, (is_object($data)) ? get_object_vars($data) : $data, true, $withNull);
|
||||
}
|
||||
catch (Exception $exception) {
|
||||
throw new RepositoryException($exception->getMessage());
|
||||
|
||||
@@ -25,19 +25,19 @@ interface RepositoryInterface {
|
||||
/**
|
||||
* @param string $class
|
||||
* @param array|object $data
|
||||
* @param bool $withNull
|
||||
* @return mixed
|
||||
* @throws RepositoryException
|
||||
*/
|
||||
public function createFromData(string $class, array|object $data) : mixed;
|
||||
public function createFromData(string $class, array|object $data, bool $withNull = true) : mixed;
|
||||
|
||||
|
||||
/**
|
||||
* @param object $object
|
||||
* @param array|object $data
|
||||
* @param bool $withNull
|
||||
* @return mixed
|
||||
* @throws RepositoryException
|
||||
*/
|
||||
public function updateFromData(object $object, array|object $data) : mixed;
|
||||
public function updateFromData(object $object, array|object $data, bool $withNull = true) : mixed;
|
||||
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user