From 054e6a7cdc88f427252970a52a9e60954efd7f4a Mon Sep 17 00:00:00 2001 From: babaev-an Date: Fri, 21 Feb 2025 18:33:23 +0300 Subject: [PATCH] 20250221 --- sources/classes/ClassMapper.php | 70 +++++++++++++++++++++++-------- tests/classes/ClassMapperTest.php | 5 ++- 2 files changed, 57 insertions(+), 18 deletions(-) diff --git a/sources/classes/ClassMapper.php b/sources/classes/ClassMapper.php index 1d7cfa3..e185971 100644 --- a/sources/classes/ClassMapper.php +++ b/sources/classes/ClassMapper.php @@ -4,6 +4,7 @@ namespace goodboyalex\php_components_pack\classes; use DateTimeImmutable; use DateTimeInterface; +use Error; use Exception; use ReflectionClass; use ReflectionException; @@ -49,21 +50,51 @@ final class ClassMapper // ---- то исключаю его из массива разрешенных unset($options['allowed'][array_search($ignoredProperty, $options['allowed'])]); - // Получаю массив свойств - $properties = get_class_vars(get_class($from)); + // Задаю массив свойств + $properties = []; + + // Получение всех свойств класса + $reflection = new ReflectionClass(get_class($from)); + $props = $reflection->getProperties(); + + var_dump($props); + + // Для каждого свойства + foreach ($props as $prop) { + $propName = $prop->getName(); + $value = $from->$propName; // Читаем значение свойства + + try { + $getterMethod = 'get' . ucfirst($propName); + $setterMethod = 'set' . ucfirst($propName); + + // Проверяем существование геттера и сеттера методами класса + if (!(method_exists($from, $getterMethod) && method_exists($from, $setterMethod))) + continue; + + // - если свойство игнорируется + if (in_array($propName, $options['ignored'])) + // -- пропускаю + continue; + + // - если свойство не разрешено + if (count($options['allowed']) > 0 && !in_array($propName, $options['allowed'])) + // -- пропускаю + continue; + + // Если не было ошибки, значит свойство имеет и геттер, и сеттер + $properties[$propName] = $value; + } + catch (Error $exception) { + var_dump($exception->getMessage()); + // Игнорируем исключения, так как нас интересуют только свойства с обоими методами + } + } + + var_dump($properties); // Для каждого элемента массива foreach ($properties as $name => $value) { - // - если свойство игнорируется - if (in_array($name, $options['ignored'])) - // -- пропускаю - continue; - - // - если свойство не разрешено - if (count($options['allowed']) > 0 && !in_array($name, $options['allowed'])) - // -- пропускаю - continue; - // - если свойство есть в объекте if (property_exists($to, $name)) // -- то присваиваю значение @@ -80,7 +111,8 @@ final class ClassMapper * * @return array Массив данных класса. */ - public static function PrepareClassProperties (array $params, ReflectionClass $classReflector, + public + static function PrepareClassProperties (array $params, ReflectionClass $classReflector, array $options = self::DefaultOptions): array { // Создаю массив данных класса @@ -140,7 +172,8 @@ final class ClassMapper * * @return void */ - public static function GetClassParametersArrayParser (array &$source, array $parametersKeys, mixed $value, + public + static function GetClassParametersArrayParser (array &$source, array $parametersKeys, mixed $value, array $options = self::DefaultOptions): void { // Если массив имен свойств пустой @@ -189,7 +222,8 @@ final class ClassMapper * @return mixed Объект класса * @throws Exception */ - public static function MapToClassProperty (string $className, array $properties): mixed + public + static function MapToClassProperty (string $className, array $properties): mixed { // Создаю try { @@ -285,7 +319,8 @@ final class ClassMapper * * @throws Exception */ - public static function SetParameterToClass (ReflectionClass $classReflector, string $propertyName, + public + static function SetParameterToClass (ReflectionClass $classReflector, string $propertyName, mixed $classObj, mixed $value): void { try { @@ -320,7 +355,8 @@ final class ClassMapper * * @return mixed|null Результат. */ - public static function GetDefaults (string $typeName): mixed + public + static function GetDefaults (string $typeName): mixed { return match ($typeName) { 'int' => 0, diff --git a/tests/classes/ClassMapperTest.php b/tests/classes/ClassMapperTest.php index 1efbada..81f90b5 100644 --- a/tests/classes/ClassMapperTest.php +++ b/tests/classes/ClassMapperTest.php @@ -3,6 +3,7 @@ namespace goodboyalex\php_components_pack\tests\classes; use goodboyalex\php_components_pack\classes\ClassMapper; +use goodboyalex\php_components_pack\tests\data\A; use PHPUnit\Framework\TestCase; class ClassMapperTest extends TestCase @@ -11,7 +12,7 @@ class ClassMapperTest extends TestCase { $this->PrepareForTest(); - $a = new \goodboyalex\php_components_pack\tests\data\A(); + $a = new A(); $a->a = 'a'; $a->b = 2; $a->c = true; @@ -19,6 +20,8 @@ class ClassMapperTest extends TestCase $b = new B(); ClassMapper::MapClass($a, $b); + var_dump($b); + $this->assertEquals('a', $b->a); $this->assertEquals(2, $b->b); }