20250330#1

This commit is contained in:
User
2025-03-30 13:28:30 +03:00
parent 96ee46476c
commit fd996d40c8
9 changed files with 193 additions and 39 deletions

View File

@@ -5,26 +5,38 @@ namespace Rmphp\Storage\Component;
use Exception;
use ReflectionClass;
use ReflectionException;
use Rmphp\Storage\Attribute\Entity;
class AbstractDataObject {
abstract class AbstractDataObject {
private static array $constructorEmptyAvailableClasses = [];
private static array $stack = [];
protected static array $constructorEmptyAvailableClasses = [];
protected static array $classes = [];
protected static array $attributeObjects = [];
/**
* @param ReflectionClass $class
* @param object $object
* @param array $data
* @param bool $update
* @param bool $withNull
* @param bool $withEmpty
* @return mixed
* @throws Exception
*/
protected static function fillObject(ReflectionClass $class, object $object, array $data, bool $update = false, bool $withNull = true) : mixed {
protected static function fillObject(ReflectionClass $class, object $object, array $data, bool $update = false, bool $withEmpty = true) : mixed {
try {
if(!isset(self::$attributeObjects[$class->getName()][0])){
self::$attributeObjects[$class->getName()][0] = !empty($class->getAttributes(Entity::class))
? $class->getAttributes(Entity::class)[0]->newInstance()
: new Entity();
}
/** @var Entity $entityAttributes */
$entityAttributes = self::$attributeObjects[$class->getName()][0];
$value = [];
foreach($class->getProperties() as $property){
$prop[$property->getName()] = ($property->hasType()) ? $property->getType()->getName() : "";
// значение в массиве по ключю с именем свойства
if(array_key_exists($property->getName(), $data)){
$value[$property->getName()] = $data[$property->getName()];
@@ -40,44 +52,58 @@ class AbstractDataObject {
// если есть внутренний метод (приоритетная обработка)
if($class->hasMethod('set'.ucfirst($property->getName()))) {
$object->{'set'.ucfirst($property->getName())}($value[$property->getName()] ?? null);
$case[$property->getName()] = 'Method set'.ucfirst($property->getName());
}
// Если тип свойства класс (valueObject)
elseif($property->hasType() && class_exists($property->getType()->getName())) {
// значение объект
if(is_object($value[$property->getName()])){
$object->{$property->getName()} = $value[$property->getName()];
$case[$property->getName()] = 'VO: Object';
}
// значение есть и не пустое
elseif(isset($value[$property->getName()]) && $value[$property->getName()] !== ""){
$object->{$property->getName()} = new ($property->getType()->getName())($value[$property->getName()]);
$case[$property->getName()] = 'VO: NewInstance';
}
elseif($withNull && isset($value[$property->getName()])){
elseif(($withEmpty && empty($entityAttributes->withoutEmpty)) && array_key_exists($property->getName(), $value)){
$object->{$property->getName()} = new ($property->getType()->getName())($value[$property->getName()]);
$case[$property->getName()] = 'VO: NewInstance withEmpty';
}
// Значения нет и VO может быть без параметров
elseif($withNull && self::isEmptyAvailable($property->getType()->getName())) {
elseif(($withEmpty && empty($entityAttributes->withoutEmpty)) && self::isEmptyAvailable($property->getType()->getName())) {
$object->{$property->getName()} = new ($property->getType()->getName())();
$case[$property->getName()] = 'VO: Without params';
}
}
// Базовые типы при наличии значения
elseif(array_key_exists($property->getName(), $value)){
if(!$property->hasType()){
$object->{$property->getName()} = $value[$property->getName()];
$case[$property->getName()] = 'Base: Hasn`t type';
}
elseif(in_array($property->getType()->getName(), ['float', 'int'])){
if(is_numeric($value[$property->getName()])) $object->{$property->getName()} = $value[$property->getName()];
$case[$property->getName()] = 'Base: Number';
}
elseif($property->getType()->getName() == 'bool'){
$object->{$property->getName()} = (bool)$value[$property->getName()];
$case[$property->getName()] = 'Base: Boolean';
}
elseif(isset($value[$property->getName()]) && $value[$property->getName()] !== ""){
elseif(isset($value[$property->getName()])){
$object->{$property->getName()} = $value[$property->getName()];
$case[$property->getName()] = 'Base: NotNull';
}
elseif($withNull && isset($value[$property->getName()])){
elseif($property->getType()->allowsNull()){
$object->{$property->getName()} = $value[$property->getName()];
$case[$property->getName()] = 'Base: Null';
}
}
}
self::$stack[$object::class." #".spl_object_id($object)]['properties'] = $prop ?? [];
self::$stack[$object::class." #".spl_object_id($object)]['values'] = $value;
self::$stack[$object::class." #".spl_object_id($object)]['matchCase'] = $case ?? [];
self::$stack[$object::class." #".spl_object_id($object)]['object'] = $object;
return $object;
}
@@ -101,7 +127,6 @@ class AbstractDataObject {
return self::$constructorEmptyAvailableClasses[$class] = true;
}
/**
* @return array
*/