20250327#2

This commit is contained in:
User
2025-03-27 03:01:09 +03:00
parent 10f6d62b43
commit 30a20d427b
3 changed files with 13 additions and 13 deletions

View File

@@ -16,11 +16,11 @@ class AbstractDataObject {
* @param object $object
* @param array $data
* @param bool $update
* @param bool $onlyNotNull
* @param bool $withNull
* @return mixed
* @throws Exception
*/
protected static function fillObject(ReflectionClass $class, object $object, array $data, bool $update = false, bool $onlyNotNull = 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){
@@ -49,11 +49,11 @@ class AbstractDataObject {
elseif(isset($value[$property->getName()]) && $value[$property->getName()] != ""){
$object->{$property->getName()} = new ($property->getType()->getName())($value[$property->getName()]);
}
elseif(!$onlyNotNull && isset($value[$property->getName()])){
elseif($withNull && isset($value[$property->getName()])){
$object->{$property->getName()} = new ($property->getType()->getName())($value[$property->getName()]);
}
// Значения нет и VO может быть без параметров
elseif(!$onlyNotNull && self::isEmptyAvailable($property->getType()->getName())) {
elseif($withNull && self::isEmptyAvailable($property->getType()->getName())) {
$object->{$property->getName()} = new ($property->getType()->getName())();
}
}
@@ -71,7 +71,7 @@ class AbstractDataObject {
elseif(isset($value[$property->getName()]) && $value[$property->getName()] != ""){
$object->{$property->getName()} = $value[$property->getName()];
}
elseif(!$onlyNotNull && isset($value[$property->getName()])){
elseif($withNull && isset($value[$property->getName()])){
$object->{$property->getName()} = $value[$property->getName()];
}
}

View File

@@ -52,10 +52,10 @@ abstract class AbstractRepository extends AbstractDataObject implements Reposito
* @inheritDoc
* @throws RepositoryException
*/
public function createFromData(string $class, array|object $data, bool $onlyNotNull = false) : object {
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, false, $onlyNotNull);
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());
@@ -67,11 +67,11 @@ abstract class AbstractRepository extends AbstractDataObject implements Reposito
* @inheritDoc
* @throws RepositoryException
*/
public function updateFromData(object $object, array|object $data, bool $onlyNotNull = false) : object {
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, $onlyNotNull);
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 $onlyNotNull
* @param bool $withNull
* @return mixed
*/
public function createFromData(string $class, array|object $data, bool $onlyNotNull = false) : mixed;
public function createFromData(string $class, array|object $data, bool $withNull = true) : mixed;
/**
* @param object $object
* @param array|object $data
* @param bool $onlyNotNull
* @param bool $withNull
* @return mixed
*/
public function updateFromData(object $object, array|object $data, bool $onlyNotNull = false) : mixed;
public function updateFromData(object $object, array|object $data, bool $withNull = true) : mixed;
/**