20250221
This commit is contained in:
parent
64c1f386eb
commit
054e6a7cdc
@ -4,6 +4,7 @@ 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;
|
||||||
@ -49,21 +50,51 @@ final class ClassMapper
|
|||||||
// ---- то исключаю его из массива разрешенных
|
// ---- то исключаю его из массива разрешенных
|
||||||
unset($options['allowed'][array_search($ignoredProperty, $options['allowed'])]);
|
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) {
|
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))
|
||||||
// -- то присваиваю значение
|
// -- то присваиваю значение
|
||||||
@ -80,7 +111,8 @@ final class ClassMapper
|
|||||||
*
|
*
|
||||||
* @return array Массив данных класса.
|
* @return array Массив данных класса.
|
||||||
*/
|
*/
|
||||||
public static function PrepareClassProperties (array $params, ReflectionClass $classReflector,
|
public
|
||||||
|
static function PrepareClassProperties (array $params, ReflectionClass $classReflector,
|
||||||
array $options = self::DefaultOptions): array
|
array $options = self::DefaultOptions): array
|
||||||
{
|
{
|
||||||
// Создаю массив данных класса
|
// Создаю массив данных класса
|
||||||
@ -140,7 +172,8 @@ final class ClassMapper
|
|||||||
*
|
*
|
||||||
* @return void
|
* @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
|
array $options = self::DefaultOptions): void
|
||||||
{
|
{
|
||||||
// Если массив имен свойств пустой
|
// Если массив имен свойств пустой
|
||||||
@ -189,7 +222,8 @@ final class ClassMapper
|
|||||||
* @return mixed Объект класса
|
* @return mixed Объект класса
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public static function MapToClassProperty (string $className, array $properties): mixed
|
public
|
||||||
|
static function MapToClassProperty (string $className, array $properties): mixed
|
||||||
{
|
{
|
||||||
// Создаю
|
// Создаю
|
||||||
try {
|
try {
|
||||||
@ -285,7 +319,8 @@ final class ClassMapper
|
|||||||
*
|
*
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public static function SetParameterToClass (ReflectionClass $classReflector, string $propertyName,
|
public
|
||||||
|
static function SetParameterToClass (ReflectionClass $classReflector, string $propertyName,
|
||||||
mixed $classObj, mixed $value): void
|
mixed $classObj, mixed $value): void
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
@ -320,7 +355,8 @@ final class ClassMapper
|
|||||||
*
|
*
|
||||||
* @return mixed|null Результат.
|
* @return mixed|null Результат.
|
||||||
*/
|
*/
|
||||||
public static function GetDefaults (string $typeName): mixed
|
public
|
||||||
|
static function GetDefaults (string $typeName): mixed
|
||||||
{
|
{
|
||||||
return match ($typeName) {
|
return match ($typeName) {
|
||||||
'int' => 0,
|
'int' => 0,
|
||||||
|
@ -3,6 +3,7 @@
|
|||||||
namespace goodboyalex\php_components_pack\tests\classes;
|
namespace goodboyalex\php_components_pack\tests\classes;
|
||||||
|
|
||||||
use goodboyalex\php_components_pack\classes\ClassMapper;
|
use goodboyalex\php_components_pack\classes\ClassMapper;
|
||||||
|
use goodboyalex\php_components_pack\tests\data\A;
|
||||||
use PHPUnit\Framework\TestCase;
|
use PHPUnit\Framework\TestCase;
|
||||||
|
|
||||||
class ClassMapperTest extends TestCase
|
class ClassMapperTest extends TestCase
|
||||||
@ -11,7 +12,7 @@ class ClassMapperTest extends TestCase
|
|||||||
{
|
{
|
||||||
$this->PrepareForTest();
|
$this->PrepareForTest();
|
||||||
|
|
||||||
$a = new \goodboyalex\php_components_pack\tests\data\A();
|
$a = new A();
|
||||||
$a->a = 'a';
|
$a->a = 'a';
|
||||||
$a->b = 2;
|
$a->b = 2;
|
||||||
$a->c = true;
|
$a->c = true;
|
||||||
@ -19,6 +20,8 @@ class ClassMapperTest extends TestCase
|
|||||||
$b = new B();
|
$b = new B();
|
||||||
ClassMapper::MapClass($a, $b);
|
ClassMapper::MapClass($a, $b);
|
||||||
|
|
||||||
|
var_dump($b);
|
||||||
|
|
||||||
$this->assertEquals('a', $b->a);
|
$this->assertEquals('a', $b->a);
|
||||||
$this->assertEquals(2, $b->b);
|
$this->assertEquals(2, $b->b);
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user