From 09e8520dedf4250396fba6527c7f5b4200e4b1e8 Mon Sep 17 00:00:00 2001 From: User Date: Sun, 30 Mar 2025 20:19:12 +0300 Subject: [PATCH] 20250330#6 --- src/Attribute/ValueObject.php | 2 +- ...tyName.php => ValueObjectAutoPropertyName.php} | 2 +- src/Repository/AbstractRepository.php | 15 +++++++++++---- 3 files changed, 13 insertions(+), 6 deletions(-) rename src/Attribute/{ValueObjectWithoutAutoPropertyName.php => ValueObjectAutoPropertyName.php} (68%) diff --git a/src/Attribute/ValueObject.php b/src/Attribute/ValueObject.php index 1792ce0..d34b48f 100644 --- a/src/Attribute/ValueObject.php +++ b/src/Attribute/ValueObject.php @@ -8,7 +8,7 @@ use Attribute; class ValueObject { public function __construct( - public bool $autoPropertyName = true, + public bool $autoPropertyName = false, public ?string $propertyName = null, ) {} diff --git a/src/Attribute/ValueObjectWithoutAutoPropertyName.php b/src/Attribute/ValueObjectAutoPropertyName.php similarity index 68% rename from src/Attribute/ValueObjectWithoutAutoPropertyName.php rename to src/Attribute/ValueObjectAutoPropertyName.php index 3e436b4..3e6c4be 100644 --- a/src/Attribute/ValueObjectWithoutAutoPropertyName.php +++ b/src/Attribute/ValueObjectAutoPropertyName.php @@ -5,4 +5,4 @@ namespace Rmphp\Storage\Attribute; use Attribute; #[Attribute(Attribute::TARGET_CLASS)] -class ValueObjectWithoutAutoPropertyName {} +class ValueObjectAutoPropertyName {} diff --git a/src/Repository/AbstractRepository.php b/src/Repository/AbstractRepository.php index af29b7d..813d2af 100644 --- a/src/Repository/AbstractRepository.php +++ b/src/Repository/AbstractRepository.php @@ -10,7 +10,7 @@ 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\Attribute\ValueObjectAutoPropertyName; use Rmphp\Storage\Component\AbstractDataObject; use Rmphp\Storage\Exception\RepositoryException; @@ -62,19 +62,26 @@ 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(self::$classes[$valueObjectClass]->getAttributes(ValueObjectAutoPropertyName::class))) $valueObjectAttributes->autoPropertyName = true; if(!empty($valueObjectAttributes->propertyName) && self::$classes[$valueObjectClass]->hasProperty($valueObjectAttributes->propertyName)){ if(self::$classes[$valueObjectClass]->getProperty($valueObjectAttributes->propertyName)->isInitialized($property->getValue($object))){ $fieldValue[$property->getName()] = self::$classes[$valueObjectClass]->getProperty($valueObjectAttributes->propertyName)->getValue($property->getValue($object)); } - } elseif(!empty($valueObjectAttributes->autoPropertyName) && count(self::$classes[$valueObjectClass]->getProperties()) === 1){ + } + elseif(!empty($valueObjectAttributes->autoPropertyName) && count(self::$classes[$valueObjectClass]->getProperties()) === 1){ if(self::$classes[$valueObjectClass]->getProperties()[0]->isInitialized($property->getValue($object))){ $fieldValue[$property->getName()] = self::$classes[$valueObjectClass]->getProperties()[0]->getValue($property->getValue($object)); } - } elseif(self::$classes[$valueObjectClass]->hasMethod('getValue')){ + } + elseif(self::$classes[$valueObjectClass]->hasMethod('getValue')){ $fieldValue[$property->getName()] = $property->getValue($object)->getValue(); } + elseif(count(self::$classes[$valueObjectClass]->getProperties()) === 1){ + if(self::$classes[$valueObjectClass]->getProperties()[0]->isInitialized($property->getValue($object))){ + $fieldValue[$property->getName()] = self::$classes[$valueObjectClass]->getProperties()[0]->getValue($property->getValue($object)); + } + } } elseif(is_bool($property->getValue($object))){ $fieldValue[$property->getName()] = (int)$property->getValue($object);