6 Commits
7.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
4 changed files with 31 additions and 18 deletions

View File

@@ -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"
```

View File

@@ -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;
}
}

View File

@@ -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());

View File

@@ -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;
/**