Skip to content

Commit a157f27

Browse files
committed
Improve getInfo/getType, add wrapper methods
1 parent 6a8fcda commit a157f27

22 files changed

+398
-58
lines changed

‎.php-cs-fixer.dist.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ public function getRules(): array
77
'void_return' => true,
88
'array_indentation' => true,
99
'ternary_to_null_coalescing' => true,
10-
'assign_null_coalescing_to_coalesce_equal' => true
10+
'assign_null_coalescing_to_coalesce_equal' => true,
1111
]);
1212
}
1313
};

‎README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -528,7 +528,7 @@ Want to add your own open-source project to this list? [Click here!](https://doc
528528
* <a href="https://docs.madelineproto.xyz/API_docs/methods/payments.getSavedInfo.html" name="payments.getSavedInfo">Get saved payment information: payments.getSavedInfo</a>
529529
* <a href="https://docs.madelineproto.xyz/API_docs/methods/messages.getScheduledHistory.html" name="messages.getScheduledHistory">Get scheduled messages: messages.getScheduledHistory</a>
530530
* <a href="https://docs.madelineproto.xyz/API_docs/methods/messages.getScheduledMessages.html" name="messages.getScheduledMessages">Get scheduled messages: messages.getScheduledMessages</a>
531-
* <a href="https://docs.madelineproto.xyz/PHP/danog/MadelineProto/API.html#secretchatstatus-int-chat-int-one-of-mtproto-secret_empty-mtproto-secret_requested-mtproto-secret_ready" name="secretChatStatus">Get secret chat status: secretChatStatus</a>
531+
* <a href="https://docs.madelineproto.xyz/PHP/danog/MadelineProto/API.html#secretchatstatus-int-chat-int-one-of-danog-madelineproto-api-secret_empty-danog-madelineproto-api-secret_requested-danog-madelineproto-api-secret_ready" name="secretChatStatus">Get secret chat status: secretChatStatus</a>
532532
* <a href="https://docs.madelineproto.xyz/PHP/danog/MadelineProto/API.html#getsecretchat-array-int-chat-array" name="getSecretChat">Get secret chat: getSecretChat</a>
533533
* <a href="https://docs.madelineproto.xyz/PHP/danog/MadelineProto/API.html#random-int-length-string" name="random">Get secure random string of specified length: random</a>
534534
* <a href="https://docs.madelineproto.xyz/API_docs/methods/account.getContentSettings.html" name="account.getContentSettings">Get sensitive content settings: account.getContentSettings</a>

‎src/EventHandler/Media.php

+31-17
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,23 @@
22

33
namespace danog\MadelineProto\EventHandler;
44

5+
use danog\MadelineProto\Ipc\IpcCapable;
6+
use danog\MadelineProto\MTProto;
7+
58
/**
69
* Represents a generic media.
710
*/
8-
abstract class Media
11+
abstract class Media extends IpcCapable
912
{
1013
/** Media filesize */
1114
public readonly int $size;
1215

1316
/** Media file name */
1417
public readonly string $fileName;
1518

19+
/** Media file extension */
20+
public readonly string $fileExt;
21+
1622
/** Media creation date */
1723
public readonly bool $creationDate;
1824

@@ -22,27 +28,35 @@ abstract class Media
2228
/** Time-to-live of media */
2329
public readonly ?int $ttl;
2430

25-
/** @var list<Media> Thumbnails */
31+
/** @var list<array> Thumbnails */
2632
public readonly array $thumbs;
2733

28-
/** @var list<Media> Video thumbnails */
34+
/** @var list<array> Video thumbnails */
2935
public readonly array $videoThumbs;
3036

3137
/** Whether the media should be hidden behind a spoiler */
3238
public readonly bool $spoiler;
3339

34-
/** If true, the current media has attached mask stickers. */
35-
public readonly bool $hasStickers;
36-
37-
/** Media ID */
38-
private readonly int $documentId;
39-
40-
/** Media access hash */
41-
private readonly int $documentAccessHash;
42-
43-
/** Media file reference */
44-
private readonly int $fileReference;
45-
46-
/** DC ID */
47-
private readonly int $dcId;
40+
/** Media location */
41+
private readonly array $location;
42+
43+
/** @internal */
44+
public function __construct(
45+
MTProto $API,
46+
array $rawMedia,
47+
) {
48+
parent::__construct($API);
49+
[
50+
'name' => $name,
51+
'ext' => $this->fileExt,
52+
'mime' => $this->mimeType,
53+
'size' => $this->size,
54+
'InputFileLocation' => $this->location
55+
] = $API->getDownloadInfo($rawMedia);
56+
$this->fileName = "$name.".$this->fileExt;
57+
58+
$this->creationDate = $rawMedia['date'];
59+
$this->ttl = $rawMedia['ttl_seconds'] ?? null;
60+
$this->spoiler = $rawMedia['spoiler'] ?? false;
61+
}
4862
}

‎src/EventHandler/Media/AbstractAudio.php

+12
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,17 @@
99
*/
1010
abstract class AbstractAudio extends Media
1111
{
12+
/** Audio duration in seconds */
1213
public readonly int $duration;
14+
15+
/** @internal */
16+
public function __construct(
17+
MTProto $API,
18+
array $rawMedia,
19+
array $audioAttribute
20+
) {
21+
{
22+
parent::__construct($API, $rawMedia);
23+
$this->duration = $audioAttribute['duration'];
24+
}
1325
}

‎src/EventHandler/Media/AbstractSticker.php

+12
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,16 @@ abstract class AbstractSticker extends Media
1414

1515
/** Associated stickerset */
1616
public readonly array $stickerset;
17+
18+
/** @internal */
19+
public function __construct(
20+
MTProto $API,
21+
array $rawMedia,
22+
array $stickerAttribute
23+
) {
24+
{
25+
parent::__construct($API, $rawMedia);
26+
$this->emoji = $stickerAttribute['alt'];
27+
$this->stickerset = $stickerAttribute['stickerset'];
28+
}
1729
}

‎src/EventHandler/Media/AbstractVideo.php

+14
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,18 @@ abstract class AbstractVideo extends Media
1717
public readonly int $width;
1818
/** Video height */
1919
public readonly int $height;
20+
21+
/** @internal */
22+
public function __construct(
23+
MTProto $API,
24+
array $rawMedia,
25+
array $attribute
26+
) {
27+
{
28+
parent::__construct($API, $rawMedia);
29+
$this->duration = $attribute['duration'];
30+
$this->supportsStreaming = $attribute['supports_streaming'];
31+
$this->width = $attribute['w'];
32+
$this->height = $attribute['h'];
33+
}
2034
}

‎src/EventHandler/Media/Audio.php

+12-6
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,16 @@ final class Audio extends AbstractAudio
1111
public readonly ?string $title;
1212
/** Performer */
1313
public readonly ?string $performer;
14-
/**
15-
* 100 values from 0 to 31, representing a waveform.
16-
*
17-
* @var list<int>|null
18-
*/
19-
public readonly ?array $waveform;
14+
15+
/** @internal */
16+
public function __construct(
17+
MTProto $API,
18+
array $rawMedia,
19+
array $audioAttribute
20+
) {
21+
{
22+
parent::__construct($API, $rawMedia);
23+
$this->title = $audioAttribute['title'] ?? null;
24+
$this->performer = $audioAttribute['performer'] ?? null;
25+
}
2026
}

‎src/EventHandler/Media/CustomEmoji.php

+12
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,16 @@ final class CustomEmoji extends AbstractSticker
1111
public readonly bool $free;
1212
/** Whether the color of this TGS custom emoji should be changed to the text color when used in messages, the accent color if used as emoji status, white on chat photos, or another appropriate color based on context. */
1313
public readonly bool $textColor;
14+
15+
/** @internal */
16+
public function __construct(
17+
MTProto $API,
18+
array $rawMedia,
19+
array $stickerAttribute
20+
) {
21+
{
22+
parent::__construct($API, $rawMedia, $stickerAttribute);
23+
$this->free = $stickerAttribute['free'];
24+
$this->textColor = $stickerAttribute['text_color'];
25+
}
1426
}

‎src/EventHandler/Media/Document.php

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace danog\MadelineProto\EventHandler\Media;
4+
5+
use danog\MadelineProto\EventHandler\Media;
6+
7+
/**
8+
* Represents a document.
9+
*/
10+
final class Document extends Media
11+
{
12+
/** @internal */
13+
public function __construct(
14+
MTProto $API,
15+
array $rawMedia
16+
) {
17+
{
18+
parent::__construct($API, $rawMedia);
19+
}
20+
}
+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace danog\MadelineProto\EventHandler\Media;
4+
5+
use danog\MadelineProto\EventHandler\Media;
6+
7+
/**
8+
* Represents a photo uploaded as a document.
9+
*/
10+
final class DocumentPhoto extends Media
11+
{
12+
/** If true; the current media has attached mask stickers. */
13+
public readonly bool $hasStickers;
14+
15+
public readonly int $width;
16+
public readonly int $height;
17+
18+
/** @internal */
19+
public function __construct(
20+
MTProto $API,
21+
array $rawMedia,
22+
array $attribute
23+
) {
24+
{
25+
parent::__construct($API, $rawMedia);
26+
$this->width = $attribute['w'];
27+
$this->height = $attribute['h'];
28+
$hasStickers = false;
29+
foreach ($rawMedia['attributes'] as ['_' => $t]) {
30+
if ($t === 'documentAttributeHasStickers') {
31+
$hasStickers = true;
32+
break;
33+
}
34+
}
35+
$this->hasStickers = $hasStickers;
36+
}
37+
}

‎src/EventHandler/Media/Gif.php

+20
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,24 @@
77
*/
88
final class Gif extends AbstractVideo
99
{
10+
/** If true; the current media has attached mask stickers. */
11+
public readonly bool $hasStickers;
12+
13+
/** @internal */
14+
public function __construct(
15+
MTProto $API,
16+
array $rawMedia,
17+
array $attribute
18+
) {
19+
{
20+
parent::__construct($API, $rawMedia, $attribute);
21+
$hasStickers = false;
22+
foreach ($rawMedia['attributes'] as ['_' => $t]) {
23+
if ($t === 'documentAttributeHasStickers') {
24+
$hasStickers = true;
25+
break;
26+
}
27+
}
28+
$this->hasStickers = $hasStickers;
29+
}
1030
}

‎src/EventHandler/Media/MaskSticker.php

+21-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ final class MaskSticker extends AbstractSticker
1010
/**
1111
* Part of the face, relative to which the mask should be placed.
1212
*/
13-
public readonly MaskPosition $n;
13+
public readonly MaskPosition $position;
1414
/**
1515
* Shift by X-axis measured in widths of the mask scaled to the face size, from left to right.
1616
*
@@ -29,4 +29,24 @@ final class MaskSticker extends AbstractSticker
2929
* For example, 2.0 means a doubled size.
3030
*/
3131
public readonly float $zoom;
32+
33+
/** @internal */
34+
public function __construct(
35+
MTProto $API,
36+
array $rawMedia,
37+
array $stickerAttribute
38+
) {
39+
{
40+
parent::__construct($API, $rawMedia, $stickerAttribute);
41+
$coords = $stickerAttribute['mask_coords'];
42+
$this->position = match ($coords['n']) {
43+
0 => MaskPosition::Forehead,
44+
1 => MaskPosition::Eyes,
45+
2 => MaskPosition::Mouth,
46+
3 => MaskPosition::Chin
47+
};
48+
$this->x = $coords['x'];
49+
$this->y = $coords['y'];
50+
$this->zoom = $coords['zoom'];
51+
}
3252
}

‎src/EventHandler/Media/Photo.php

+19
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,23 @@
99
*/
1010
final class Photo extends Media
1111
{
12+
/** If true; the current media has attached mask stickers. */
13+
public readonly bool $hasStickers;
14+
15+
/** @internal */
16+
public function __construct(
17+
MTProto $API,
18+
array $rawMedia
19+
) {
20+
{
21+
parent::__construct($API, $rawMedia);
22+
$hasStickers = false;
23+
foreach ($rawMedia['attributes'] as ['_' => $t]) {
24+
if ($t === 'documentAttributeHasStickers') {
25+
$hasStickers = true;
26+
break;
27+
}
28+
}
29+
$this->hasStickers = $hasStickers;
30+
}
1231
}

‎src/EventHandler/Media/Sticker.php

+13
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,17 @@
77
*/
88
final class Sticker extends AbstractSticker
99
{
10+
/** Whether this is a premium sticker and a premium sticker animation must be played. */
11+
public readonly bool $premiumSticker;
12+
13+
/** @internal */
14+
public function __construct(
15+
MTProto $API,
16+
array $rawMedia,
17+
array $stickerAttribute
18+
) {
19+
{
20+
parent::__construct($API, $rawMedia, $stickerAttribute);
21+
$this->premiumSticker = !$rawMedia['nopremium'];
22+
}
1023
}

‎src/EventHandler/Media/Video.php

+20
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,24 @@
77
*/
88
final class Video extends AbstractVideo
99
{
10+
/** If true; the current media has attached mask stickers. */
11+
public readonly bool $hasStickers;
12+
13+
/** @internal */
14+
public function __construct(
15+
MTProto $API,
16+
array $rawMedia,
17+
array $attribute
18+
) {
19+
{
20+
parent::__construct($API, $rawMedia, $attribute);
21+
$hasStickers = false;
22+
foreach ($rawMedia['attributes'] as ['_' => $t]) {
23+
if ($t === 'documentAttributeHasStickers') {
24+
$hasStickers = true;
25+
break;
26+
}
27+
}
28+
$this->hasStickers = $hasStickers;
29+
}
1030
}

0 commit comments

Comments
 (0)