Skip to content

Commit

Permalink
Merge pull request #530 from nutgram/fix-inputsticker_refactor
Browse files Browse the repository at this point in the history
wip
  • Loading branch information
sergix44 committed Jul 26, 2023
2 parents 54bb3f8 + 0cab48e commit 76e72c4
Show file tree
Hide file tree
Showing 23 changed files with 142 additions and 126 deletions.
91 changes: 33 additions & 58 deletions src/Telegram/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@
use SergiX44\Nutgram\Telegram\Endpoints\UpdateMethods;
use SergiX44\Nutgram\Telegram\Endpoints\UpdatesMessages;
use SergiX44\Nutgram\Telegram\Exceptions\TelegramException;
use SergiX44\Nutgram\Telegram\Types\Input\InputMedia;
use SergiX44\Nutgram\Telegram\Types\Input\InputSticker;
use SergiX44\Nutgram\Telegram\Types\Internal\InputFile;
use SergiX44\Nutgram\Telegram\Types\Internal\Uploadable;
use SergiX44\Nutgram\Telegram\Types\Internal\UploadableArray;
use SergiX44\Nutgram\Telegram\Types\Media\File;
use SergiX44\Nutgram\Telegram\Types\Message\Message;
use stdClass;
Expand Down Expand Up @@ -169,7 +169,37 @@ protected function requestMultipart(
string $mapTo = stdClass::class,
array $options = []
): mixed {
$parameters = $this->parseMultipartData(array_filter($multipart));
$parameters = [];
foreach (array_filter($multipart) as $name => $contents) {
if ($contents instanceof UploadableArray || $contents instanceof Uploadable) {
$files = $contents instanceof UploadableArray ? $contents->files : [$contents];
foreach ($files as $file) {
if ($file->isLocal()) {
$parameters[] = [
'name' => $file->getFilename(),
'contents' => $file->getResource(),
'filename' => $file->getFilename(),
];
}
}
}

$parameters[] = match (true) {
$contents instanceof InputFile => [
'name' => $name,
'contents' => $contents->getResource(),
'filename' => $contents->getFilename(),
],
$contents instanceof JsonSerializable, is_array($contents) => [
'name' => $name,
'contents' => json_encode($contents, JSON_THROW_ON_ERROR),
],
default => [
'name' => $name,
'contents' => $contents instanceof BackedEnum ? $contents->value : $contents,
]
};
}

$request = ['multipart' => $parameters, ...$options];

Expand Down Expand Up @@ -321,59 +351,4 @@ protected function redactTokenFromConnectException(ConnectException $e): void
$e->getHandlerContext(),
);
}

protected function parseMultipartData(array $items): array
{
$parameters = [];
foreach ($items as $name => $contents) {
$parameters = [...$parameters, ...$this->getInputToArray($contents)];

if (is_array($contents)) {
foreach ($contents as $item) {
$parameters = [...$parameters, ...$this->getInputToArray($item)];
}
}

$parameters[] = match (true) {
$contents instanceof InputFile => [
'name' => $name,
'contents' => $contents->getResource(),
'filename' => $contents->getFilename(),
],
is_array($contents), $contents instanceof JsonSerializable => [
'name' => $name,
'contents' => json_encode($contents, JSON_THROW_ON_ERROR),
],
$contents instanceof BackedEnum => [
'name' => $name,
'contents' => $contents->value,
],
default => [
'name' => $name,
'contents' => $contents,
]
};
}
return $parameters;
}

protected function getInputToArray(mixed $input): array
{
$parameters = [];
if ($input instanceof InputMedia && $input->media instanceof InputFile) {
$parameters[] = [
'name' => $input->media->getFilename(),
'contents' => $input->media->getResource(),
'filename' => $input->media->getFilename(),
];
}
if ($input instanceof InputSticker && $input->sticker instanceof InputFile) {
$parameters[] = [
'name' => $input->sticker->getFilename(),
'contents' => $input->sticker->getResource(),
'filename' => $input->sticker->getFilename(),
];
}
return $parameters;
}
}
3 changes: 2 additions & 1 deletion src/Telegram/Endpoints/Stickers.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use SergiX44\Nutgram\Telegram\Properties\StickerFormat;
use SergiX44\Nutgram\Telegram\Types\Input\InputSticker;
use SergiX44\Nutgram\Telegram\Types\Internal\InputFile;
use SergiX44\Nutgram\Telegram\Types\Internal\UploadableArray;
use SergiX44\Nutgram\Telegram\Types\Keyboard\ForceReply;
use SergiX44\Nutgram\Telegram\Types\Keyboard\InlineKeyboardMarkup;
use SergiX44\Nutgram\Telegram\Types\Keyboard\ReplyKeyboardMarkup;
Expand Down Expand Up @@ -135,11 +136,11 @@ public function createNewStickerSet(
'user_id',
'name',
'title',
'stickers',
'sticker_format',
'sticker_type',
'needs_repainting',
);
$parameters['stickers'] = new UploadableArray($stickers);

