Further public API improvements

This commit is contained in:
Daniil Gentili 2020-02-26 18:12:00 +01:00
parent fe8e795f8a
commit 5af7130b25
Signed by: danog
GPG Key ID: 8C1BE3B34B230CA7
10 changed files with 234 additions and 72 deletions

View File

@ -79,7 +79,6 @@ class MyEventHandler extends EventHandler
return; return;
} }
$res = \json_encode($update, JSON_PRETTY_PRINT); $res = \json_encode($update, JSON_PRETTY_PRINT);
try { try {
yield $this->messages->sendMessage(['peer' => $update, 'message' => "<code>$res</code>", 'reply_to_msg_id' => isset($update['message']['id']) ? $update['message']['id'] : null, 'parse_mode' => 'HTML']); yield $this->messages->sendMessage(['peer' => $update, 'message' => "<code>$res</code>", 'reply_to_msg_id' => isset($update['message']['id']) ? $update['message']['id'] : null, 'parse_mode' => 'HTML']);
if (isset($update['message']['media']) && $update['message']['media']['_'] !== 'messageMediaGame') { if (isset($update['message']['media']) && $update['message']['media']['_'] !== 'messageMediaGame') {

View File

@ -67,6 +67,22 @@ final class APIWrapper
* @var integer * @var integer
*/ */
private int $serialized = 0; private int $serialized = 0;
/**
* Whether lua is being used.
*
* @internal
*
* @var boolean
*/
private bool $lua = false;
/**
* Whether async is enabled.
*
* @internal
*
* @var boolean
*/
private bool $async = false;
/** /**
* AbstractAPIFactory instance. * AbstractAPIFactory instance.
@ -111,7 +127,7 @@ final class APIWrapper
*/ */
public static function __sleep(): array public static function __sleep(): array
{ {
return ['API', 'webApiTemplate', 'gettingApiId', 'myTelegramOrgWrapper', 'storage']; return ['API', 'webApiTemplate', 'gettingApiId', 'myTelegramOrgWrapper', 'storage', 'lua', 'async'];
} }
/** /**
@ -124,6 +140,16 @@ final class APIWrapper
return $this->API; return $this->API;
} }
/**
* Whether async is being used.
*
* @return boolean
*/
public function isAsync(): bool
{
return $this->async;
}
/** /**
* Get API factory. * Get API factory.
* *

View File

@ -54,7 +54,7 @@ abstract class AbstractAPIFactory extends AsyncConstruct
* *
* @var boolean * @var boolean
*/ */
private bool $async = false; protected bool $async = false;
/** /**
* Method list. * Method list.
* *

View File

@ -436,7 +436,11 @@ image: https://docs.madelineproto.xyz/favicons/android-chrome-256x256.png
## Type: bytes ## Type: bytes
[Back to constructor index](index.md) [Back to constructor index](index.md)
An object of type `\\danog\\MadelineProto\\TL\\Types\\Bytes`. ```php
$bytes = "simple string of bytes";
```
Internally, an object of type `\\danog\\MadelineProto\\TL\\Types\\Bytes`.
When casted to string, turns into a string of bytes of variable length, with length smaller than or equal to 16777215. When casted to string, turns into a string of bytes of variable length, with length smaller than or equal to 16777215.
When JSON-serialized, turns into an array of the following format: When JSON-serialized, turns into an array of the following format:
``` ```

View File

@ -4121,19 +4121,6 @@ class InternalDoc extends APIFactory
{ {
return $this->API->getDataCenterConnections(); return $this->API->getDataCenterConnections();
} }
/**
* Get correct settings array for the latest version.
*
* @param array $settings Current settings array
* @param array $previousSettings Previous settings array
*
* @return array
*/
public function getSettings(array $settings, array $previousSettings = [
]): array
{
return \danog\MadelineProto\MTProto::getSettings($settings, $previousSettings);
}
/** /**
* Parse, update and store settings. * Parse, update and store settings.
* *
@ -4146,6 +4133,15 @@ class InternalDoc extends APIFactory
{ {
return $this->__call(__FUNCTION__, [$settings, $reinit, $extra]); return $this->__call(__FUNCTION__, [$settings, $reinit, $extra]);
} }
/**
* Return current settings array.
*
* @return array
*/
public function getSettings(): array
{
return $this->API->getSettings();
}
/** /**
* Setup logger. * Setup logger.
* *

View File

@ -110,7 +110,7 @@ class Logger
{ {
if (!self::$default) { if (!self::$default) {
// The getLogger function will automatically init the static logger, but we'll do it again anyway // The getLogger function will automatically init the static logger, but we'll do it again anyway
self::$default = self::getLoggerFromSettings(MTProto::getSettings($settings)); self::$default = self::getLoggerFromSettings(MTProto::parseSettings($settings));
} }
} }
/** /**

View File

@ -976,9 +976,11 @@ class MTProto extends AsyncConstruct implements TLCallback
* @param array $settings Current settings array * @param array $settings Current settings array
* @param array $previousSettings Previous settings array * @param array $previousSettings Previous settings array
* *
* @internal
*
* @return array * @return array
*/ */
public static function getSettings(array $settings, array $previousSettings = []): array public static function parseSettings(array $settings, array $previousSettings = []): array
{ {
Magic::classExists(); Magic::classExists();
$settings = \array_replace_recursive($previousSettings, $settings); $settings = \array_replace_recursive($previousSettings, $settings);
@ -1296,7 +1298,7 @@ class MTProto extends AsyncConstruct implements TLCallback
*/ */
public function updateSettings(array $settings, bool $reinit = true): \Generator public function updateSettings(array $settings, bool $reinit = true): \Generator
{ {
$settings = self::getSettings($settings, $this->settings); $settings = self::parseSettings($settings, $this->settings);
if ($settings['app_info'] === null) { if ($settings['app_info'] === null) {
throw new \danog\MadelineProto\Exception(Lang::$current_lang['api_not_set'], 0, null, 'MadelineProto', 1); throw new \danog\MadelineProto\Exception(Lang::$current_lang['api_not_set'], 0, null, 'MadelineProto', 1);
} }
@ -1312,6 +1314,15 @@ class MTProto extends AsyncConstruct implements TLCallback
yield from $this->initAsynchronously(); yield from $this->initAsynchronously();
} }
} }
/**
* Return current settings array.
*
* @return array
*/
public function getSettings(): array
{
return $this->settings;
}
/** /**
* Setup logger. * Setup logger.
* *

View File

@ -42,7 +42,7 @@ class MyTelegramOrgWrapper
} }
public function __construct($settings = []) public function __construct($settings = [])
{ {
$this->settings = MTProto::getSettings($settings, $this->settings); $this->settings = MTProto::parseSettings($settings, $this->settings);
$this->__wakeup(); $this->__wakeup();
} }
public function __wakeup() public function __wakeup()
@ -53,7 +53,7 @@ class MyTelegramOrgWrapper
if (!$this->jar || !$this->jar instanceof InMemoryCookieJar) { if (!$this->jar || !$this->jar instanceof InMemoryCookieJar) {
$this->jar = new InMemoryCookieJar(); $this->jar = new InMemoryCookieJar();
} }
$this->settings = MTProto::getSettings($this->settings); $this->settings = MTProto::parseSettings($this->settings);
$this->datacenter = new DataCenter(new class($this->settings) { $this->datacenter = new DataCenter(new class($this->settings) {
public function __construct($settings) public function __construct($settings)
{ {

View File

@ -19,74 +19,147 @@
namespace danog\MadelineProto\TL\Types; namespace danog\MadelineProto\TL\Types;
use danog\MadelineProto\MTProto;
use danog\MadelineProto\Tools;
class Button implements \JsonSerializable, \ArrayAccess class Button implements \JsonSerializable, \ArrayAccess
{ {
use \danog\Serializable; /**
use \danog\MadelineProto\Tools; * Button data.
private $info = []; */
private $data = []; private array $button;
public function __magic_construct($API, $message, $button) /**
* MTProto instance.
*/
private MTProto $API;
/**
* Message ID.
*/
private int $id;
/**
* Peer ID.
*
* @var array|int
*/
private $peer;
/**
* Constructor function.
*
* @param MTProto $API API instance
* @param array $message Message
* @param array $button Button info
*/
public function __construct(MTProto $API, array $message, array $button)
{ {
$this->data = $button; $this->button = $button;
$this->info['peer'] = $message['to_id'] === ['_' => 'peerUser', 'user_id' => $API->authorization['user']['id']] ? $message['from_id'] : $message['to_id']; $this->peer = $message['to_id'] === ['_' => 'peerUser', 'user_id' => $API->authorization['user']['id']] ? $message['from_id'] : $message['to_id'];
$this->info['id'] = $message['id']; $this->id = $message['id'];
$this->info['API'] = $API; $this->API = $API;
} }
public function __sleep() /**
* Sleep function.
*
* @return array
*/
public function __sleep(): array
{ {
return ['data', 'info']; return ['button', 'peer', 'id', 'API'];
} }
public function click($donotwait = false, $params = []) /**
* Click on button.
*
* @param boolean $donotwait Whether to wait for the result of the method
*
* @return mixed
*/
public function click(bool $donotwait = true)
{ {
if (\is_array($donotwait)) { $async = isset($this->API->wrapper) ? $this->API->wrapper->isAsync() : true;
$params = $donotwait;
$donotwait = false;
}
$async = $params['async'] ?? (isset($this->info['API']->wrapper) ? $this->info['API']->wrapper->async : true);
$method = $donotwait ? 'methodCallAsyncWrite' : 'methodCallAsyncRead'; $method = $donotwait ? 'methodCallAsyncWrite' : 'methodCallAsyncRead';
switch ($this->data['_']) { switch ($this->button['_']) {
default: default:
return false; return false;
case 'keyboardButtonUrl': case 'keyboardButtonUrl':
return $this->data['url']; return $this->button['url'];
case 'keyboardButton': case 'keyboardButton':
$res = $this->info['API']->methodCallAsyncRead('messages.sendMessage', ['peer' => $this->info['peer'], 'message' => $this->data['text'], 'reply_to_msg_id' => $this->info['id']], ['datacenter' => $this->info['API']->datacenter->curdc]); $res = $this->API->methodCallAsyncRead('messages.sendMessage', ['peer' => $this->peer, 'message' => $this->button['text'], 'reply_to_msg_id' => $this->id], ['datacenter' => $this->API->datacenter->curdc]);
break; break;
case 'keyboardButtonCallback': case 'keyboardButtonCallback':
$res = $this->info['API']->{$method}('messages.getBotCallbackAnswer', ['peer' => $this->info['peer'], 'msg_id' => $this->info['id'], 'data' => $this->data['data']], ['datacenter' => $this->info['API']->datacenter->curdc]); $res = $this->API->{$method}('messages.getBotCallbackAnswer', ['peer' => $this->peer, 'msg_id' => $this->id, 'data' => $this->button['data']], ['datacenter' => $this->API->datacenter->curdc]);
break; break;
case 'keyboardButtonGame': case 'keyboardButtonGame':
$res = $this->info['API']->{$method}('messages.getBotCallbackAnswer', ['peer' => $this->info['peer'], 'msg_id' => $this->info['id'], 'game' => true], ['datacenter' => $this->info['API']->datacenter->curdc]); $res = $this->API->{$method}('messages.getBotCallbackAnswer', ['peer' => $this->peer, 'msg_id' => $this->id, 'game' => true], ['datacenter' => $this->API->datacenter->curdc]);
break; break;
} }
return $async ? $res : \danog\MadelineProto\Tools::wait($res); return $async ? $res : Tools::wait($res);
} }
public function __debugInfo() /**
* Get debug info.
*
* @return array
*/
public function __debugInfo(): array
{ {
return ['data' => $this->data, 'info' => ['peer' => $this->info['peer'], 'id' => $this->info['id']]]; $res = \get_object_vars($this);
unset($res['API']);
return $res;
} }
public function jsonSerialize() /**
* Serialize button.
*
* @return array
*/
public function jsonSerialize(): array
{ {
return (array) $this->data; return $this->button;
} }
public function offsetSet($name, $value) /**
* Set button info.
*
* @param $name Offset
* @param mixed $value Value
*
* @return void
*/
public function offsetSet($name, $value): void
{ {
if ($name === null) { if ($name === null) {
$this->data[] = $value; $this->button[] = $value;
} else { } else {
$this->data[$name] = $value; $this->button[$name] = $value;
} }
} }
/**
* Get button info.
*
* @param $name Field name
*
* @return void
*/
public function offsetGet($name) public function offsetGet($name)
{ {
return $this->data[$name]; return $this->button[$name];
} }
public function offsetUnset($name) /**
* Unset button info.
*
* @param $name Offset
*
* @return void
*/
public function offsetUnset($name): void
{ {
unset($this->data[$name]); unset($this->button[$name]);
} }
public function offsetExists($name) /**
* Check if button field exists.
*
* @param $name Offset
*
* @return boolean
*/
public function offsetExists($name): bool
{ {
return isset($this->data[$name]); return isset($this->button[$name]);
} }
} }

View File

@ -21,42 +21,95 @@ namespace danog\MadelineProto\TL\Types;
class Bytes implements \JsonSerializable, \ArrayAccess class Bytes implements \JsonSerializable, \ArrayAccess
{ {
use \danog\Serializable; /**
private $bytes = []; * Bytes.
public function __magic_construct($bytes) *
* @var string Bytes
*/
private string $bytes;
/**
* Constructor function.
*
* @param string $bytes Contents
*/
public function __construct(string $bytes)
{ {
$this->bytes = $bytes; $this->bytes = $bytes;
} }
public function __sleep() /**
* Sleep function.
*
* @return array
*/
public function __sleep(): array
{ {
return ['bytes']; return ['bytes'];
} }
public function __toString() /**
* Cast bytes to string.
*
* @return string
*/
public function __toString(): string
{ {
return $this->bytes; return $this->bytes;
} }
public function jsonSerialize() /**
* Obtain values for JSON-encoding.
*
* @return array
*/
public function jsonSerialize(): array
{ {
return ['_' => 'bytes', 'bytes' => \base64_encode($this->bytes)]; return ['_' => 'bytes', 'bytes' => \base64_encode($this->bytes)];
} }
public function offsetSet($name, $value) /**
* Set char at offset.
*
* @param integer|null $offset Offset
* @param string $value Char
*
* @return void
*/
public function offsetSet($offset, $value): void
{ {
if ($name === null) { if ($offset === null) {
$this->bytes .= $value; $this->bytes .= $value;
} else { } else {
$this->bytes[$name] = $value; $this->bytes[$offset] = $value;
} }
} }
public function offsetGet($name) /**
* Get char at offset.
*
* @param integer $offset Name
*
* @return string
*/
public function offsetGet($offset): string
{ {
return $this->bytes[$name]; return $this->bytes[$offset];
} }
public function offsetUnset($name) /**
* Unset char at offset.
*
* @param integer $offset Offset
*
* @return void
*/
public function offsetUnset($offset): void
{ {
unset($this->bytes[$name]); unset($this->bytes[$offset]);
} }
public function offsetExists($name) /**
* Check if char at offset exists.
*
* @param integer $offset Offset
*
* @return boolean
*/
public function offsetExists($offset): bool
{ {
return isset($this->bytes[$name]); return isset($this->bytes[$offset]);
} }
} }