Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
89.47% |
17 / 19 |
|
0.00% |
0 / 1 |
CRAP | |
0.00% |
0 / 1 |
| LinkQueryParser | |
89.47% |
17 / 19 |
|
0.00% |
0 / 1 |
10.12 | |
0.00% |
0 / 1 |
| parse | |
89.47% |
17 / 19 |
|
0.00% |
0 / 1 |
10.12 | |||
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Helper functions for query and fragment parsing from link URLs. |
| 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-08 |
| 16 | */ |
| 17 | |
| 18 | declare(strict_types=1); |
| 19 | |
| 20 | namespace phpMyFAQ\Link\Util; |
| 21 | |
| 22 | /** |
| 23 | * Hilfsfunktionen für Query- und Fragment-Parsing aus Link-URLs. |
| 24 | */ |
| 25 | final class LinkQueryParser |
| 26 | { |
| 27 | /** |
| 28 | * @return array<string,string> |
| 29 | */ |
| 30 | public static function parse(string $url): array |
| 31 | { |
| 32 | $parameters = []; |
| 33 | if ($url === '' || $url === '0') { |
| 34 | return $parameters; |
| 35 | } |
| 36 | |
| 37 | $parsed = parse_url($url); |
| 38 | if (!is_array($parsed)) { |
| 39 | return $parameters; |
| 40 | } |
| 41 | |
| 42 | if (array_key_exists('query', $parsed) && is_string($parsed['query'])) { |
| 43 | $rawQuery = str_replace(search: ['&', '#38;', 'amp;'], replace: '&', subject: $parsed['query']); |
| 44 | $tmp = []; |
| 45 | parse_str($rawQuery, $tmp); |
| 46 | foreach ($tmp as $k => $v) { |
| 47 | if (!is_scalar($v)) { |
| 48 | continue; |
| 49 | } |
| 50 | |
| 51 | $parameters[$k] = $v; |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | if (array_key_exists('fragment', $parsed) && is_string($parsed['fragment'])) { |
| 56 | $fragment = $parsed['fragment']; |
| 57 | $parameters['#'] = $fragment; // historisch |
| 58 | $parameters['fragment'] = $fragment; |
| 59 | } |
| 60 | |
| 61 | return $parameters; |
| 62 | } |
| 63 | } |