20250330#4
This commit is contained in:
14
src/Attribute/Data.php
Normal file
14
src/Attribute/Data.php
Normal file
@@ -0,0 +1,14 @@
|
||||
<?php
|
||||
|
||||
namespace Rmphp\Storage\Attribute;
|
||||
|
||||
use Attribute;
|
||||
|
||||
#[Attribute(Attribute::TARGET_CLASS)]
|
||||
class Data {
|
||||
|
||||
public function __construct(
|
||||
public bool $ignorEmpty = false,
|
||||
) {}
|
||||
|
||||
}
|
||||
@@ -5,4 +5,4 @@ namespace Rmphp\Storage\Attribute;
|
||||
use Attribute;
|
||||
|
||||
#[Attribute(Attribute::TARGET_CLASS)]
|
||||
class EntityIgnorEmpty {}
|
||||
class DataIgnorEmpty {}
|
||||
@@ -8,7 +8,7 @@ use Attribute;
|
||||
class Entity {
|
||||
|
||||
public function __construct(
|
||||
public bool $ignorEmpty = false,
|
||||
public bool $noReturnIfNull = false,
|
||||
) {}
|
||||
|
||||
}
|
||||
|
||||
@@ -9,8 +9,8 @@ class Property {
|
||||
|
||||
public function __construct(
|
||||
public ?string $keyName = null,
|
||||
public bool $empty = false,
|
||||
public bool $emptyIfNull = false,
|
||||
public bool $noReturn = false,
|
||||
public bool $noReturnIfNull = false,
|
||||
) {}
|
||||
|
||||
}
|
||||
|
||||
@@ -5,8 +5,8 @@ namespace Rmphp\Storage\Component;
|
||||
use Exception;
|
||||
use ReflectionClass;
|
||||
use ReflectionException;
|
||||
use Rmphp\Storage\Attribute\Entity;
|
||||
use Rmphp\Storage\Attribute\EntityIgnorEmpty;
|
||||
use Rmphp\Storage\Attribute\Data;
|
||||
use Rmphp\Storage\Attribute\DataIgnorEmpty;
|
||||
|
||||
abstract class AbstractDataObject {
|
||||
|
||||
@@ -27,13 +27,13 @@ abstract class AbstractDataObject {
|
||||
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();
|
||||
self::$attributeObjects[$class->getName()][0] = !empty($class->getAttributes(Data::class))
|
||||
? $class->getAttributes(Data::class)[0]->newInstance()
|
||||
: new Data();
|
||||
}
|
||||
/** @var Entity $entityAttributes */
|
||||
$entityAttributes = self::$attributeObjects[$class->getName()][0];
|
||||
if(!empty($class->getAttributes(EntityIgnorEmpty::class))) $entityAttributes->ignorEmpty = true;
|
||||
/** @var Data $dataAttributes */
|
||||
$dataAttributes = self::$attributeObjects[$class->getName()][0];
|
||||
if(!empty($class->getAttributes(DataIgnorEmpty::class))) $dataAttributes->ignorEmpty = true;
|
||||
|
||||
$value = [];
|
||||
foreach($class->getProperties() as $property){
|
||||
@@ -68,12 +68,12 @@ abstract class AbstractDataObject {
|
||||
$object->{$property->getName()} = new ($property->getType()->getName())($value[$property->getName()]);
|
||||
$case[$property->getName()] = 'VO: NewInstance';
|
||||
}
|
||||
elseif(($withEmpty && empty($entityAttributes->ignorEmpty)) && array_key_exists($property->getName(), $value)){
|
||||
elseif(($withEmpty && empty($dataAttributes->ignorEmpty)) && array_key_exists($property->getName(), $value)){
|
||||
$object->{$property->getName()} = new ($property->getType()->getName())($value[$property->getName()]);
|
||||
$case[$property->getName()] = 'VO: NewInstance withEmpty';
|
||||
}
|
||||
// Значения нет и VO может быть без параметров
|
||||
elseif(($withEmpty && empty($entityAttributes->ignorEmpty)) && self::isEmptyAvailable($property->getType()->getName())) {
|
||||
elseif(($withEmpty && empty($dataAttributes->ignorEmpty)) && self::isEmptyAvailable($property->getType()->getName())) {
|
||||
$object->{$property->getName()} = new ($property->getType()->getName())();
|
||||
$case[$property->getName()] = 'VO: Without params';
|
||||
}
|
||||
|
||||
@@ -5,7 +5,6 @@ namespace Rmphp\Storage\Repository;
|
||||
use Exception;
|
||||
use ReflectionClass;
|
||||
use Rmphp\Storage\Attribute\Entity;
|
||||
use Rmphp\Storage\Attribute\EntityIgnorEmpty;
|
||||
use Rmphp\Storage\Attribute\EntityNoReturnIfNull;
|
||||
use Rmphp\Storage\Attribute\Property;
|
||||
use Rmphp\Storage\Attribute\PropertyNoReturn;
|
||||
@@ -30,8 +29,7 @@ abstract class AbstractRepository extends AbstractDataObject implements Reposito
|
||||
}
|
||||
/** @var Entity $entityAttributes */
|
||||
$entityAttributes = self::$attributeObjects[$class][0];
|
||||
if(!empty(self::$classes[$class]->getAttributes(EntityIgnorEmpty::class))) $entityAttributes->ignorEmpty = true;
|
||||
if(!empty(self::$classes[$class]->getAttributes(EntityNoReturnIfNull::class))) $entityAttributes->ignorEmpty = true;
|
||||
if(!empty(self::$classes[$class]->getAttributes(EntityNoReturnIfNull::class))) $entityAttributes->noReturnIfNull = true;
|
||||
|
||||
$fieldValue = [];
|
||||
foreach(self::$classes[$class]->getProperties() as $property){
|
||||
@@ -43,9 +41,9 @@ abstract class AbstractRepository extends AbstractDataObject implements Reposito
|
||||
}
|
||||
/** @var Property $propertyAttributes */
|
||||
$propertyAttributes = self::$attributeObjects[$class][$property->getName()];
|
||||
if(!empty($property->getAttributes(PropertyNoReturnIfNull::class))) $propertyAttributes->emptyIfNull = true;
|
||||
if(!empty($property->getAttributes(PropertyNoReturnIfNull::class))) $propertyAttributes->noReturnIfNull = true;
|
||||
|
||||
if(!empty($property->getAttributes(PropertyNoReturn::class)) || !empty($propertyAttributes->empty)) continue;
|
||||
if(!empty($property->getAttributes(PropertyNoReturn::class)) || !empty($propertyAttributes->noReturn)) continue;
|
||||
|
||||
if($property->isInitialized($object)) {
|
||||
|
||||
@@ -85,7 +83,7 @@ abstract class AbstractRepository extends AbstractDataObject implements Reposito
|
||||
$fieldValue[$property->getName()] = $property->getValue($object);
|
||||
}
|
||||
|
||||
if(!isset($fieldValue[$property->getName()]) && (!empty($propertyAttributes->emptyIfNull) || !empty($entityAttributes->ignorEmpty))) continue;
|
||||
if(!isset($fieldValue[$property->getName()]) && (!empty($propertyAttributes->noReturnIfNull) || !empty($entityAttributes->noReturnIfNull))) continue;
|
||||
|
||||
if(array_key_exists($property->getName(), $fieldValue) && false !== $fieldValue[$property->getName()]) {
|
||||
$columnName = !empty($propertyAttributes->keyName) ? $propertyAttributes->keyName : strtolower(preg_replace("'([A-Z])'", "_$1", $property->getName()));
|
||||
|
||||
Reference in New Issue
Block a user