20250221
This commit is contained in:
		| @@ -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, | ||||
|   | ||||
		Reference in New Issue
	
	Block a user