20250330#3
This commit is contained in:
@@ -8,7 +8,7 @@ use Attribute;
|
||||
class Entity {
|
||||
|
||||
public function __construct(
|
||||
public bool $withoutEmpty = false,
|
||||
public bool $ignorEmpty = false,
|
||||
) {}
|
||||
|
||||
}
|
||||
|
||||
@@ -5,4 +5,4 @@ namespace Rmphp\Storage\Attribute;
|
||||
use Attribute;
|
||||
|
||||
#[Attribute(Attribute::TARGET_CLASS)]
|
||||
class EntityWithoutEmpty {}
|
||||
class EntityIgnorEmpty {}
|
||||
8
src/Attribute/EntityNoReturnIfNull.php
Normal file
8
src/Attribute/EntityNoReturnIfNull.php
Normal file
@@ -0,0 +1,8 @@
|
||||
<?php
|
||||
|
||||
namespace Rmphp\Storage\Attribute;
|
||||
|
||||
use Attribute;
|
||||
|
||||
#[Attribute(Attribute::TARGET_CLASS)]
|
||||
class EntityNoReturnIfNull {}
|
||||
@@ -1,8 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Rmphp\Storage\Attribute;
|
||||
|
||||
use Attribute;
|
||||
|
||||
#[Attribute(Attribute::TARGET_PROPERTY|Attribute::TARGET_CLASS)]
|
||||
class GetPropertyEmptyIfNull {}
|
||||
@@ -5,7 +5,7 @@ namespace Rmphp\Storage\Attribute;
|
||||
use Attribute;
|
||||
|
||||
#[Attribute(Attribute::TARGET_PROPERTY)]
|
||||
class GetProperty {
|
||||
class Property {
|
||||
|
||||
public function __construct(
|
||||
public ?string $keyName = null,
|
||||
@@ -5,4 +5,4 @@ namespace Rmphp\Storage\Attribute;
|
||||
use Attribute;
|
||||
|
||||
#[Attribute(Attribute::TARGET_PROPERTY)]
|
||||
class GetPropertyEmpty {}
|
||||
class PropertyNoReturn {}
|
||||
8
src/Attribute/PropertyNoReturnIfNull.php
Normal file
8
src/Attribute/PropertyNoReturnIfNull.php
Normal file
@@ -0,0 +1,8 @@
|
||||
<?php
|
||||
|
||||
namespace Rmphp\Storage\Attribute;
|
||||
|
||||
use Attribute;
|
||||
|
||||
#[Attribute(Attribute::TARGET_PROPERTY)]
|
||||
class PropertyNoReturnIfNull {}
|
||||
8
src/Attribute/ValueObjectWithoutAutoPropertyName.php
Normal file
8
src/Attribute/ValueObjectWithoutAutoPropertyName.php
Normal file
@@ -0,0 +1,8 @@
|
||||
<?php
|
||||
|
||||
namespace Rmphp\Storage\Attribute;
|
||||
|
||||
use Attribute;
|
||||
|
||||
#[Attribute(Attribute::TARGET_CLASS)]
|
||||
class ValueObjectWithoutAutoPropertyName {}
|
||||
@@ -6,7 +6,7 @@ use Exception;
|
||||
use ReflectionClass;
|
||||
use ReflectionException;
|
||||
use Rmphp\Storage\Attribute\Entity;
|
||||
use Rmphp\Storage\Attribute\EntityWithoutEmpty;
|
||||
use Rmphp\Storage\Attribute\EntityIgnorEmpty;
|
||||
|
||||
abstract class AbstractDataObject {
|
||||
|
||||
@@ -33,7 +33,7 @@ abstract class AbstractDataObject {
|
||||
}
|
||||
/** @var Entity $entityAttributes */
|
||||
$entityAttributes = self::$attributeObjects[$class->getName()][0];
|
||||
if(!empty($class->getAttributes(EntityWithoutEmpty::class))) $entityAttributes->withoutEmpty = true;
|
||||
if(!empty($class->getAttributes(EntityIgnorEmpty::class))) $entityAttributes->ignorEmpty = true;
|
||||
|
||||
$value = [];
|
||||
foreach($class->getProperties() as $property){
|
||||
@@ -68,16 +68,15 @@ abstract class AbstractDataObject {
|
||||
$object->{$property->getName()} = new ($property->getType()->getName())($value[$property->getName()]);
|
||||
$case[$property->getName()] = 'VO: NewInstance';
|
||||
}
|
||||
elseif(($withEmpty && empty($entityAttributes->withoutEmpty)) && array_key_exists($property->getName(), $value)){
|
||||
elseif(($withEmpty && empty($entityAttributes->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->withoutEmpty)) && self::isEmptyAvailable($property->getType()->getName())) {
|
||||
elseif(($withEmpty && empty($entityAttributes->ignorEmpty)) && 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)){
|
||||
|
||||
@@ -2,19 +2,18 @@
|
||||
|
||||
namespace Rmphp\Storage\Repository;
|
||||
|
||||
use App\Domain\ValueObject\Price;
|
||||
use Exception;
|
||||
use ReflectionClass;
|
||||
use Rmphp\Storage\Attribute\Entity;
|
||||
use Rmphp\Storage\Attribute\EntityWithoutEmpty;
|
||||
use Rmphp\Storage\Attribute\GetProperty;
|
||||
use Rmphp\Storage\Attribute\GetPropertyEmpty;
|
||||
use Rmphp\Storage\Attribute\GetPropertyEmptyIfNull;
|
||||
use Rmphp\Storage\Attribute\EntityIgnorEmpty;
|
||||
use Rmphp\Storage\Attribute\EntityNoReturnIfNull;
|
||||
use Rmphp\Storage\Attribute\Property;
|
||||
use Rmphp\Storage\Attribute\PropertyNoReturn;
|
||||
use Rmphp\Storage\Attribute\PropertyNoReturnIfNull;
|
||||
use Rmphp\Storage\Attribute\ValueObject;
|
||||
use Rmphp\Storage\Attribute\ValueObjectWithoutAutoPropertyName;
|
||||
use Rmphp\Storage\Component\AbstractDataObject;
|
||||
use Rmphp\Storage\Entity\ValueObjectInterface;
|
||||
use Rmphp\Storage\Exception\RepositoryException;
|
||||
use Throwable;
|
||||
|
||||
abstract class AbstractRepository extends AbstractDataObject implements RepositoryInterface {
|
||||
|
||||
@@ -31,22 +30,22 @@ abstract class AbstractRepository extends AbstractDataObject implements Reposito
|
||||
}
|
||||
/** @var Entity $entityAttributes */
|
||||
$entityAttributes = self::$attributeObjects[$class][0];
|
||||
if(!empty(self::$classes[$class]->getAttributes(EntityWithoutEmpty::class))) $entityAttributes->withoutEmpty = true;
|
||||
if(!empty(self::$classes[$class]->getAttributes(GetPropertyEmptyIfNull::class))) $entityAttributes->withoutEmpty = true;
|
||||
if(!empty(self::$classes[$class]->getAttributes(EntityIgnorEmpty::class))) $entityAttributes->ignorEmpty = true;
|
||||
if(!empty(self::$classes[$class]->getAttributes(EntityNoReturnIfNull::class))) $entityAttributes->ignorEmpty = true;
|
||||
|
||||
$fieldValue = [];
|
||||
foreach(self::$classes[$class]->getProperties() as $property){
|
||||
|
||||
if(!isset(self::$attributeObjects[$class][$property->getName()])){
|
||||
self::$attributeObjects[$class][$property->getName()] = !empty($property->getAttributes(GetProperty::class))
|
||||
? $property->getAttributes(GetProperty::class)[0]->newInstance()
|
||||
: new GetProperty();
|
||||
self::$attributeObjects[$class][$property->getName()] = !empty($property->getAttributes(Property::class))
|
||||
? $property->getAttributes(Property::class)[0]->newInstance()
|
||||
: new Property();
|
||||
}
|
||||
/** @var GetProperty $getPropertyAttributes */
|
||||
$getPropertyAttributes = self::$attributeObjects[$class][$property->getName()];
|
||||
if(!empty($property->getAttributes(GetPropertyEmptyIfNull::class))) $getPropertyAttributes->emptyIfNull = true;
|
||||
/** @var Property $propertyAttributes */
|
||||
$propertyAttributes = self::$attributeObjects[$class][$property->getName()];
|
||||
if(!empty($property->getAttributes(PropertyNoReturnIfNull::class))) $propertyAttributes->emptyIfNull = true;
|
||||
|
||||
if(!empty($property->getAttributes(GetPropertyEmpty::class)) || !empty($getPropertyAttributes->empty)) continue;
|
||||
if(!empty($property->getAttributes(PropertyNoReturn::class)) || !empty($propertyAttributes->empty)) continue;
|
||||
|
||||
if($property->isInitialized($object)) {
|
||||
|
||||
@@ -55,8 +54,7 @@ abstract class AbstractRepository extends AbstractDataObject implements Reposito
|
||||
if(self::$classes[$class]->hasMethod('get'.ucfirst($property->getName()))){
|
||||
$fieldValue[$property->getName()] = $object->{'get'.ucfirst($property->getName())}($property->getValue($object));
|
||||
}
|
||||
elseif($property->hasType() && class_exists($property->getType()->getName()) && $property->getValue($object) instanceof ValueObjectInterface){
|
||||
|
||||
elseif($property->hasType() && class_exists($property->getType()->getName())){
|
||||
$valueObjectClass = get_class($property->getValue($object));
|
||||
if(!isset(self::$classes[$valueObjectClass])) self::$classes[$valueObjectClass] = new ReflectionClass($valueObjectClass);
|
||||
|
||||
@@ -66,6 +64,7 @@ abstract class AbstractRepository extends AbstractDataObject implements Reposito
|
||||
: new ValueObject();
|
||||
}
|
||||
$valueObjectAttributes = self::$attributeObjects[$valueObjectClass];
|
||||
if(!empty(self::$classes[$valueObjectClass]->getAttributes(ValueObjectWithoutAutoPropertyName::class))) $valueObjectAttributes->autoPropertyName = false;
|
||||
|
||||
if(!empty($valueObjectAttributes->propertyName) && self::$classes[$valueObjectClass]->hasProperty($valueObjectAttributes->propertyName)){
|
||||
if(self::$classes[$valueObjectClass]->getProperty($valueObjectAttributes->propertyName)->isInitialized($property->getValue($object))){
|
||||
@@ -75,7 +74,7 @@ abstract class AbstractRepository extends AbstractDataObject implements Reposito
|
||||
if(self::$classes[$valueObjectClass]->getProperties()[0]->isInitialized($property->getValue($object))){
|
||||
$fieldValue[$property->getName()] = self::$classes[$valueObjectClass]->getProperties()[0]->getValue($property->getValue($object));
|
||||
}
|
||||
} else{
|
||||
} elseif(self::$classes[$valueObjectClass]->hasMethod('getValue')){
|
||||
$fieldValue[$property->getName()] = $property->getValue($object)->getValue();
|
||||
}
|
||||
}
|
||||
@@ -86,15 +85,14 @@ abstract class AbstractRepository extends AbstractDataObject implements Reposito
|
||||
$fieldValue[$property->getName()] = $property->getValue($object);
|
||||
}
|
||||
|
||||
if(!isset($fieldValue[$property->getName()]) && (!empty($getPropertyAttributes->emptyIfNull) || !empty($entityAttributes->withoutEmpty))) continue;
|
||||
if(!isset($fieldValue[$property->getName()]) && (!empty($propertyAttributes->emptyIfNull) || !empty($entityAttributes->ignorEmpty))) continue;
|
||||
|
||||
if(array_key_exists($property->getName(), $fieldValue) && false !== $fieldValue[$property->getName()]) {
|
||||
$columnName = !empty($getPropertyAttributes->keyName) ? $getPropertyAttributes->keyName : strtolower(preg_replace("'([A-Z])'", "_$1", $property->getName()));
|
||||
$columnName = !empty($propertyAttributes->keyName) ? $propertyAttributes->keyName : strtolower(preg_replace("'([A-Z])'", "_$1", $property->getName()));
|
||||
$out[$columnName] = $fieldValue[$property->getName()];
|
||||
}
|
||||
}
|
||||
}
|
||||
//dd($this->getAttributesObjectsCache());
|
||||
return (isset($method)) ? array_map($method, $out ?? []) : $out ?? [];
|
||||
}
|
||||
catch (\ReflectionException $exception) {
|
||||
|
||||
Reference in New Issue
Block a user