From f42bc7751a7a45b2cff1b1e32c3c31d08f18ed7e Mon Sep 17 00:00:00 2001 From: User Date: Sun, 22 Jun 2025 01:05:12 +0300 Subject: [PATCH] 20250622#1 --- composer.json | 1 + src/AbstractMysqlRepository.php | 6 +- src/Attribute/Data.php | 14 -- src/Attribute/DataIgnorEmpty.php | 8 - src/Attribute/Entity.php | 14 -- src/Attribute/EntityNoReturnIfNull.php | 8 - src/Attribute/Property.php | 16 -- src/Attribute/PropertyNoReturn.php | 8 - src/Attribute/PropertyNoReturnIfNull.php | 8 - src/Attribute/ValueObject.php | 15 -- src/Attribute/ValueObjectFirstProperty.php | 8 - src/Attribute/ValueObjectPropertyName.php | 14 -- src/Component/AbstractDataObject.php | 134 -------------- src/Mysql/MysqlRepositoryInterface.php | 4 +- src/{Entity => Repository}/AbstractEntity.php | 2 +- src/Repository/AbstractRepository.php | 169 ------------------ .../EntityInterface.php | 2 +- .../RepositoryException.php | 2 +- src/Repository/RepositoryInterface.php | 2 - .../ValueObjectInterface.php | 2 +- 20 files changed, 10 insertions(+), 427 deletions(-) delete mode 100644 src/Attribute/Data.php delete mode 100644 src/Attribute/DataIgnorEmpty.php delete mode 100644 src/Attribute/Entity.php delete mode 100644 src/Attribute/EntityNoReturnIfNull.php delete mode 100644 src/Attribute/Property.php delete mode 100644 src/Attribute/PropertyNoReturn.php delete mode 100644 src/Attribute/PropertyNoReturnIfNull.php delete mode 100644 src/Attribute/ValueObject.php delete mode 100644 src/Attribute/ValueObjectFirstProperty.php delete mode 100644 src/Attribute/ValueObjectPropertyName.php delete mode 100644 src/Component/AbstractDataObject.php rename src/{Entity => Repository}/AbstractEntity.php (94%) delete mode 100644 src/Repository/AbstractRepository.php rename src/{Entity => Repository}/EntityInterface.php (83%) rename src/{Exception => Repository}/RepositoryException.php (88%) rename src/{Entity => Repository}/ValueObjectInterface.php (70%) diff --git a/composer.json b/composer.json index 2a3c8d5..264e96f 100644 --- a/composer.json +++ b/composer.json @@ -9,6 +9,7 @@ ], "require": { "php": "^8.1", + "rmphp/odm": "^1.0", "ext-mysqli": "*" }, "autoload": { diff --git a/src/AbstractMysqlRepository.php b/src/AbstractMysqlRepository.php index 37afcf7..2d79eda 100644 --- a/src/AbstractMysqlRepository.php +++ b/src/AbstractMysqlRepository.php @@ -2,12 +2,12 @@ namespace Rmphp\Storage; -use Rmphp\Storage\Entity\EntityInterface; -use Rmphp\Storage\Exception\RepositoryException; +use Rmphp\ODM\AbstractRepository; use Rmphp\Storage\Mysql\MysqlRepositoryInterface; use Rmphp\Storage\Mysql\MysqlResultData; use Rmphp\Storage\Mysql\MysqlStorageInterface; -use Rmphp\Storage\Repository\AbstractRepository; +use Rmphp\Storage\Repository\EntityInterface; +use Rmphp\Storage\Repository\RepositoryException; abstract class AbstractMysqlRepository extends AbstractRepository implements MysqlRepositoryInterface { diff --git a/src/Attribute/Data.php b/src/Attribute/Data.php deleted file mode 100644 index 9760fde..0000000 --- a/src/Attribute/Data.php +++ /dev/null @@ -1,14 +0,0 @@ -getName()][0])){ - self::$attributeObjects[$class->getName()][0] = !empty($class->getAttributes(Data::class)) - ? $class->getAttributes(Data::class)[0]->newInstance() - : new Data(); - } - /** @var Data $dataAttributes */ - $dataAttributes = self::$attributeObjects[$class->getName()][0]; - if(!empty($class->getAttributes(DataIgnorEmpty::class))) $dataAttributes->ignorEmpty = true; - - $value = []; - foreach($class->getProperties() as $property){ - $prop[$property->getName()] = ($property->hasType()) ? $property->getType()->getName() : ""; - - // значение в массиве по ключю с именем свойства - if(array_key_exists($property->getName(), $data)){ - $value[$property->getName()] = $data[$property->getName()]; - } - // значение в массиве по ключю с именем свойства в snake case - elseif(array_key_exists(strtolower(preg_replace("'([A-Z])'", "_$1", $property->getName())), $data)){ - $value[$property->getName()] = $data[strtolower(preg_replace("'([A-Z])'", "_$1", $property->getName()))]; - } - elseif($update) { - continue; - } - - // если есть внутренний метод (приоритетная обработка) - if($class->hasMethod('set'.ucfirst($property->getName()))) { - $object->{'set'.ucfirst($property->getName())}($value[$property->getName()] ?? null); - $case[$property->getName()] = 'Method set'.ucfirst($property->getName()); - } - // Если тип свойства класс (valueObject) - elseif($property->hasType() && class_exists($property->getType()->getName())) { - // значение объект - if(isset($value[$property->getName()]) && is_object($value[$property->getName()])){ - $object->{$property->getName()} = $value[$property->getName()]; - $case[$property->getName()] = 'VO: Object'; - } - // значение не пустое - elseif(isset($value[$property->getName()]) && $value[$property->getName()] !== ""){ - $object->{$property->getName()} = new ($property->getType()->getName())($value[$property->getName()]); - $case[$property->getName()] = 'VO: NewInstance'; - } - // Значения нет и VO может быть без параметров - elseif(($withEmpty && empty($dataAttributes->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)){ - if(!$property->hasType()){ - $object->{$property->getName()} = $value[$property->getName()]; - $case[$property->getName()] = 'Base: Hasn`t type'; - } - elseif(in_array($property->getType()->getName(), ['float', 'int'])){ - if(is_numeric($value[$property->getName()])) $object->{$property->getName()} = $value[$property->getName()]; - $case[$property->getName()] = 'Base: Number'; - } - elseif($property->getType()->getName() == 'bool'){ - $object->{$property->getName()} = (bool)$value[$property->getName()]; - $case[$property->getName()] = 'Base: Boolean'; - } - elseif(isset($value[$property->getName()])){ - $object->{$property->getName()} = $value[$property->getName()]; - $case[$property->getName()] = 'Base: NotNull'; - } - elseif($property->getType()->allowsNull()){ - $object->{$property->getName()} = $value[$property->getName()]; - $case[$property->getName()] = 'Base: Null'; - } - } - } - self::$stack[$object::class." #".spl_object_id($object)]['properties'] = $prop ?? []; - self::$stack[$object::class." #".spl_object_id($object)]['values'] = $value; - self::$stack[$object::class." #".spl_object_id($object)]['matchCase'] = $case ?? []; - self::$stack[$object::class." #".spl_object_id($object)]['object'] = $object; - return $object; - } - catch (ReflectionException $exception) { - throw new Exception($exception->getMessage()); - } - } - - - /** - * @throws ReflectionException - */ - private static function isEmptyAvailable(string $class) : bool { - if(isset(self::$constructorEmptyAvailableClasses[$class])) return self::$constructorEmptyAvailableClasses[$class]; - if(!$constructor = (new \ReflectionClass($class))->getConstructor()) return self::$constructorEmptyAvailableClasses[$class] = false; - foreach($constructor->getParameters() as $param){ - if(!$param->isDefaultValueAvailable()){ - return self::$constructorEmptyAvailableClasses[$class] = false; - } - } - return self::$constructorEmptyAvailableClasses[$class] = true; - } - - /** - * @return array - */ - protected function getFillObjectStack() : array { - return self::$stack; - } - -} diff --git a/src/Mysql/MysqlRepositoryInterface.php b/src/Mysql/MysqlRepositoryInterface.php index 47fe683..e65b1b5 100644 --- a/src/Mysql/MysqlRepositoryInterface.php +++ b/src/Mysql/MysqlRepositoryInterface.php @@ -8,8 +8,8 @@ namespace Rmphp\Storage\Mysql; -use Rmphp\Storage\Entity\EntityInterface; -use Rmphp\Storage\Exception\RepositoryException; +use Rmphp\Storage\Repository\EntityInterface; +use Rmphp\Storage\Repository\RepositoryException; use Rmphp\Storage\Repository\RepositoryInterface; interface MysqlRepositoryInterface extends RepositoryInterface { diff --git a/src/Entity/AbstractEntity.php b/src/Repository/AbstractEntity.php similarity index 94% rename from src/Entity/AbstractEntity.php rename to src/Repository/AbstractEntity.php index e2b7b48..79ad16c 100644 --- a/src/Entity/AbstractEntity.php +++ b/src/Repository/AbstractEntity.php @@ -1,6 +1,6 @@ getAttributes(Entity::class)) - ? self::$classes[$class]->getAttributes(Entity::class)[0]->newInstance() - : new Entity(); - } - /** @var Entity $entityAttributes */ - $entityAttributes = self::$attributeObjects[$class][0]; - if(!empty(self::$classes[$class]->getAttributes(EntityNoReturnIfNull::class))) $entityAttributes->noReturnIfNull = true; - - $fieldValue = []; - foreach(self::$classes[$class]->getProperties() as $property){ - - if(!isset(self::$attributeObjects[$class][$property->getName()])){ - self::$attributeObjects[$class][$property->getName()] = !empty($property->getAttributes(Property::class)) - ? $property->getAttributes(Property::class)[0]->newInstance() - : new Property(); - } - /** @var Property $propertyAttributes */ - $propertyAttributes = self::$attributeObjects[$class][$property->getName()]; - if(!empty($property->getAttributes(PropertyNoReturnIfNull::class))) $propertyAttributes->noReturnIfNull = true; - - if(!empty($property->getAttributes(PropertyNoReturn::class)) || !empty($propertyAttributes->noReturn)) continue; - - if($property->isInitialized($object)) { - - if(is_array($property->getValue($object))) continue; - - 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())){ - $valueObjectClass = get_class($property->getValue($object)); - if(!isset(self::$classes[$valueObjectClass])) self::$classes[$valueObjectClass] = new ReflectionClass($valueObjectClass); - - if(!isset(self::$attributeObjects[$valueObjectClass])){ - self::$attributeObjects[$valueObjectClass] = !empty(self::$classes[$valueObjectClass]->getAttributes(ValueObject::class)) - ? self::$classes[$valueObjectClass]->getAttributes(ValueObject::class)[0]->newInstance() - : new ValueObject(); - if(!empty(self::$classes[$valueObjectClass]->getAttributes(ValueObjectFirstProperty::class))) { - self::$attributeObjects[$valueObjectClass]->firstProperty = true; - } - if(!empty(self::$classes[$valueObjectClass]->getAttributes(ValueObjectPropertyName::class))) { - $propertyName = self::$classes[$valueObjectClass]->getAttributes(ValueObjectPropertyName::class)[0]->newInstance(); - if(isset($propertyName->name)) self::$attributeObjects[$valueObjectClass]->propertyName = $propertyName->name; - } - } - $valueObjectAttributes = self::$attributeObjects[$valueObjectClass]; - - 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->firstProperty) && count(self::$classes[$valueObjectClass]->getProperties()) > 0){ - 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')){ - $fieldValue[$property->getName()] = $property->getValue($object)->getValue(); - } - } - elseif(is_bool($property->getValue($object))){ - $fieldValue[$property->getName()] = (int)$property->getValue($object); - } - else{ - $fieldValue[$property->getName()] = $property->getValue($object); - } - - 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())); - $out[$columnName] = $fieldValue[$property->getName()]; - } - } - } - return (isset($method)) ? array_map($method, $out ?? []) : $out ?? []; - } - catch (\ReflectionException $exception) { - throw new RepositoryException($exception->getMessage()); - } - } - - - /** - * @inheritDoc - * @throws RepositoryException - */ - public function createFromData(string $class, array|object $data, bool $withEmpty = true) : object { - try { - if(!isset(self::$classes[$class])) self::$classes[$class] = new ReflectionClass($class); - return self::fillObject(self::$classes[$class], new $class, (is_object($data)) ? get_object_vars($data) : $data, false, $withEmpty); - } - catch (Exception $exception) { - throw new RepositoryException($exception->getMessage()); - } - } - - - /** - * @inheritDoc - * @throws RepositoryException - */ - public function updateFromData(object $object, array|object $data, bool $withEmpty = true) : object { - try { - $class = get_class($object); - if(!isset(self::$classes[$class])) self::$classes[$class] = new ReflectionClass($class); - return self::fillObject(self::$classes[$class], clone $object, (is_object($data)) ? get_object_vars($data) : $data, true, $withEmpty); - } - catch (Exception $exception) { - throw new RepositoryException($exception->getMessage()); - } - } - - /** - * @return array - */ - public function getRepositoryStack() : array { - return $this->getFillObjectStack(); - } - - /** - * @return array - */ - public function getClassesCache() : array { - return self::$classes; - } - - /** - * @return array - */ - public function getAttributesObjectsCache() : array { - return self::$attributeObjects; - } - - /** - * @return array - */ - public function getConstructorEmptyAvailableClassesCache() : array { - return self::$constructorEmptyAvailableClasses; - } - -} diff --git a/src/Entity/EntityInterface.php b/src/Repository/EntityInterface.php similarity index 83% rename from src/Entity/EntityInterface.php rename to src/Repository/EntityInterface.php index 715c7d0..2e5fc3b 100644 --- a/src/Entity/EntityInterface.php +++ b/src/Repository/EntityInterface.php @@ -6,7 +6,7 @@ * Time: 3:58 */ -namespace Rmphp\Storage\Entity; +namespace Rmphp\Storage\Repository; interface EntityInterface { diff --git a/src/Exception/RepositoryException.php b/src/Repository/RepositoryException.php similarity index 88% rename from src/Exception/RepositoryException.php rename to src/Repository/RepositoryException.php index f32707d..8adfb77 100644 --- a/src/Exception/RepositoryException.php +++ b/src/Repository/RepositoryException.php @@ -1,6 +1,6 @@