From 8d0b2be534f32599ff85fa1f3176d2d5205b0cb7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=94=D0=BC=D0=B8=D1=82=D1=80=D0=B8=D0=B9?= Date: Tue, 4 Aug 2026 12:28:20 +0300 Subject: [PATCH] =?UTF-8?q?feat(=D0=B2=D0=BE=D1=80=D0=BE=D0=BD=D0=BA=D0=B0?= =?UTF-8?q?):=20=D0=BA=D0=BB=D1=8E=D1=87=20=D1=81=D1=80=D0=B0=D0=B2=D0=BD?= =?UTF-8?q?=D0=B5=D0=BD=D0=B8=D1=8F=20=D0=BD=D0=B8=D1=88=20=E2=80=94=20?= =?UTF-8?q?=D1=80=D0=B5=D0=B3=D0=B8=D1=81=D1=82=D1=80,=20=D1=91,=20=D0=BB?= =?UTF-8?q?=D0=B0=D1=82=D0=B8=D0=BD=D1=81=D0=BA=D0=B8=D0=B5=20=D0=B4=D0=B2?= =?UTF-8?q?=D0=BE=D0=B9=D0=BD=D0=B8=D0=BA=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/app/Support/RubricKey.php | 38 ++++++++++++++++++++++++ app/tests/Unit/Support/RubricKeyTest.php | 30 +++++++++++++++++++ 2 files changed, 68 insertions(+) create mode 100644 app/app/Support/RubricKey.php create mode 100644 app/tests/Unit/Support/RubricKeyTest.php diff --git a/app/app/Support/RubricKey.php b/app/app/Support/RubricKey.php new file mode 100644 index 00000000..d8f5f893 --- /dev/null +++ b/app/app/Support/RubricKey.php @@ -0,0 +1,38 @@ + 'а', 'c' => 'с', 'e' => 'е', 'o' => 'о', 'p' => 'р', 'x' => 'х', + 'y' => 'у', 'k' => 'к', 'm' => 'м', 'h' => 'н', 't' => 'т', 'b' => 'в', + ]; + + public static function make(?string $raw): string + { + $s = mb_strtolower(trim((string) $raw), 'UTF-8'); + + if (preg_match('/\p{Cyrillic}/u', $s) === 1) { + $s = strtr($s, self::LOOKALIKES); + } + + $s = str_replace('ё', 'е', $s); + $s = (string) preg_replace('/[^\p{L}\p{N}]+/u', ' ', $s); + + return trim((string) preg_replace('/\s+/u', ' ', $s)); + } +} diff --git a/app/tests/Unit/Support/RubricKeyTest.php b/app/tests/Unit/Support/RubricKeyTest.php new file mode 100644 index 00000000..047e2816 --- /dev/null +++ b/app/tests/Unit/Support/RubricKeyTest.php @@ -0,0 +1,30 @@ +toBe(RubricKey::make(' окна ')) + ->and(RubricKey::make('Окна'))->toBe(RubricKey::make('«Окна».')) + ->and(RubricKey::make('Ремонт, стройка'))->toBe(RubricKey::make('ремонт стройка')); +}); + +test('ё и е — одна буква', function () { + expect(RubricKey::make('Клёны'))->toBe(RubricKey::make('Клены')); +}); + +test('латинские двойники в русском слове приводятся к кириллице', function () { + // «Окнa» — последняя буква латинская. + expect(RubricKey::make("Окн\u{0061}"))->toBe(RubricKey::make('Окна')); +}); + +test('чисто латинское название не калечится', function () { + expect(RubricKey::make('SPA'))->toBe('spa') + ->and(RubricKey::make('IT'))->toBe('it'); +}); + +test('пустая строка даёт пустой ключ', function () { + expect(RubricKey::make(null))->toBe('') + ->and(RubricKey::make(' '))->toBe(''); +});