20250223
[О] [ClassMapper::MapClass]: Отменена проверка свойства на доступность get и set, так как выдавала ошибку. Используйте лучше в таких случаях $options['ignored'] для таких свойств.
This commit is contained in:
parent
ebfd42a88e
commit
e09ea26a3c
@ -4,12 +4,10 @@ namespace goodboyalex\php_components_pack\classes;
|
||||
|
||||
use DateTimeImmutable;
|
||||
use DateTimeInterface;
|
||||
use Error;
|
||||
use Exception;
|
||||
use ReflectionClass;
|
||||
use ReflectionException;
|
||||
use stdClass;
|
||||
use Throwable;
|
||||
use UnitEnum;
|
||||
|
||||
/**
|
||||
@ -51,79 +49,21 @@ final class ClassMapper
|
||||
// ---- то исключаю его из массива разрешенных
|
||||
unset($options['allowed'][array_search($ignoredProperty, $options['allowed'])]);
|
||||
|
||||
// Задаю массив свойств
|
||||
$properties = [];
|
||||
|
||||
// Получаю имя исходного класса
|
||||
$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;
|
||||
}
|
||||
}
|
||||
// Получаю массив свойств
|
||||
$properties = get_class_vars(get_class($from));
|
||||
|
||||
// Для каждого элемента массива
|
||||
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))
|
||||
// -- то присваиваю значение
|
||||
@ -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 Массив данных класса.
|
||||
*/
|
||||
public
|
||||
static function PrepareClassProperties (array $params, ReflectionClass $classReflector,
|
||||
public static function PrepareClassProperties (array $params, ReflectionClass $classReflector,
|
||||
array $options = self::DefaultOptions): array
|
||||
{
|
||||
// Создаю массив данных класса
|
||||
@ -221,8 +140,7 @@ 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
|
||||
{
|
||||
// Если массив имен свойств пустой
|
||||
@ -271,8 +189,7 @@ 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 {
|
||||
@ -368,8 +285,7 @@ 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 {
|
||||
@ -396,4 +312,24 @@ final class ClassMapper
|
||||
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,
|
||||
};
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user