diff --git a/src/Attribute/Entity.php b/src/Attribute/Entity.php index 5e98c7a..1ec1550 100644 --- a/src/Attribute/Entity.php +++ b/src/Attribute/Entity.php @@ -8,7 +8,7 @@ use Attribute; class Entity { public function __construct( - public bool $withoutEmpty = false, + public bool $ignorEmpty = false, ) {} } diff --git a/src/Attribute/EntityWithoutEmpty.php b/src/Attribute/EntityIgnorEmpty.php similarity index 77% rename from src/Attribute/EntityWithoutEmpty.php rename to src/Attribute/EntityIgnorEmpty.php index 415fe4b..bd46eee 100644 --- a/src/Attribute/EntityWithoutEmpty.php +++ b/src/Attribute/EntityIgnorEmpty.php @@ -5,4 +5,4 @@ namespace Rmphp\Storage\Attribute; use Attribute; #[Attribute(Attribute::TARGET_CLASS)] -class EntityWithoutEmpty {} +class EntityIgnorEmpty {} diff --git a/src/Attribute/EntityNoReturnIfNull.php b/src/Attribute/EntityNoReturnIfNull.php new file mode 100644 index 0000000..2d3d986 --- /dev/null +++ b/src/Attribute/EntityNoReturnIfNull.php @@ -0,0 +1,8 @@ +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)){ diff --git a/src/Repository/AbstractRepository.php b/src/Repository/AbstractRepository.php index 8ea23d4..17786f1 100644 --- a/src/Repository/AbstractRepository.php +++ b/src/Repository/AbstractRepository.php @@ -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) {