return $this->requestMultipart(__FUNCTION__, $parameters, options: $clientOpt);
}
Expand Down
26 changes: 25 additions & 1 deletion src/Telegram/Types/Input/InputMedia.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

use SergiX44\Nutgram\Telegram\Properties\InputMediaType;
use SergiX44\Nutgram\Telegram\Types\BaseType;
use SergiX44\Nutgram\Telegram\Types\Internal\InputFile;
use SergiX44\Nutgram\Telegram\Types\Internal\Uploadable;

/**
* This object represents the content of a media message to be sent. It should be one of
Expand All @@ -14,10 +16,32 @@
* - {@see InputMediaPhoto InputMediaPhoto}
* - {@see InputMediaVideo InputMediaVideo}
*/
abstract class InputMedia extends BaseType
abstract class InputMedia extends BaseType implements Uploadable
{
/**
* Type of the result
*/
public InputMediaType $type;

/**
* File to send.
* Pass a file_id to send a file that exists on the Telegram servers (recommended), pass an HTTP URL for Telegram to get a file from the Internet, or pass “attach://<file_attach_name>” to upload a new one using multipart/form-data under <file_attach_name> name.
* {@see https://core.telegram.org/bots/api#sending-files More information on Sending Files »}
*/
public InputFile|string $media;

public function isLocal(): bool
{
return $this->media instanceof InputFile;
}

public function getResource()
{
return $this->media->getResource();
}

public function getFilename(): string
{
return $this->media->getFilename();
}
}
7 changes: 0 additions & 7 deletions src/Telegram/Types/Input/InputMediaAnimation.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,6 @@ class InputMediaAnimation extends InputMedia implements JsonSerializable
/** Type of the result, must be animation */
public InputMediaType $type = InputMediaType::ANIMATION;

/**
* File to send.
* Pass a file_id to send a file that exists on the Telegram servers (recommended), pass an HTTP URL for Telegram to get a file from the Internet, or pass “attach://<file_attach_name>” to upload a new one using multipart/form-data under <file_attach_name> name.
* {@see https://core.telegram.org/bots/api#sending-files More information on Sending Files »}
*/
public InputFile|string $media;

/**
* Optional.
* Thumbnail of the file sent;
Expand Down
7 changes: 0 additions & 7 deletions src/Telegram/Types/Input/InputMediaAudio.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,6 @@ class InputMediaAudio extends InputMedia implements JsonSerializable
/** Type of the result, must be audio */
public InputMediaType $type = InputMediaType::AUDIO;

/**
* File to send.
* Pass a file_id to send a file that exists on the Telegram servers (recommended), pass an HTTP URL for Telegram to get a file from the Internet, or pass “attach://<file_attach_name>” to upload a new one using multipart/form-data under <file_attach_name> name.
* {@see https://core.telegram.org/bots/api#sending-files More information on Sending Files »}
*/
public InputFile|string $media;

/**
* Optional.
* Thumbnail of the file sent;
Expand Down
7 changes: 0 additions & 7 deletions src/Telegram/Types/Input/InputMediaDocument.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,6 @@ class InputMediaDocument extends InputMedia implements JsonSerializable
/** Type of the result, must be document */
public InputMediaType $type = InputMediaType::DOCUMENT;

/**
* File to send.
* Pass a file_id to send a file that exists on the Telegram servers (recommended), pass an HTTP URL for Telegram to get a file from the Internet, or pass “attach://<file_attach_name>” to upload a new one using multipart/form-data under <file_attach_name> name.
* {@see https://core.telegram.org/bots/api#sending-files More information on Sending Files »}
*/
public InputFile|string $media;

/**
* Optional.
* Thumbnail of the file sent;
Expand Down
7 changes: 0 additions & 7 deletions src/Telegram/Types/Input/InputMediaPhoto.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,6 @@ class InputMediaPhoto extends InputMedia implements JsonSerializable
/** Type of the result, must be photo */
public InputMediaType $type = InputMediaType::PHOTO;

/**
* File to send.
* Pass a file_id to send a file that exists on the Telegram servers (recommended), pass an HTTP URL for Telegram to get a file from the Internet, or pass “attach://<file_attach_name>” to upload a new one using multipart/form-data under <file_attach_name> name.
* {@see https://core.telegram.org/bots/api#sending-files More information on Sending Files »}
*/
public InputFile|string $media;

