Skip to content

Commit

Permalink
Added InputFile as manual file
Browse files Browse the repository at this point in the history
  • Loading branch information
TiiFuchs committed May 4, 2022
1 parent 9fcf8bf commit a43267d
Show file tree
Hide file tree
Showing 10 changed files with 110 additions and 25 deletions.
37 changes: 33 additions & 4 deletions src/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,11 @@ public function raw(string $method, $data = []): mixed
$data = array_combine($parameters, $data);
$data = array_filter($data, fn($item) => ! is_null($item));

$response = $this->client->post($method, [
'form_params' => $data,
'proxy' => $this->proxy,
]);
$sendsFiles = array_reduce($data, fn($carry, $item) => $carry || $item instanceof InputFile, false);

$response = $sendsFiles
? $this->sendViaMultipart($method, $data)
: $this->sendViaForm($method, $data);

$jsonResponse = json_decode($response->getBody()->getContents(), true);
if ($jsonResponse['ok'] === true) {
Expand All @@ -58,4 +59,32 @@ public function raw(string $method, $data = []): mixed
throw new \Exception($jsonResponse['description']);
}

protected function sendViaMultipart(string $method, array $data)
{
$multiparts = [];

foreach ($data as $key => $value) {

$multiparts[] = [
'name' => $key,
'contents' => $value instanceof InputFile
? $value->getContents() : json_encode($value)
];

}

return $this->client->post($method, [
'multipart' => $multiparts,
'proxy' => $this->proxy
]);
}

protected function sendViaForm(string $method, array $data): \Psr\Http\Message\ResponseInterface
{
return $this->client->post($method, [
'form_params' => $data,
'proxy' => $this->proxy,
]);
}

}
8 changes: 8 additions & 0 deletions src/Exceptions/InputFileException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace Tii\Telepath\Exceptions;

class InputFileException extends \Exception
{

}
60 changes: 60 additions & 0 deletions src/InputFile.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

namespace Tii\Telepath;

use GuzzleHttp\Psr7\Stream;
use Psr\Http\Message\StreamInterface;
use Tii\Telepath\Exceptions\InputFileException;

/**
* 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
{
public function __construct(
protected StreamInterface|string $contents
) {
}

public static function fromFileId(string $fileId): static
{
return new static($fileId);
}

public static function fromUrl(string $url): static
{
if (! str_starts_with($url, 'http')) {
throw new InputFileException('Invalid URL.');
}

return new static($url);
}

public static function fromFile(string $file): static
{
if (! file_exists($file)) {
throw new InputFileException('File does not exists.');
}

return static::fromResource(fopen($file, 'r'));
}

public static function fromResource($resource): static
{
if (get_resource_type($resource) !== 'stream') {
throw new InputFileException('Invalid resource type.');
}

return static::fromStream(new Stream($resource));
}

public static function fromStream(StreamInterface $stream): static
{
return new static($stream);
}

public function getContents(): StreamInterface|string
{
return $this->contents;
}
}
19 changes: 0 additions & 19 deletions src/Telegram/InputFile.php

This file was deleted.

2 changes: 2 additions & 0 deletions src/Telegram/InputMediaAnimation.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

namespace Tii\Telepath\Telegram;

use Tii\Telepath\InputFile;

/**
* Represents an animation file (GIF or H.264/MPEG-4 AVC video without sound) to be sent.
*/
Expand Down
2 changes: 2 additions & 0 deletions src/Telegram/InputMediaAudio.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

namespace Tii\Telepath\Telegram;

use Tii\Telepath\InputFile;

/**
* Represents an audio file to be treated as music to be sent.
*/
Expand Down
2 changes: 2 additions & 0 deletions src/Telegram/InputMediaDocument.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

namespace Tii\Telepath\Telegram;

use Tii\Telepath\InputFile;

/**
* Represents a general file to be sent.
*/
Expand Down
2 changes: 2 additions & 0 deletions src/Telegram/InputMediaVideo.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

namespace Tii\Telepath\Telegram;

use Tii\Telepath\InputFile;

/**
* Represents a video to be sent.
*/
Expand Down
1 change: 0 additions & 1 deletion src/TelegramBot.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
use Tii\Telepath\Telegram\ForceReply;
use Tii\Telepath\Telegram\InlineKeyboardMarkup;
use Tii\Telepath\Telegram\InlineQueryResult;
use Tii\Telepath\Telegram\InputFile;
use Tii\Telepath\Telegram\InputMedia;
use Tii\Telepath\Telegram\InputMediaAudio;
use Tii\Telepath\Telegram\InputMediaDocument;
Expand Down
2 changes: 1 addition & 1 deletion src/Type.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class Type
{
use CastsToTelegramTypes;

public function __construct(array $data)
public function __construct(array $data = [])
{
foreach ($data as $key => $value) {

Expand Down

0 comments on commit a43267d

Please sign in to comment.