From b3a27f4fa29c7c5c09654d52601e2f85bb8215e6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A1=D0=B5=D1=80=D0=B3=D1=96=D0=B9?= <61122611+CrazyTapok-bit@users.noreply.github.com> Date: Sun, 31 Mar 2024 11:08:39 +0300 Subject: [PATCH] Get rid of identical Make child classes --- src/Entities/InitData.php | 4 ++-- src/Entities/LoginWidget.php | 4 ++-- src/Make/InitData.php | 28 ---------------------------- src/Make/LoginWidget.php | 22 ---------------------- src/Make/Make.php | 24 +++++++++++++++++++++++- 5 files changed, 27 insertions(+), 55 deletions(-) delete mode 100644 src/Make/InitData.php delete mode 100644 src/Make/LoginWidget.php diff --git a/src/Entities/InitData.php b/src/Entities/InitData.php index 6bc0111..f477f87 100644 --- a/src/Entities/InitData.php +++ b/src/Entities/InitData.php @@ -6,9 +6,9 @@ use TgWebValid\Entities\InitData\Chat; use TgWebValid\Entities\InitData\Receiver; use TgWebValid\Entities\InitData\User; -use TgWebValid\Make\InitData as MakeInitData; +use TgWebValid\Make\Make; -final class InitData extends MakeInitData +final class InitData extends Make { /** * Optional. A unique identifier for the Web App session, required for sending messages via the answerWebAppQuery method. diff --git a/src/Entities/LoginWidget.php b/src/Entities/LoginWidget.php index 2df0756..c652ce3 100644 --- a/src/Entities/LoginWidget.php +++ b/src/Entities/LoginWidget.php @@ -3,9 +3,9 @@ namespace TgWebValid\Entities; use Carbon\CarbonInterface; -use TgWebValid\Make\LoginWidget as MakeLoginWidget; +use TgWebValid\Make\Make; -final class LoginWidget extends MakeLoginWidget +final class LoginWidget extends Make { /** * A unique identifier for the user or bot. diff --git a/src/Make/InitData.php b/src/Make/InitData.php deleted file mode 100644 index daa29a6..0000000 --- a/src/Make/InitData.php +++ /dev/null @@ -1,28 +0,0 @@ - $props - */ - public function __construct(array $props = []) - { - foreach ($props as $prop => $value) { - $value = match ($prop) { - 'user' => new User($this->tryParseJSON($value)), - 'receiver' => new Receiver($this->tryParseJSON($value)), - 'chat' => new Chat($this->tryParseJSON($value)), - 'auth_date' => Carbon::createFromTimestamp((int) $value), - default => $value - }; - $this->setProperty(camelize($prop), $value); - } - } -} diff --git a/src/Make/LoginWidget.php b/src/Make/LoginWidget.php deleted file mode 100644 index edf90ac..0000000 --- a/src/Make/LoginWidget.php +++ /dev/null @@ -1,22 +0,0 @@ - $props - */ - public function __construct(array $props = []) - { - foreach ($props as $prop => $value) { - $value = match ($prop) { - 'auth_date' => Carbon::createFromTimestamp((int) $value), - default => $value - }; - $this->setProperty(camelize($prop), $value); - } - } -} diff --git a/src/Make/Make.php b/src/Make/Make.php index f7b4c39..a7fe59a 100644 --- a/src/Make/Make.php +++ b/src/Make/Make.php @@ -2,6 +2,9 @@ namespace TgWebValid\Make; +use Carbon\Carbon; +use ReflectionNamedType; +use ReflectionProperty; use TgWebValid\Support\Arrayable; abstract class Make extends Arrayable @@ -12,15 +15,34 @@ abstract class Make extends Arrayable public function __construct(array $props = []) { foreach ($props as $prop => $value) { + $value = match ($prop) { + 'auth_date' => Carbon::createFromTimestamp((int) $value), + default => $value + }; $this->setProperty(camelize($prop), $value); } } protected function setProperty(string $property, mixed $value): void { - if (property_exists(get_class($this), $property)) { + if(!property_exists(get_class($this), $property)) { + return; + } + + $reflection = new ReflectionProperty(get_class($this), $property); + + $type = $reflection->getType(); + + if(!($type instanceof ReflectionNamedType) || $type->isBuiltin()) { $this->$property = $value; + return; } + + $class = $type->getName(); + + $this->$property = is_subclass_of($class, self::class) + ? new $class($this->tryParseJSON($value)) + : $value; } /**