Skip to content

Commit

Permalink
Get rid of identical Make child classes
Browse files Browse the repository at this point in the history
  • Loading branch information
CrazyTapok-bit committed Mar 31, 2024
1 parent 913ab5b commit b3a27f4
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 55 deletions.
4 changes: 2 additions & 2 deletions src/Entities/InitData.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
4 changes: 2 additions & 2 deletions src/Entities/LoginWidget.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
28 changes: 0 additions & 28 deletions src/Make/InitData.php

This file was deleted.

22 changes: 0 additions & 22 deletions src/Make/LoginWidget.php

This file was deleted.

24 changes: 23 additions & 1 deletion src/Make/Make.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

namespace TgWebValid\Make;

use Carbon\Carbon;
use ReflectionNamedType;
use ReflectionProperty;
use TgWebValid\Support\Arrayable;

abstract class Make extends Arrayable
Expand All @@ -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;
}

/**
Expand Down

0 comments on commit b3a27f4

Please sign in to comment.