MadelineProtoDocs/docs/PHPInternal/danog/MadelineProto/Tools.md

810 lines
15 KiB
Markdown
Raw Normal View History

2020-10-15 18:42:54 +02:00
---
title: danog\MadelineProto\Tools: Some tools.
description:
---
# `danog\MadelineProto\Tools`
[Back to index](../../index.md)
> Author: Daniil Gentili <daniil@daniil.it>
Some tools.
## Method list:
* `genVectorHash(array $ints): \int Vector hash`
* `randomInt(int $modulus): int`
* `random(int $length): \string Random string`
* `posmod(int $a, int $b): \int Modulo`
* `unpackSignedInt(string $value): int`
* `unpackSignedLong(string $value): int`
* `unpackSignedLongString(string $value): string`
* `packSignedInt(int $value): string`
* `packSignedLong(int $value): string`
* `packUnsignedInt(int $value): string`
* `packDouble(float $value): string`
* `unpackDouble(string $value): float`
* `wait(\Generator|\Amp\Promise $promise, bool $ignoreSignal): mixed`
* `all((\Generator|\Amp\Promise)[] $promises): \Amp\Promise`
* `any((\Amp\Promise|\Generator)[] $promises): \Amp\Promise`
* `some((\Amp\Promise|\Generator)[] $promises): \Amp\Promise`
* `first((\Amp\Promise|\Generator)[] $promises): \Amp\Promise`
* `timeout(\Generator|\Amp\Promise $promise, int $timeout): \Amp\Promise`
* `timeoutWithDefault(\Amp\Promise|\Generator $promise, int $timeout, mixed $default): \Amp\Promise<\TReturn>|\Amp\Promise<\TReturnAlt>`
* `call(\Generator|\Amp\Promise|mixed $promise): \Amp\Promise`
* `callFork(\Generator|\Amp\Promise $promise, ?\Generator|\Amp\Promise $actual, string $file): \Amp\Promise|mixed`
* `callForkDefer(\Generator|\Amp\Promise $promise): void`
* `rethrow(\Throwable $e, string $file): void`
* `after(\Generator|\Amp\Promise $a, \Generator|\Amp\Promise $b): \Amp\Promise`
* `flock(string $file, int $operation, float $polling, ?\Amp\Promise $token, ?callable $failureCb): \Amp\Promise<?callable>`
* `sleep(int|float $time): \Amp\Promise`
* `readLine(string $prompt): \Amp\Promise<string>`
* `echo(string $string): \Amp\Promise`
* `isArrayOrAlike(mixed $var): bool`
* `arr(mixed $params): array`
* `base64urlDecode(string $data): string`
* `base64urlEncode(string $data): string`
* `rleDecode(string $string): string`
* `rleEncode(string $string): string`
* `inflateStripped(string $stripped): \string JPG payload`
* `end(array $what): mixed`
* `isAltervista(): bool`
* `hasVar(object $obj, string $var): bool`
* `getVar(object $obj, string $var): mixed`
* `setVar(object $obj, string $var, mixed $val): void`
* `toCamelCase(string $input): string`
* `toSnakeCase(string $input): string`
* `markdownEscape(string $hwat): string`
* `typeEscape(string $type): string`
* `methodEscape(string $method): string`
* `getMimeFromExtension(string $extension, string $default): string`
* `getExtensionFromMime(string $mime): string`
* `getExtensionFromLocation(mixed $location, string $default): string`
* `getMimeFromFile(string $file): string`
* `getMimeFromBuffer(string $buffer): string`
## Methods:
### `genVectorHash(array $ints): \int Vector hash`
Generate MTProto vector hash.
Parameters:
* `$ints`: `array` IDs
Return value: Vector hash
2020-10-15 19:11:10 +02:00
---
Generated by [danog/phpdoc](https://phpdoc.daniil.it)
2020-10-15 18:42:54 +02:00
### `randomInt(int $modulus): int`
Get random integer.
Parameters:
* `$modulus`: `int` Modulus
2020-10-15 19:11:10 +02:00
---
Generated by [danog/phpdoc](https://phpdoc.daniil.it)
2020-10-15 18:42:54 +02:00
### `random(int $length): \string Random string`
Get random string of specified length.
Parameters:
* `$length`: `int` Length
Return value: Random string
2020-10-15 19:11:10 +02:00
---
Generated by [danog/phpdoc](https://phpdoc.daniil.it)
2020-10-15 18:42:54 +02:00
### `posmod(int $a, int $b): \int Modulo`
Positive modulo
Works just like the % (modulus) operator, only returns always a postive number.
Parameters:
* `$a`: `int` A
* `$b`: `int` B
Return value: Modulo
2020-10-15 19:11:10 +02:00
---
Generated by [danog/phpdoc](https://phpdoc.daniil.it)
2020-10-15 18:42:54 +02:00
### `unpackSignedInt(string $value): int`
Unpack base256 signed int.
Parameters:
* `$value`: `string` base256 int
2020-10-15 19:11:10 +02:00
---
Generated by [danog/phpdoc](https://phpdoc.daniil.it)
2020-10-15 18:42:54 +02:00
### `unpackSignedLong(string $value): int`
Unpack base256 signed long.
Parameters:
* `$value`: `string` base256 long
2020-10-15 19:11:10 +02:00
---
Generated by [danog/phpdoc](https://phpdoc.daniil.it)
2020-10-15 18:42:54 +02:00
### `unpackSignedLongString(string $value): string`
Unpack base256 signed long to string.
Parameters:
* `$value`: `string` base256 long
2020-10-15 19:11:10 +02:00
---
Generated by [danog/phpdoc](https://phpdoc.daniil.it)
2020-10-15 18:42:54 +02:00
### `packSignedInt(int $value): string`
Convert integer to base256 signed int.
Parameters:
* `$value`: `int` Value to convert
2020-10-15 19:11:10 +02:00
---
Generated by [danog/phpdoc](https://phpdoc.daniil.it)
2020-10-15 18:42:54 +02:00
### `packSignedLong(int $value): string`
Convert integer to base256 long.
Parameters:
* `$value`: `int` Value to convert
2020-10-15 19:11:10 +02:00
---
Generated by [danog/phpdoc](https://phpdoc.daniil.it)
2020-10-15 18:42:54 +02:00
### `packUnsignedInt(int $value): string`
Convert value to unsigned base256 int.
Parameters:
* `$value`: `int` Value
2020-10-15 19:11:10 +02:00
---
Generated by [danog/phpdoc](https://phpdoc.daniil.it)
2020-10-15 18:42:54 +02:00
### `packDouble(float $value): string`
Convert double to binary version.
Parameters:
* `$value`: `float` Value to convert
2020-10-15 19:11:10 +02:00
---
Generated by [danog/phpdoc](https://phpdoc.daniil.it)
2020-10-15 18:42:54 +02:00
### `unpackDouble(string $value): float`
Unpack binary double.
Parameters:
* `$value`: `string` Value to unpack
2020-10-15 19:11:10 +02:00
---
Generated by [danog/phpdoc](https://phpdoc.daniil.it)
2020-10-15 18:42:54 +02:00
### `wait(\Generator|\Amp\Promise $promise, bool $ignoreSignal): mixed`
Synchronously wait for a promise|generator.
Parameters:
* `$promise`: `\Generator|\Amp\Promise` The promise to wait for
* `$ignoreSignal`: `bool` Whether to ignore shutdown signals
#### See also:
* `\Generator`
* `\Amp\Promise`
2020-10-15 19:11:10 +02:00
---
Generated by [danog/phpdoc](https://phpdoc.daniil.it)
2020-10-15 18:42:54 +02:00
### `all((\Generator|\Amp\Promise)[] $promises): \Amp\Promise`
Returns a promise that succeeds when all promises succeed, and fails if any promise fails.
Returned promise succeeds with an array of values used to succeed each contained promise, with keys corresponding to the array of promises.
Parameters:
* `$promises`: `(\Generator|\Amp\Promise)[]` Promises
#### See also:
* `\Generator`
* `\Amp\Promise`
2020-10-15 19:11:10 +02:00
---
Generated by [danog/phpdoc](https://phpdoc.daniil.it)
2020-10-15 18:42:54 +02:00
### `any((\Amp\Promise|\Generator)[] $promises): \Amp\Promise`
Returns a promise that is resolved when all promises are resolved. The returned promise will not fail.
Parameters:
* `$promises`: `(\Amp\Promise|\Generator)[]` Promises
#### See also:
* `\Amp\Promise`
* `\Generator`
2020-10-15 19:11:10 +02:00
---
Generated by [danog/phpdoc](https://phpdoc.daniil.it)
2020-10-15 18:42:54 +02:00
### `some((\Amp\Promise|\Generator)[] $promises): \Amp\Promise`
Resolves with a two-item array delineating successful and failed Promise results.
The returned promise will only fail if the given number of required promises fail.
Parameters:
* `$promises`: `(\Amp\Promise|\Generator)[]` Promises
#### See also:
* `\Amp\Promise`
* `\Generator`
2020-10-15 19:11:10 +02:00
---
Generated by [danog/phpdoc](https://phpdoc.daniil.it)
2020-10-15 18:42:54 +02:00
### `first((\Amp\Promise|\Generator)[] $promises): \Amp\Promise`
Returns a promise that succeeds when the first promise succeeds, and fails only if all promises fail.
Parameters:
* `$promises`: `(\Amp\Promise|\Generator)[]` Promises
#### See also:
* `\Amp\Promise`
* `\Generator`
2020-10-15 19:11:10 +02:00
---
Generated by [danog/phpdoc](https://phpdoc.daniil.it)
2020-10-15 18:42:54 +02:00
### `timeout(\Generator|\Amp\Promise $promise, int $timeout): \Amp\Promise`
Create an artificial timeout for any \Generator or Promise.
Parameters:
* `$promise`: `\Generator|\Amp\Promise`
* `$timeout`: `int`
#### See also:
* `\Generator`
* `\Amp\Promise`
2020-10-15 19:11:10 +02:00
---
Generated by [danog/phpdoc](https://phpdoc.daniil.it)
2020-10-15 18:42:54 +02:00
### `timeoutWithDefault(\Amp\Promise|\Generator $promise, int $timeout, mixed $default): \Amp\Promise<\TReturn>|\Amp\Promise<\TReturnAlt>`
Creates an artificial timeout for any `Promise`.
If the promise is resolved before the timeout expires, the result is returned
If the timeout expires before the promise is resolved, a default value is returned
Parameters:
* `$promise`: `\Amp\Promise|\Generator` Promise to which the timeout is applied.
Full type:
```
\Amp\Promise<\TReturn>|\TGenerator
```
* `$timeout`: `int` Timeout in milliseconds.
* `$default`: `mixed`
Full type:
```
\TReturnAlt
```
#### See also:
* `\Amp\Promise`
* `\Generator`
* `\TReturn`
* `\TGenerator`
* `\TReturnAlt`
2020-10-15 19:11:10 +02:00
---
Generated by [danog/phpdoc](https://phpdoc.daniil.it)
2020-10-15 18:42:54 +02:00
### `call(\Generator|\Amp\Promise|mixed $promise): \Amp\Promise`
Convert generator, promise or any other value to a promise.
Parameters:
* `$promise`: `\Generator|\Amp\Promise|mixed`
Full type:
```
\Generator<mixed, mixed, mixed, \TReturn>|\Amp\Promise<\TReturn>|\TReturn
```
Fully typed return value:
```
\Amp\Promise<\TReturn>
```
#### See also:
* `\Generator`
* `\Amp\Promise`
* `\TReturn`
2020-10-15 19:11:10 +02:00
---
Generated by [danog/phpdoc](https://phpdoc.daniil.it)
2020-10-15 18:42:54 +02:00
### `callFork(\Generator|\Amp\Promise $promise, ?\Generator|\Amp\Promise $actual, string $file): \Amp\Promise|mixed`
Call promise in background.
Parameters:
* `$promise`: `\Generator|\Amp\Promise` Promise to resolve
* `$actual`: `?\Generator|\Amp\Promise` Promise to resolve instead of $promise
* `$file`: `string` File
#### See also:
* `\Generator`
* `\Amp\Promise`
2020-10-15 19:11:10 +02:00
---
Generated by [danog/phpdoc](https://phpdoc.daniil.it)
2020-10-15 18:42:54 +02:00
### `callForkDefer(\Generator|\Amp\Promise $promise): void`
Call promise in background, deferring execution.
Parameters:
* `$promise`: `\Generator|\Amp\Promise` Promise to resolve
#### See also:
* `\Generator`
* `\Amp\Promise`
2020-10-15 19:11:10 +02:00
---
Generated by [danog/phpdoc](https://phpdoc.daniil.it)
2020-10-15 18:42:54 +02:00
### `rethrow(\Throwable $e, string $file): void`
Rethrow error catched in strand.
Parameters:
* `$e`: `\Throwable` Exception
* `$file`: `string` File where the strand started
#### See also:
* `\Throwable`
2020-10-15 19:11:10 +02:00
---
Generated by [danog/phpdoc](https://phpdoc.daniil.it)
2020-10-15 18:42:54 +02:00
### `after(\Generator|\Amp\Promise $a, \Generator|\Amp\Promise $b): \Amp\Promise`
Call promise $b after promise $a.
Parameters:
* `$a`: `\Generator|\Amp\Promise` Promise A
* `$b`: `\Generator|\Amp\Promise` Promise B
#### See also:
* `\Generator`
* `\Amp\Promise`
2020-10-15 19:11:10 +02:00
---
Generated by [danog/phpdoc](https://phpdoc.daniil.it)
2020-10-15 18:42:54 +02:00
### `flock(string $file, int $operation, float $polling, ?\Amp\Promise $token, ?callable $failureCb): \Amp\Promise<?callable>`
Asynchronously lock a file
Resolves with a callbable that MUST eventually be called in order to release the lock.
Parameters:
* `$file`: `string` File to lock
* `$operation`: `int` Locking mode
* `$polling`: `float` Polling interval
* `$token`: `?\Amp\Promise` Cancellation token
* `$failureCb`: `?callable` Failure callback, called only once if the first locking attempt fails.
#### See also:
* `\Amp\Promise`
2020-10-15 19:11:10 +02:00
---
Generated by [danog/phpdoc](https://phpdoc.daniil.it)
2020-10-15 18:42:54 +02:00
### `sleep(int|float $time): \Amp\Promise`
Asynchronously sleep.
Parameters:
* `$time`: `int|float` Number of seconds to sleep for
#### See also:
* `\Amp\Promise`
2020-10-15 19:11:10 +02:00
---
Generated by [danog/phpdoc](https://phpdoc.daniil.it)
2020-10-15 18:42:54 +02:00
### `readLine(string $prompt): \Amp\Promise<string>`
Asynchronously read line.
Parameters:
* `$prompt`: `string` Prompt
#### See also:
* `\Amp\Promise`
2020-10-15 19:11:10 +02:00
---
Generated by [danog/phpdoc](https://phpdoc.daniil.it)
2020-10-15 18:42:54 +02:00
### `echo(string $string): \Amp\Promise`
Asynchronously write to stdout/browser.
Parameters:
* `$string`: `string` Message to echo
#### See also:
* `\Amp\Promise`
2020-10-15 19:11:10 +02:00
---
Generated by [danog/phpdoc](https://phpdoc.daniil.it)
2020-10-15 18:42:54 +02:00
### `isArrayOrAlike(mixed $var): bool`
Check if is array or similar (traversable && countable && arrayAccess).
Parameters:
* `$var`: `mixed` Value to check
2020-10-15 19:11:10 +02:00
---
Generated by [danog/phpdoc](https://phpdoc.daniil.it)
2020-10-15 18:42:54 +02:00
### `arr(mixed $params): array`
Create array.
Parameters:
* `$params`: `mixed` Params
2020-10-15 19:11:10 +02:00
---
Generated by [danog/phpdoc](https://phpdoc.daniil.it)
2020-10-15 18:42:54 +02:00
### `base64urlDecode(string $data): string`
base64URL decode.
Parameters:
* `$data`: `string` Data to decode
2020-10-15 19:11:10 +02:00
---
Generated by [danog/phpdoc](https://phpdoc.daniil.it)
2020-10-15 18:42:54 +02:00
### `base64urlEncode(string $data): string`
Base64URL encode.
Parameters:
* `$data`: `string` Data to encode
2020-10-15 19:11:10 +02:00
---
Generated by [danog/phpdoc](https://phpdoc.daniil.it)
2020-10-15 18:42:54 +02:00
### `rleDecode(string $string): string`
null-byte RLE decode.
Parameters:
* `$string`: `string` Data to decode
2020-10-15 19:11:10 +02:00
---
Generated by [danog/phpdoc](https://phpdoc.daniil.it)
2020-10-15 18:42:54 +02:00
### `rleEncode(string $string): string`
null-byte RLE encode.
Parameters:
* `$string`: `string` Data to encode
2020-10-15 19:11:10 +02:00
---
Generated by [danog/phpdoc](https://phpdoc.daniil.it)
2020-10-15 18:42:54 +02:00
### `inflateStripped(string $stripped): \string JPG payload`
Inflate stripped photosize to full JPG payload.
Parameters:
* `$stripped`: `string` Stripped photosize
Return value: JPG payload
2020-10-15 19:11:10 +02:00
---
Generated by [danog/phpdoc](https://phpdoc.daniil.it)
2020-10-15 18:42:54 +02:00
### `end(array $what): mixed`
Get final element of array.
Parameters:
* `$what`: `array` Array
2020-10-15 19:11:10 +02:00
---
Generated by [danog/phpdoc](https://phpdoc.daniil.it)
2020-10-15 18:42:54 +02:00
### `isAltervista(): bool`
Whether this is altervista.
2020-10-15 19:11:10 +02:00
---
Generated by [danog/phpdoc](https://phpdoc.daniil.it)
2020-10-15 18:42:54 +02:00
### `hasVar(object $obj, string $var): bool`
Checks private property exists in an object.
Parameters:
* `$obj`: `object` Object
* `$var`: `string` Attribute name
2020-10-15 19:11:10 +02:00
---
Generated by [danog/phpdoc](https://phpdoc.daniil.it)
2020-10-15 18:42:54 +02:00
### `getVar(object $obj, string $var): mixed`
Accesses a private variable from an object.
Parameters:
* `$obj`: `object` Object
* `$var`: `string` Attribute name
2020-10-15 19:11:10 +02:00
---
Generated by [danog/phpdoc](https://phpdoc.daniil.it)
2020-10-15 18:42:54 +02:00
### `setVar(object $obj, string $var, mixed $val): void`
Sets a private variable in an object.
Parameters:
* `$obj`: `object` Object
* `$var`: `string` Attribute name
* `$val`: `mixed` Attribute value
2020-10-15 19:11:10 +02:00
---
Generated by [danog/phpdoc](https://phpdoc.daniil.it)
2020-10-15 18:42:54 +02:00
### `toCamelCase(string $input): string`
Convert to camelCase.
Parameters:
* `$input`: `string` String
2020-10-15 19:11:10 +02:00
---
Generated by [danog/phpdoc](https://phpdoc.daniil.it)
2020-10-15 18:42:54 +02:00
### `toSnakeCase(string $input): string`
Convert to snake_case.
Parameters:
* `$input`: `string` String
2020-10-15 19:11:10 +02:00
---
Generated by [danog/phpdoc](https://phpdoc.daniil.it)
2020-10-15 18:42:54 +02:00
### `markdownEscape(string $hwat): string`
Escape string for markdown.
Parameters:
* `$hwat`: `string` String to escape
2020-10-15 19:11:10 +02:00
---
Generated by [danog/phpdoc](https://phpdoc.daniil.it)
2020-10-15 18:42:54 +02:00
### `typeEscape(string $type): string`
Escape type name.
Parameters:
* `$type`: `string` String to escape
2020-10-15 19:11:10 +02:00
---
Generated by [danog/phpdoc](https://phpdoc.daniil.it)
2020-10-15 18:42:54 +02:00
### `methodEscape(string $method): string`
Escape method name.
Parameters:
* `$method`: `string` Method name
2020-10-15 19:11:10 +02:00
---
Generated by [danog/phpdoc](https://phpdoc.daniil.it)
2020-10-15 18:42:54 +02:00
### `getMimeFromExtension(string $extension, string $default): string`
Get mime type from file extension.
Parameters:
* `$extension`: `string` File extension
* `$default`: `string` Default mime type
2020-10-15 19:11:10 +02:00
---
Generated by [danog/phpdoc](https://phpdoc.daniil.it)
2020-10-15 18:42:54 +02:00
### `getExtensionFromMime(string $mime): string`
Get extension from mime type.
Parameters:
* `$mime`: `string` MIME type
2020-10-15 19:11:10 +02:00
---
Generated by [danog/phpdoc](https://phpdoc.daniil.it)
2020-10-15 18:42:54 +02:00
### `getExtensionFromLocation(mixed $location, string $default): string`
Get extension from file location.
Parameters:
* `$location`: `mixed` File location
* `$default`: `string` Default extension
2020-10-15 19:11:10 +02:00
---
Generated by [danog/phpdoc](https://phpdoc.daniil.it)
2020-10-15 18:42:54 +02:00
### `getMimeFromFile(string $file): string`
Get mime type of file.
Parameters:
* `$file`: `string` File
2020-10-15 19:11:10 +02:00
---
Generated by [danog/phpdoc](https://phpdoc.daniil.it)
2020-10-15 18:42:54 +02:00
### `getMimeFromBuffer(string $buffer): string`
Get mime type from buffer.
Parameters:
* `$buffer`: `string` Buffer
2020-10-15 19:11:10 +02:00
---
Generated by [danog/phpdoc](https://phpdoc.daniil.it)
2020-10-15 18:42:54 +02:00
2020-10-15 19:11:10 +02:00
---
Generated by [danog/phpdoc](https://phpdoc.daniil.it)