2025-07-12 16:27:53 +03:00

56 lines
1.4 KiB
PHP

<?php
namespace goodboyalex\php_components_pack\tests\data;
use Closure;
use goodboyalex\php_components_pack\extensions\TypeExtension;
class D
{
public string $stringD;
public int $intD;
public bool $boolD;
public A $a;
public B $b;
public C $c;
public function __construct (string $string = "", int $int = 0, bool $bool = false, ?A $a = null, ?B $b = null,
?C $c = null)
{
$this->stringD = $string;
$this->intD = $int;
$this->boolD = $bool;
$this->a = $a ?? new A();
$this->b = $b ?? new B();
$this->c = $c ?? new C();
}
public static function CLOSURE_FROM_ARRAY (): Closure
{
return fn (array $classArray): object
=> $classArray['type_class'] == C::class
? self::CLOSURE_FROM_FOR_C($classArray["type_value"]) : TypeExtension::FromArray($classArray);
}
private static function CLOSURE_FROM_FOR_C (string $serializedC): object
{
$classC = new C();
$classC->UnSerialize($serializedC);
return $classC;
}
public static function CLOSURE_TO_ARRAY (): Closure
{
return fn (object $class): array => $class instanceof C ? self::CLOSURE_TO_FOR_C($class)
: TypeExtension::ToArray($class);
}
private static function CLOSURE_TO_FOR_C (C $class): array
{
return [
"type_class" => C::class,
"type_value" => $class->Serialize()
];
}
}