/**
* Optional.
* Caption of the photo to be sent, 0-1024 characters after entities parsing
Expand Down
7 changes: 0 additions & 7 deletions src/Telegram/Types/Input/InputMediaVideo.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,6 @@ class InputMediaVideo extends InputMedia implements JsonSerializable
/** Type of the result, must be video */
public InputMediaType $type = InputMediaType::VIDEO;

/**
* File to send.
* Pass a file_id to send a file that exists on the Telegram servers (recommended), pass an HTTP URL for Telegram to get a file from the Internet, or pass “attach://<file_attach_name>” to upload a new one using multipart/form-data under <file_attach_name> name.
* {@see https://core.telegram.org/bots/api#sending-files More information on Sending Files »}
*/
public InputFile|string $media;

/**
* Optional.
* Thumbnail of the file sent;
Expand Down
18 changes: 17 additions & 1 deletion src/Telegram/Types/Input/InputSticker.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@
use JsonSerializable;
use SergiX44\Nutgram\Telegram\Types\BaseType;
use SergiX44\Nutgram\Telegram\Types\Internal\InputFile;
use SergiX44\Nutgram\Telegram\Types\Internal\Uploadable;
use SergiX44\Nutgram\Telegram\Types\Sticker\MaskPosition;
use function SergiX44\Nutgram\Support\array_filter_null;

/**
* This object describes a sticker to be added to a sticker set.
* @see https://core.telegram.org/bots/api#inputsticker
*/
class InputSticker extends BaseType implements JsonSerializable
class InputSticker extends BaseType implements JsonSerializable, Uploadable
{
/**
* The added sticker.
Expand Down Expand Up @@ -79,4 +80,19 @@ public function jsonSerialize(): array
'keywords' => $this->keywords,
]);
}

public function isLocal(): bool
{
return $this->sticker instanceof InputFile;
}

public function getFilename(): string
{
return $this->sticker->getFilename();
}

public function getResource()
{
return $this->sticker->getResource();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
/**
* @mixin BaseType
*/
trait HasDownload
trait Downloadable
{
public function download(string $path, array $clientOpt = []): ?bool
{
Expand Down
5 changes: 3 additions & 2 deletions src/Telegram/Types/Internal/InputFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@
namespace SergiX44\Nutgram\Telegram\Types\Internal;

use InvalidArgumentException;
use JsonSerializable;

/**
* This object represents the contents of a file to be uploaded. Must be posted using
* multipart/form-data in the usual way that files are uploaded via the browser.
*/
class InputFile implements \JsonSerializable
class InputFile implements JsonSerializable
{
/**
* @var resource
Expand Down Expand Up @@ -62,7 +63,7 @@ public function filename(?string $filename): InputFile
}

/**
* @return false|resource
* @return resource
*/
public function getResource()
{
Expand Down
16 changes: 16 additions & 0 deletions src/Telegram/Types/Internal/Uploadable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace SergiX44\Nutgram\Telegram\Types\Internal;

interface Uploadable
{
public function isLocal(): bool;


public function getFilename(): string;

/**
* @return resource
*/
public function getResource();
}
19 changes: 19 additions & 0 deletions src/Telegram/Types/Internal/UploadableArray.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace SergiX44\Nutgram\Telegram\Types\Internal;

class UploadableArray implements \JsonSerializable
{
/**
* @param Uploadable[] $files
*/
public function __construct(public readonly array $files)
{
}


public function jsonSerialize(): mixed
{
return $this->files;
}
}
4 changes: 2 additions & 2 deletions src/Telegram/Types/Media/Animation.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@
namespace SergiX44\Nutgram\Telegram\Types\Media;

use SergiX44\Nutgram\Telegram\Types\BaseType;
use SergiX44\Nutgram\Telegram\Types\Internal\HasDownload;
use SergiX44\Nutgram\Telegram\Types\Internal\Downloadable;

/**
* This object represents an animation file (GIF or H.264/MPEG-4 AVC video without sound).
* @see https://core.telegram.org/bots/api#animation
*/
class Animation extends BaseType
{
use HasDownload;
use Downloadable;

/** Identifier for this file, which can be used to download or reuse the file */
public string $file_id;
Expand Down
4 changes: 2 additions & 2 deletions src/Telegram/Types/Media/Audio.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@
namespace SergiX44\Nutgram\Telegram\Types\Media;

use SergiX44\Nutgram\Telegram\Types\BaseType;
use SergiX44\Nutgram\Telegram\Types\Internal\HasDownload;
use SergiX44\Nutgram\Telegram\Types\Internal\Downloadable;

/**
* This object represents an audio file to be treated as music by the Telegram clients.
* @see https://core.telegram.org/bots/api#audio
*/
class Audio extends BaseType
{
use HasDownload;
use Downloadable;

/** Identifier for this file, which can be used to download or reuse the file */
public string $file_id;
Expand Down
Loading

0 comments on commit 76e72c4

Please sign in to comment.