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