20250327#1
This commit is contained in:
@@ -16,10 +16,11 @@ class AbstractDataObject {
|
||||
* @param object $object
|
||||
* @param array $data
|
||||
* @param bool $update
|
||||
* @param bool $onlyNotNull
|
||||
* @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 $onlyNotNull = false) : 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(!$onlyNotNull && isset($value[$property->getName()])){
|
||||
$object->{$property->getName()} = new ($property->getType()->getName())($value[$property->getName()]);
|
||||
}
|
||||
// Значения нет и VO может быть без параметров
|
||||
elseif(self::isEmptyAvailable($property->getType()->getName())) {
|
||||
elseif(!$onlyNotNull && 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(!$onlyNotNull && isset($value[$property->getName()])){
|
||||
$object->{$property->getName()} = $value[$property->getName()];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -48,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 $onlyNotNull = false) : 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, $onlyNotNull);
|
||||
}
|
||||
catch (Exception $exception) {
|
||||
throw new RepositoryException($exception->getMessage());
|
||||
@@ -60,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 $onlyNotNull = false) : 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, $onlyNotNull);
|
||||
}
|
||||
catch (Exception $exception) {
|
||||
throw new RepositoryException($exception->getMessage());
|
||||
|
||||
@@ -25,19 +25,19 @@ interface RepositoryInterface {
|
||||
/**
|
||||
* @param string $class
|
||||
* @param array|object $data
|
||||
* @param bool $onlyNotNull
|
||||
* @return mixed
|
||||
* @throws RepositoryException
|
||||
*/
|
||||
public function createFromData(string $class, array|object $data) : mixed;
|
||||
public function createFromData(string $class, array|object $data, bool $onlyNotNull = false) : mixed;
|
||||
|
||||
|
||||
/**
|
||||
* @param object $object
|
||||
* @param array|object $data
|
||||
* @param bool $onlyNotNull
|
||||
* @return mixed
|
||||
* @throws RepositoryException
|
||||
*/
|
||||
public function updateFromData(object $object, array|object $data) : mixed;
|
||||
public function updateFromData(object $object, array|object $data, bool $onlyNotNull = false) : mixed;
|
||||
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user