Skip to content

Commit

Permalink
Merge pull request #14 from CrazyTapok-bit/dev-master
Browse files Browse the repository at this point in the history
Single form of filling entities with data has been
  • Loading branch information
CrazyTapok-bit authored Mar 31, 2023
2 parents c4c9e60 + 0a5922d commit 9a08b76
Show file tree
Hide file tree
Showing 9 changed files with 51 additions and 41 deletions.
3 changes: 2 additions & 1 deletion src/Entities/InitData.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@
use TgWebValid\Entities\InitData\Chat;
use TgWebValid\Entities\InitData\Receiver;
use TgWebValid\Entities\InitData\User;
use TgWebValid\Entities\Make\InitDataMake;

class InitData extends Make
class InitData extends InitDataMake
{
/**
* Optional. A unique identifier for the Web App session, required for sending messages via the answerWebAppQuery method.
Expand Down
2 changes: 1 addition & 1 deletion src/Entities/InitData/Chat.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace TgWebValid\Entities\InitData;

use TgWebValid\Entities\Make;
use TgWebValid\Entities\Make\Make;

class Chat extends Make
{
Expand Down
2 changes: 1 addition & 1 deletion src/Entities/InitData/Receiver.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace TgWebValid\Entities\InitData;

use TgWebValid\Entities\Make;
use TgWebValid\Entities\Make\Make;

class Receiver extends Make
{
Expand Down
2 changes: 1 addition & 1 deletion src/Entities/InitData/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace TgWebValid\Entities\InitData;

use TgWebValid\Entities\Make;
use TgWebValid\Entities\Make\Make;

class User extends Make
{
Expand Down
4 changes: 3 additions & 1 deletion src/Entities/User.php → src/Entities/LoginWidget.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

namespace TgWebValid\Entities;

class User extends Make
use TgWebValid\Entities\Make\Make;

class LoginWidget extends Make
{
/**
* A unique identifier for the user or bot.
Expand Down
23 changes: 23 additions & 0 deletions src/Entities/Make/InitDataMake.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace TgWebValid\Entities\Make;

use TgWebValid\Entities\InitData\Chat;
use TgWebValid\Entities\InitData\Receiver;
use TgWebValid\Entities\InitData\User;

class InitDataMake extends Make
{
public function __construct(array $props)
{
foreach ($props as $prop => $value) {
$props[$prop] = match ($prop) {
'user' => new User(json_decode($value, true)),
'receiver' => new Receiver(json_decode($value, true)),
'chat' => new Chat(json_decode($value, true)),
default => $value
};
}
parent::__construct($props);
}
}
2 changes: 1 addition & 1 deletion src/Entities/Make.php → src/Entities/Make/Make.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace TgWebValid\Entities;
namespace TgWebValid\Entities\Make;

class Make
{
Expand Down
27 changes: 0 additions & 27 deletions src/Field.php

This file was deleted.

27 changes: 19 additions & 8 deletions src/TgWebValid.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
namespace TgWebValid;

use TgWebValid\Entities\InitData;
use TgWebValid\Entities\User;
use TgWebValid\Entities\LoginWidget;

class TgWebValid
{
public ?InitData $initData;

public ?User $user;
public ?LoginWidget $user;

public function __construct(
private string $token
Expand All @@ -21,12 +21,23 @@ public function isValid(string $initData)
{
$rawData = explode('&', rawurldecode($initData));

sort($rawData);

$this->initData = new InitData(array_merge(...array_map(
fn ($item) => (new Field(...explode('=', $item)))->toArray(),
$rawData = array_merge(...array_map(
function($item) {
[$prop, $value] = explode('=', $item);
return [$prop => $value];
},
$rawData
)));
));

$this->initData = new InitData($rawData);

$rawData = array_map(
fn($value, $key) => $key . '=' . $value,
$rawData,
array_keys($rawData)
);

sort($rawData);

$data = implode("\n", $this->ridHash($rawData));

Expand All @@ -38,7 +49,7 @@ public function isValid(string $initData)

public function isLoginValid(array $user)
{
$this->user = new User($user);
$this->user = new LoginWidget($user);

$rawData = array_map(
fn($value, $key) => $key . '=' . $value,
Expand Down

0 comments on commit 9a08b76

Please sign in to comment.