This commit is contained in:
2026-01-14 15:30:23 +05:00
parent ae97b9e464
commit d660f4db2c
4 changed files with 83 additions and 61 deletions

View File

@@ -1,16 +1,16 @@
# unmarkdorn
Library for escaping all markdown symbols
Library for normalizing Zanzara (Telegram API) objects
```php
// Library for escaping all markdown symbols
use function mirzaev\unmarkdown;
// Function for normalizing images
use function mirzaev\unzanzara\images;
var_dump(unmarkdown('*Hello!*')); // "\\*Hello\\!\\*"
var_dump(images($context->getMessage()?->getPhoto() ?? []); // [id => [[small], [medium], [big]]]
```
## Installation
```bash
composer require mirzaev/unmarkdown
composer require mirzaev/unzanzara
```

View File

@@ -1,9 +1,9 @@
{
"name": "mirzaev/unmarkdown",
"description": "Library for escaping all markdown symbols",
"homepage": "https://git.svoboda.works/mirzaev/unmarkdown",
"name": "mirzaev/unzanzara",
"description": "Library for normalizing Zanzara (Telegram API) objects ",
"homepage": "https://git.svoboda.works/mirzaev/unzanzara",
"type": "library",
"keywords": ["markdown", "escaping"],
"keywords": ["zanzara", "telegram"],
"readme": "README.md",
"license": "WTFPL",
"authors": [
@@ -15,12 +15,12 @@
}
],
"support": {
"issues": "https://git.svoboda.works/mirzaev/unmarkdown/issues"
"issues": "https://git.svoboda.works/mirzaev/unzanzara/issues"
},
"require": {
"php": "^8.4"
"php": "^8.5"
},
"autoload": {
"files": ["mirzaev/unmarkdown/system/unmarkdown.php"]
"files": ["mirzaev/unzanzara/system/images.php"]
}
}

View File

@@ -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);
}

View 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;
}