Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
26 / 26 |
|
100.00% |
2 / 2 |
CRAP | |
100.00% |
1 / 1 |
| GlossaryHelper | |
100.00% |
26 / 26 |
|
100.00% |
2 / 2 |
9 | |
100.00% |
1 / 1 |
| extractMatchParts | |
100.00% |
19 / 19 |
|
100.00% |
1 / 1 |
8 | |||
| formatTooltip | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * The glossary helper class. |
| 5 | * |
| 6 | * This Source Code Form is subject to the terms of the Mozilla Public License, |
| 7 | * v. 2.0. If a copy of the MPL was not distributed with this file, You can |
| 8 | * obtain one at https://mozilla.org/MPL/2.0/. |
| 9 | * |
| 10 | * @package phpMyFAQ |
| 11 | * @author Thorsten Rinne <thorsten@phpmyfaq.de> |
| 12 | * @copyright 2025 phpMyFAQ Team |
| 13 | * @license https://www.mozilla.org/MPL/2.0/ Mozilla Public License Version 2.0 |
| 14 | * @link https://www.phpmyfaq.de |
| 15 | * @since 2025-11-05 |
| 16 | */ |
| 17 | |
| 18 | declare(strict_types=1); |
| 19 | |
| 20 | namespace phpMyFAQ\Glossary; |
| 21 | |
| 22 | final class GlossaryHelper |
| 23 | { |
| 24 | /** |
| 25 | * Extract prefix, item, postfix from regex matches produced by pattern: /(^|\W)(item)(\W|$)/ |
| 26 | * keeping backward-compatibility with older, more verbose patterns. |
| 27 | * |
| 28 | * @param array<int, string> $matches |
| 29 | * @return array{0:string,1:string,2:string} [prefix, item, postfix] |
| 30 | */ |
| 31 | public function extractMatchParts(array $matches): array |
| 32 | { |
| 33 | $prefix = ''; |
| 34 | $item = ''; |
| 35 | $postfix = ''; |
| 36 | $count = count($matches); |
| 37 | |
| 38 | if ($count > 9) { |
| 39 | $prefix = $matches[9]; |
| 40 | $item = $matches[10]; |
| 41 | } |
| 42 | |
| 43 | if ($item === '' && $count > 7) { |
| 44 | $item = $matches[7]; |
| 45 | $postfix = $matches[8]; |
| 46 | } |
| 47 | |
| 48 | if ($item === '' && $count > 4) { |
| 49 | $prefix = $matches[4]; |
| 50 | $item = $matches[5]; |
| 51 | $postfix = $matches[6]; |
| 52 | } |
| 53 | |
| 54 | if ($item === '' && $count >= 3) { |
| 55 | $prefix = $matches[1] ?? ''; |
| 56 | $item = $matches[2] ?? ''; |
| 57 | $postfix = $matches[3] ?? ''; |
| 58 | } |
| 59 | |
| 60 | return [$prefix, $item, $postfix]; |
| 61 | } |
| 62 | |
| 63 | public function formatTooltip(string $definition, string $item, string $prefix = '', string $postfix = ''): string |
| 64 | { |
| 65 | return sprintf( |
| 66 | '%s<abbr data-bs-toggle="tooltip" data-bs-placement="bottom" title="%s" class="initialism">%s</abbr>%s', |
| 67 | $prefix, |
| 68 | htmlspecialchars($definition, ENT_QUOTES | ENT_HTML5, encoding: 'UTF-8'), |
| 69 | $item, |
| 70 | $postfix, |
| 71 | ); |
| 72 | } |
| 73 | } |