created
This commit is contained in:
10
README.md
10
README.md
@@ -1,16 +1,16 @@
|
|||||||
# unmarkdorn
|
# unmarkdorn
|
||||||
Library for escaping all markdown symbols
|
Library for normalizing Zanzara (Telegram API) objects
|
||||||
|
|
||||||
```php
|
```php
|
||||||
// Library for escaping all markdown symbols
|
// Function for normalizing images
|
||||||
use function mirzaev\unmarkdown;
|
use function mirzaev\unzanzara\images;
|
||||||
|
|
||||||
var_dump(unmarkdown('*Hello!*')); // "\\*Hello\\!\\*"
|
var_dump(images($context->getMessage()?->getPhoto() ?? []); // [id => [[small], [medium], [big]]]
|
||||||
```
|
```
|
||||||
|
|
||||||
## Installation
|
## Installation
|
||||||
```bash
|
```bash
|
||||||
composer require mirzaev/unmarkdown
|
composer require mirzaev/unzanzara
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -1,9 +1,9 @@
|
|||||||
{
|
{
|
||||||
"name": "mirzaev/unmarkdown",
|
"name": "mirzaev/unzanzara",
|
||||||
"description": "Library for escaping all markdown symbols",
|
"description": "Library for normalizing Zanzara (Telegram API) objects ",
|
||||||
"homepage": "https://git.svoboda.works/mirzaev/unmarkdown",
|
"homepage": "https://git.svoboda.works/mirzaev/unzanzara",
|
||||||
"type": "library",
|
"type": "library",
|
||||||
"keywords": ["markdown", "escaping"],
|
"keywords": ["zanzara", "telegram"],
|
||||||
"readme": "README.md",
|
"readme": "README.md",
|
||||||
"license": "WTFPL",
|
"license": "WTFPL",
|
||||||
"authors": [
|
"authors": [
|
||||||
@@ -15,12 +15,12 @@
|
|||||||
}
|
}
|
||||||
],
|
],
|
||||||
"support": {
|
"support": {
|
||||||
"issues": "https://git.svoboda.works/mirzaev/unmarkdown/issues"
|
"issues": "https://git.svoboda.works/mirzaev/unzanzara/issues"
|
||||||
},
|
},
|
||||||
"require": {
|
"require": {
|
||||||
"php": "^8.4"
|
"php": "^8.5"
|
||||||
},
|
},
|
||||||
"autoload": {
|
"autoload": {
|
||||||
"files": ["mirzaev/unmarkdown/system/unmarkdown.php"]
|
"files": ["mirzaev/unzanzara/system/images.php"]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,49 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
declare(strict_types=1);
|
|
||||||
|
|
||||||
namespace mirzaev;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Escape all markdown symbols
|
|
||||||
*
|
|
||||||
* @param string $text Text for escaping
|
|
||||||
* @param array $exception Characters to be excluded from the escape list
|
|
||||||
*
|
|
||||||
* @return string Escaped text
|
|
||||||
*/
|
|
||||||
function unmarkdown(string $text, array $exceptions = []): string
|
|
||||||
{
|
|
||||||
// Initializing the registry of characters for escaping
|
|
||||||
$from = array_diff(
|
|
||||||
[
|
|
||||||
'.',
|
|
||||||
'#',
|
|
||||||
'*',
|
|
||||||
'-',
|
|
||||||
'_',
|
|
||||||
'=',
|
|
||||||
'[',
|
|
||||||
']',
|
|
||||||
'{',
|
|
||||||
'}',
|
|
||||||
'(',
|
|
||||||
')',
|
|
||||||
'>',
|
|
||||||
'<',
|
|
||||||
'!',
|
|
||||||
'`',
|
|
||||||
'\\\\',
|
|
||||||
'|',
|
|
||||||
'+'
|
|
||||||
],
|
|
||||||
$exceptions
|
|
||||||
);
|
|
||||||
|
|
||||||
// Initializing the registry of escaped characters
|
|
||||||
$to = [];
|
|
||||||
foreach ($from as $symbol) $to[] = "\\$symbol";
|
|
||||||
|
|
||||||
// Escaping the text and exit (success)
|
|
||||||
return str_replace($from, $to, $text);
|
|
||||||
}
|
|
||||||
71
mirzaev/unzanzara/system/images.php
Normal file
71
mirzaev/unzanzara/system/images.php
Normal file
@@ -0,0 +1,71 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace mirzaev\unzanzara;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Unzanzara images
|
||||||
|
*
|
||||||
|
* Convert images from Telegram by Zanzara
|
||||||
|
*
|
||||||
|
* from `$context->getMessage()->getPhoto()`
|
||||||
|
* to
|
||||||
|
* [
|
||||||
|
* [
|
||||||
|
* 'identifier' =>
|
||||||
|
* [
|
||||||
|
* [small],
|
||||||
|
* [medium],
|
||||||
|
* [big]
|
||||||
|
* ...
|
||||||
|
* ]
|
||||||
|
* ]
|
||||||
|
* ...
|
||||||
|
*]
|
||||||
|
*
|
||||||
|
* @param array $images Images from `$context->getMessage()->getPhoto()`
|
||||||
|
*
|
||||||
|
* @return array Images grouped by identifier and size
|
||||||
|
*
|
||||||
|
* @package kodorvan\neurobot\models\functions\unzanzara
|
||||||
|
*
|
||||||
|
* @license http://www.wtfpl.net/ Do What The Fuck You Want To Public License
|
||||||
|
* @author Arsen Mirzaev Tatyano-Muradovich <arsen@mirzaev.sexy>
|
||||||
|
*/
|
||||||
|
function images(array $images): array
|
||||||
|
{
|
||||||
|
// Declaring the array of converted images
|
||||||
|
$converted = [];
|
||||||
|
|
||||||
|
foreach ($images as $image) {
|
||||||
|
// Iterating over images
|
||||||
|
|
||||||
|
// Initializing the image in the array of converted images
|
||||||
|
$converted[$image->getFileId()] ??= [];
|
||||||
|
|
||||||
|
// Initializing the image size in the array of converted images
|
||||||
|
$converted[$image->getFileId()][$image->getFileUniqueId()] = [
|
||||||
|
'width' => $image->getWith(),
|
||||||
|
'height' => $image->getHeight(),
|
||||||
|
'size' => $image->getFileSize()
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach ($images as &$image) {
|
||||||
|
// Iterating over images
|
||||||
|
|
||||||
|
// Sorting image sized by width
|
||||||
|
$image = usort(
|
||||||
|
$image,
|
||||||
|
fn(array $a, array $b) => match (true) {
|
||||||
|
$a['width'] < $b['width'] => -1,
|
||||||
|
$a['width'] > $b['width'] => 1,
|
||||||
|
default => 0
|
||||||
|
}
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Exit (success)
|
||||||
|
return $converted;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user