generated from spatie/package-skeleton-laravel
-
-
Notifications
You must be signed in to change notification settings - Fork 118
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #650 from andrey-helldar/patch/2024-09-29/23-38
Added entities processing
- Loading branch information
Showing
8 changed files
with
260 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace DefStudio\Telegraph\DTO; | ||
|
||
use Illuminate\Contracts\Support\Arrayable; | ||
|
||
/** | ||
* @implements Arrayable<string, string|int|array<string, mixed>> | ||
*/ | ||
class Entity implements Arrayable | ||
{ | ||
private string $type; | ||
|
||
private int $offset; | ||
|
||
private int $length; | ||
|
||
private ?string $url = null; | ||
|
||
private ?User $user = null; | ||
|
||
private ?string $language = null; | ||
|
||
private ?string $customEmojiId = null; | ||
|
||
private function __construct() | ||
{ | ||
} | ||
|
||
/** | ||
* @param array{ | ||
* type: string, | ||
* offset: int, | ||
* length: int, | ||
* url?: string, | ||
* user?: array<string, mixed>, | ||
* language?: string, | ||
* custom_emoji_id?: string | ||
* } $data | ||
* | ||
* @return \DefStudio\Telegraph\DTO\Entity | ||
*/ | ||
public static function fromArray(array $data): Entity | ||
{ | ||
$entity = new self(); | ||
|
||
$entity->type = $data['type']; | ||
$entity->offset = $data['offset']; | ||
$entity->length = $data['length']; | ||
|
||
if (isset($data['url'])) { | ||
$entity->url = $data['url']; | ||
} | ||
|
||
if (isset($data['user'])) { | ||
/* @phpstan-ignore-next-line */ | ||
$entity->user = User::fromArray($data['user']); | ||
} | ||
|
||
if (isset($data['language'])) { | ||
$entity->language = $data['language']; | ||
} | ||
|
||
if (isset($data['custom_emoji_id'])) { | ||
$entity->customEmojiId = $data['custom_emoji_id']; | ||
} | ||
|
||
return $entity; | ||
} | ||
|
||
public function type(): string | ||
{ | ||
return $this->type; | ||
} | ||
|
||
public function offset(): int | ||
{ | ||
return $this->offset; | ||
} | ||
|
||
public function length(): int | ||
{ | ||
return $this->length; | ||
} | ||
|
||
public function url(): ?string | ||
{ | ||
return $this->url; | ||
} | ||
|
||
public function user(): ?User | ||
{ | ||
return $this->user; | ||
} | ||
|
||
public function language(): ?string | ||
{ | ||
return $this->language; | ||
} | ||
|
||
public function customEmojiId(): ?string | ||
{ | ||
return $this->customEmojiId; | ||
} | ||
|
||
public function toArray(): array | ||
{ | ||
return array_filter([ | ||
'type' => $this->type, | ||
'offset' => $this->offset, | ||
'length' => $this->length, | ||
'url' => $this->url, | ||
'user' => $this->user()?->toArray(), | ||
'language' => $this->language, | ||
'custom_emoji_id' => $this->customEmojiId, | ||
], fn ($value) => $value !== null); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace DefStudio\Telegraph\Tests\Support; | ||
|
||
use DefStudio\Telegraph\Handlers\WebhookHandler; | ||
use Illuminate\Support\Stringable; | ||
|
||
class TestEntitiesWebhookHandler extends WebhookHandler | ||
{ | ||
protected function handleChatMessage(Stringable $text): void | ||
{ | ||
/** @var \DefStudio\Telegraph\DTO\Entity $entity */ | ||
$entity = $this->message->entities()->first(); | ||
|
||
$fromText = $text->substr($entity->offset(), $entity->length()); | ||
$fromEntity = $entity->url(); | ||
|
||
$this->chat->html(implode('. ', [ | ||
'URL from text: ' . $fromText, | ||
'URL from entity: ' . $fromEntity, | ||
]))->send(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
<?php | ||
|
||
/** @noinspection PhpUnhandledExceptionInspection */ | ||
|
||
use DefStudio\Telegraph\DTO\Entity; | ||
use DefStudio\Telegraph\DTO\Message; | ||
use Illuminate\Support\Str; | ||
|
||
it('export all properties to array', function () { | ||
$dto = Message::fromArray([ | ||
'message_id' => 2, | ||
'date' => now()->timestamp, | ||
'entities' => [ | ||
[ | ||
'type' => 'url', | ||
'offset' => 10, | ||
'length' => 19, | ||
'url' => 'https://example.com', | ||
'user' => [ | ||
'id' => 1, | ||
'is_bot' => true, | ||
'first_name' => 'a', | ||
'last_name' => 'b', | ||
'username' => 'c', | ||
'language_code' => 'd', | ||
'is_premium' => false, | ||
], | ||
'language' => 'en', | ||
'custom_emoji_id' => '12345', | ||
], | ||
], | ||
]); | ||
|
||
$array = $dto->entities()->first()->toArray(); | ||
|
||
$reflection = new ReflectionClass(Entity::class); | ||
foreach ($reflection->getProperties() as $property) { | ||
expect($array)->toHaveKey(Str::of($property->name)->snake()); | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters