39 lines
1.2 KiB
PHP
39 lines
1.2 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace paygw_moneta\local\receipt;
|
|
|
|
/**
|
|
* Текстовые поля чека: «значения не должны содержать кавычек, знаков &, $, #,
|
|
* обратных и прямых слэшей» (`cmsspecification.pdf`).
|
|
*
|
|
* @package paygw_moneta
|
|
* @copyright 2026 Moneta Labs {@link https://moneta.ru/}
|
|
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
*/
|
|
final class ReceiptText
|
|
{
|
|
private function __construct() {}
|
|
|
|
public static function validate(string $value, int $maxLength): string
|
|
{
|
|
$maxLength = max(0, $maxLength);
|
|
$value = self::sanitize($value);
|
|
if (mb_strlen($value, 'UTF-8') <= $maxLength) {
|
|
return $value;
|
|
}
|
|
|
|
return mb_substr($value, 0, max(0, $maxLength - 3), 'UTF-8') . ($maxLength > 3 ? '...' : '');
|
|
}
|
|
|
|
public static function sanitize(string $value): string
|
|
{
|
|
$decoded = html_entity_decode(strip_tags($value), ENT_QUOTES | ENT_HTML5, 'UTF-8');
|
|
$whitelisted = preg_replace('/[^\p{L}\p{N}\s.,()_№+-]/u', '', $decoded) ?? '';
|
|
$collapsed = preg_replace('/\s+/u', ' ', $whitelisted) ?? $whitelisted;
|
|
|
|
return trim($collapsed);
|
|
}
|
|
}
|