20250223
[О] [ClassMapper::MapClass]: Отменена проверка свойства на доступность get и set, так как выдавала ошибку. Используйте лучше в таких случаях $options['ignored'] для таких свойств.
This commit is contained in:
		| @@ -4,12 +4,10 @@ namespace goodboyalex\php_components_pack\classes; | |||||||
|  |  | ||||||
| use DateTimeImmutable; | use DateTimeImmutable; | ||||||
| use DateTimeInterface; | use DateTimeInterface; | ||||||
| use Error; |  | ||||||
| use Exception; | use Exception; | ||||||
| use ReflectionClass; | use ReflectionClass; | ||||||
| use ReflectionException; | use ReflectionException; | ||||||
| use stdClass; | use stdClass; | ||||||
| use Throwable; |  | ||||||
| use UnitEnum; | use UnitEnum; | ||||||
|  |  | ||||||
| /** | /** | ||||||
| @@ -51,79 +49,21 @@ final class ClassMapper | |||||||
|                     // ---- то исключаю его из массива разрешенных |                     // ---- то исключаю его из массива разрешенных | ||||||
|                     unset($options['allowed'][array_search($ignoredProperty, $options['allowed'])]); |                     unset($options['allowed'][array_search($ignoredProperty, $options['allowed'])]); | ||||||
|  |  | ||||||
|         // Задаю массив свойств |         // Получаю массив свойств | ||||||
|         $properties = []; |         $properties = get_class_vars(get_class($from)); | ||||||
|  |  | ||||||
|         // Получаю имя исходного класса |  | ||||||
|         $className = get_class($from); |  | ||||||
|  |  | ||||||
|         // Получение всех свойств класса |  | ||||||
|         try { |  | ||||||
|             $reflection = new ReflectionClass($className); |  | ||||||
|         } |  | ||||||
|         catch (ReflectionException) { |  | ||||||
|             return; |  | ||||||
|         } |  | ||||||
|  |  | ||||||
|         // Получение всех свойств класса |  | ||||||
|         $props = $reflection->getProperties(); |  | ||||||
|  |  | ||||||
|         // Создаю экземпляр класса |  | ||||||
|         $instance = new $className(); |  | ||||||
|  |  | ||||||
|         // Для каждого свойства |  | ||||||
|         foreach ($props as $prop) { |  | ||||||
|             // - получаю имя свойства |  | ||||||
|             $propName = $prop->getName(); |  | ||||||
|             // - получаю значение свойства |  | ||||||
|             $value = $from->$propName; |  | ||||||
|  |  | ||||||
|             try { |  | ||||||
|                 // - получаю тип свойства |  | ||||||
|                 $typeOf = gettype($from->$propName); |  | ||||||
|  |  | ||||||
|                 // - получаю значение свойства по типу и по умолчанию |  | ||||||
|                 $writeValue = self::GetDefaults($typeOf); |  | ||||||
|  |  | ||||||
|                 try { |  | ||||||
|                     // - проверяем, можно ли записать и прочитать значение |  | ||||||
|                     // -- пытаюсь установить значение |  | ||||||
|                     $instance->$propName = $writeValue; |  | ||||||
|                     // -- пытаюсь прочитать установленное значение |  | ||||||
|                     $readValue = $instance->$propName; |  | ||||||
|  |  | ||||||
|                     // -- и проверяю, что значение совпадают |  | ||||||
|                     /** @noinspection PhpConditionAlreadyCheckedInspection */ |  | ||||||
|                     if ($readValue !== $writeValue) |  | ||||||
|                         continue; |  | ||||||
|                 } |  | ||||||
|                 catch (Throwable) { |  | ||||||
|                     // - в случае ошибки, понимаем, что свойство доступно или только для чтения, или |  | ||||||
|                     // только для записи и оно нам не подходит. Поэтому пропускаем его. |  | ||||||
|                     continue; |  | ||||||
|                 } |  | ||||||
|  |  | ||||||
|                 // - если свойство игнорируется |  | ||||||
|                 if (in_array($propName, $options['ignored'])) |  | ||||||
|                     // -- пропускаю |  | ||||||
|                     continue; |  | ||||||
|  |  | ||||||
|                 // - если свойство не разрешено |  | ||||||
|                 if (count($options['allowed']) > 0 && !in_array($propName, $options['allowed'])) |  | ||||||
|                     // -- пропускаю |  | ||||||
|                     continue; |  | ||||||
|  |  | ||||||
|                 // Если не было ошибки, значит свойство имеет и геттер, и сеттер |  | ||||||
|                 $properties[$propName] = $value; |  | ||||||
|             } |  | ||||||
|             catch (Error) { |  | ||||||
|                 // - в случае ошибки, понимаем, что свойство нам не подходит. Поэтому пропускаю его. |  | ||||||
|                 continue; |  | ||||||
|             } |  | ||||||
|         } |  | ||||||
|  |  | ||||||
|         // Для каждого элемента массива |         // Для каждого элемента массива | ||||||
|         foreach ($properties as $name => $value) { |         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)) |             if (property_exists($to, $name)) | ||||||
|                 // -- то присваиваю значение |                 // -- то присваиваю значение | ||||||
| @@ -131,26 +71,6 @@ final class ClassMapper | |||||||
|         } |         } | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     /** |  | ||||||
|      * Возвращает значение по умолчанию для типа $typeName. |  | ||||||
|      * |  | ||||||
|      * @param string $typeName Тип |  | ||||||
|      * |  | ||||||
|      * @return mixed Значение по умолчанию |  | ||||||
|      */ |  | ||||||
|     public static function GetDefaults (string $typeName): mixed |  | ||||||
|     { |  | ||||||
|         return match (strtolower($typeName)) { |  | ||||||
|             'int', 'integer' => 0, |  | ||||||
|             'float', 'double' => 0.0, |  | ||||||
|             'bool', 'boolean' => false, |  | ||||||
|             'string' => '', |  | ||||||
|             'array' => [], |  | ||||||
|             'object' => new stdClass(), |  | ||||||
|             default => null, |  | ||||||
|         }; |  | ||||||
|     } |  | ||||||
|  |  | ||||||
|     /** |     /** | ||||||
|      * Подготавливает значения свойств класса. |      * Подготавливает значения свойств класса. | ||||||
|      * |      * | ||||||
| @@ -160,8 +80,7 @@ final class ClassMapper | |||||||
|      * |      * | ||||||
|      * @return array Массив данных класса. |      * @return array Массив данных класса. | ||||||
|      */ |      */ | ||||||
|     public |     public static function PrepareClassProperties (array $params, ReflectionClass $classReflector, | ||||||
|     static function PrepareClassProperties (array $params, ReflectionClass $classReflector, |  | ||||||
|         array $options = self::DefaultOptions): array |         array $options = self::DefaultOptions): array | ||||||
|     { |     { | ||||||
|         // Создаю массив данных класса |         // Создаю массив данных класса | ||||||
| @@ -221,8 +140,7 @@ final class ClassMapper | |||||||
|      * |      * | ||||||
|      * @return void |      * @return void | ||||||
|      */ |      */ | ||||||
|     public |     public static function GetClassParametersArrayParser (array &$source, array $parametersKeys, mixed $value, | ||||||
|     static function GetClassParametersArrayParser (array &$source, array $parametersKeys, mixed $value, |  | ||||||
|         array $options = self::DefaultOptions): void |         array $options = self::DefaultOptions): void | ||||||
|     { |     { | ||||||
|         // Если массив имен свойств пустой |         // Если массив имен свойств пустой | ||||||
| @@ -271,8 +189,7 @@ final class ClassMapper | |||||||
|      * @return mixed Объект класса |      * @return mixed Объект класса | ||||||
|      * @throws Exception |      * @throws Exception | ||||||
|      */ |      */ | ||||||
|     public |     public static function MapToClassProperty (string $className, array $properties): mixed | ||||||
|     static function MapToClassProperty (string $className, array $properties): mixed |  | ||||||
|     { |     { | ||||||
|         // Создаю |         // Создаю | ||||||
|         try { |         try { | ||||||
| @@ -368,8 +285,7 @@ final class ClassMapper | |||||||
|      * |      * | ||||||
|      * @throws Exception |      * @throws Exception | ||||||
|      */ |      */ | ||||||
|     public |     public static function SetParameterToClass (ReflectionClass $classReflector, string $propertyName, | ||||||
|     static function SetParameterToClass (ReflectionClass $classReflector, string $propertyName, |  | ||||||
|         mixed $classObj, mixed $value): void |         mixed $classObj, mixed $value): void | ||||||
|     { |     { | ||||||
|         try { |         try { | ||||||
| @@ -396,4 +312,24 @@ final class ClassMapper | |||||||
|             throw new Exception($exception->getMessage()); |             throw new Exception($exception->getMessage()); | ||||||
|         } |         } | ||||||
|     } |     } | ||||||
|  |  | ||||||
|  |     /** | ||||||
|  |      * Возвращает значение по умолчанию для типа $typeName. | ||||||
|  |      * | ||||||
|  |      * @param string $typeName Тип | ||||||
|  |      * | ||||||
|  |      * @return mixed Значение по умолчанию | ||||||
|  |      */ | ||||||
|  |     public static function GetDefaults (string $typeName): mixed | ||||||
|  |     { | ||||||
|  |         return match (strtolower($typeName)) { | ||||||
|  |             'int', 'integer' => 0, | ||||||
|  |             'float', 'double' => 0.0, | ||||||
|  |             'bool', 'boolean' => false, | ||||||
|  |             'string' => '', | ||||||
|  |             'array' => [], | ||||||
|  |             'object' => new stdClass(), | ||||||
|  |             default => null, | ||||||
|  |         }; | ||||||
|  |     } | ||||||
| } | } | ||||||
		Reference in New Issue
	
	Block a user