20250221
This commit is contained in:
parent
64c1f386eb
commit
054e6a7cdc
@ -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;
|
||||
|
||||
// Для каждого элемента массива
|
||||
foreach ($properties as $name => $value) {
|
||||
// - если свойство игнорируется
|
||||
if (in_array($name, $options['ignored']))
|
||||
if (in_array($propName, $options['ignored']))
|
||||
// -- пропускаю
|
||||
continue;
|
||||
|
||||
// - если свойство не разрешено
|
||||
if (count($options['allowed']) > 0 && !in_array($name, $options['allowed']))
|
||||
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 (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,
|
||||
|
@ -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);
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user