8 Commits
6.x ... 8.0

Author SHA1 Message Date
User
96ee46476c 20250327#4 2025-03-27 03:44:20 +03:00
User
d8991dcef5 20250327#3 2025-03-27 03:13:36 +03:00
User
30a20d427b 20250327#2 2025-03-27 03:01:09 +03:00
User
10f6d62b43 20250327#1 2025-03-27 00:02:46 +03:00
User
2beff209de 20250326#3 2025-03-26 16:31:17 +03:00
User
a39c0a6bf1 20250326#2 2025-03-26 16:30:23 +03:00
User
c516546b5d 20250326#1 2025-03-26 16:02:27 +03:00
User
945c95441f 20250321#1 2025-03-21 21:46:19 +03:00
6 changed files with 50 additions and 22 deletions

View File

@@ -10,12 +10,12 @@ Stable version
composer require rmphp/storage composer require rmphp/storage
``` ```
```bash ```bash
composer require rmphp/storage:"^6.0" composer require rmphp/storage:"^8.0"
``` ```
Dev version contains the latest changes Dev version contains the latest changes
```bash ```bash
composer require rmphp/storage:"6.x-dev" composer require rmphp/storage:"8.x-dev"
``` ```

View File

@@ -16,10 +16,11 @@ class AbstractDataObject {
* @param object $object * @param object $object
* @param array $data * @param array $data
* @param bool $update * @param bool $update
* @param bool $withNull
* @return mixed * @return mixed
* @throws Exception * @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 { try {
$value = []; $value = [];
foreach($class->getProperties() as $property){ foreach($class->getProperties() as $property){
@@ -45,11 +46,14 @@ class AbstractDataObject {
if(is_object($value[$property->getName()])){ if(is_object($value[$property->getName()])){
$object->{$property->getName()} = $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()]); $object->{$property->getName()} = new ($property->getType()->getName())($value[$property->getName()]);
} }
// Значения нет и VO может быть без параметров // Значения нет и VO может быть без параметров
elseif(self::isEmptyAvailable($property->getType()->getName())) { elseif($withNull && self::isEmptyAvailable($property->getType()->getName())) {
$object->{$property->getName()} = new ($property->getType()->getName())(); $object->{$property->getName()} = new ($property->getType()->getName())();
} }
} }
@@ -64,7 +68,10 @@ class AbstractDataObject {
elseif($property->getType()->getName() == 'bool'){ elseif($property->getType()->getName() == 'bool'){
$object->{$property->getName()} = (bool)$value[$property->getName()]; $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()]; $object->{$property->getName()} = $value[$property->getName()];
} }
} }
@@ -101,4 +108,5 @@ class AbstractDataObject {
protected function getFillObjectStack() : array { protected function getFillObjectStack() : array {
return self::$stack; return self::$stack;
} }
} }

View File

@@ -28,4 +28,12 @@ abstract class AbstractEntity implements EntityInterface {
return $this->$name ?? ""; return $this->$name ?? "";
} }
/**
* @param string $name
* @return bool
*/
public function __isset(string $name): bool {
return isset($this->$name);
}
} }

View File

@@ -15,17 +15,24 @@ class MysqlStorage implements MysqlStorageInterface {
/** /**
* Внутренний конструктор подключения к БД * Внутренний конструктор подключения к БД
* Mysql constructor. * Mysql constructor.
* @param array $params * @param object $params
* @throws Exception * @throws Exception
*/ */
public function __construct(array $params) { public function __construct(
$this->mysqli = new mysqli($params['host'], $params['user'], $params['pass'], $params['base']); 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) { 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("utf8");
if(!empty($params['logsEnable'])) $this->logsEnabled = true; if(!empty($this->params->logsEnable)) $this->logsEnabled = true;
} }
/** @inheritDoc */ /** @inheritDoc */

View File

@@ -27,8 +27,7 @@ abstract class AbstractRepository extends AbstractDataObject implements Reposito
$fieldValue[$property->getName()] = $object->{'get'.ucfirst($property->getName())}($property->getValue($object)); $fieldValue[$property->getName()] = $object->{'get'.ucfirst($property->getName())}($property->getValue($object));
} }
elseif($property->hasType() && class_exists($property->getType()->getName()) && $property->getValue($object) instanceof ValueObjectInterface){ elseif($property->hasType() && class_exists($property->getType()->getName()) && $property->getValue($object) instanceof ValueObjectInterface){
$value = $property->getValue($object)->getValue(); $fieldValue[$property->getName()] = $property->getValue($object)->getValue();
if(isset($value)) $fieldValue[$property->getName()] = $value;
} }
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);
@@ -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 { try {
if(!isset(self::$classes[$class])) self::$classes[$class] = new ReflectionClass($class); 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) { catch (Exception $exception) {
throw new RepositoryException($exception->getMessage()); 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 { try {
$class = get_class($object); $class = get_class($object);
if(!isset(self::$classes[$class])) self::$classes[$class] = new ReflectionClass($class); 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) { catch (Exception $exception) {
throw new RepositoryException($exception->getMessage()); throw new RepositoryException($exception->getMessage());

View File

@@ -25,19 +25,19 @@ interface RepositoryInterface {
/** /**
* @param string $class * @param string $class
* @param array|object $data * @param array|object $data
* @param bool $withNull
* @return mixed * @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 object $object
* @param array|object $data * @param array|object $data
* @param bool $withNull
* @return mixed * @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;
/** /**