Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
95.83% |
23 / 24 |
|
0.00% |
0 / 1 |
CRAP | |
0.00% |
0 / 1 |
| FormsHelper | |
95.83% |
23 / 24 |
|
0.00% |
0 / 1 |
8 | |
0.00% |
0 / 1 |
| filterAndSortFormData | |
95.83% |
23 / 24 |
|
0.00% |
0 / 1 |
8 | |||
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * The forms helper class to reduce complexity in Forms. |
| 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 | * @author Jan Harms <model_railroader@gmx-topmail.de> |
| 13 | * @copyright 2025-2026 phpMyFAQ Team |
| 14 | * @license https://www.mozilla.org/MPL/2.0/ Mozilla Public License Version 2.0 |
| 15 | * @link https://www.phpmyfaq.de |
| 16 | * @since 2025-11-07 |
| 17 | */ |
| 18 | |
| 19 | declare(strict_types=1); |
| 20 | |
| 21 | namespace phpMyFAQ\Form; |
| 22 | |
| 23 | use phpMyFAQ\Translation; |
| 24 | |
| 25 | final class FormsHelper |
| 26 | { |
| 27 | /** |
| 28 | * @param \stdClass[] $formData |
| 29 | * @return \stdClass[] |
| 30 | */ |
| 31 | public function filterAndSortFormData(array $formData, Translation $translation): array |
| 32 | { |
| 33 | foreach ($formData as $input) { |
| 34 | if ($input->input_lang !== 'default') { |
| 35 | continue; |
| 36 | } |
| 37 | |
| 38 | $input->input_label = Translation::get((string) $input->input_label); |
| 39 | } |
| 40 | |
| 41 | $filteredEntries = []; |
| 42 | $fallbackCandidates = []; |
| 43 | $seenIds = []; |
| 44 | |
| 45 | foreach ($formData as $entry) { |
| 46 | if ($entry->input_lang === $translation->getCurrentLanguage()) { |
| 47 | $filteredEntries[] = $entry; |
| 48 | $seenIds[] = $entry->input_id; |
| 49 | continue; |
| 50 | } |
| 51 | |
| 52 | $fallbackCandidates[] = $entry; |
| 53 | } |
| 54 | |
| 55 | foreach ($fallbackCandidates as $fallbackCandidate) { |
| 56 | if ( |
| 57 | !( |
| 58 | $fallbackCandidate->input_lang === 'default' |
| 59 | && !in_array($fallbackCandidate->input_id, $seenIds, strict: true) |
| 60 | ) |
| 61 | ) { |
| 62 | continue; |
| 63 | } |
| 64 | |
| 65 | $filteredEntries[] = $fallbackCandidate; |
| 66 | } |
| 67 | |
| 68 | usort( |
| 69 | $filteredEntries, |
| 70 | static fn(\stdClass $a, \stdClass $b): int => (int) $a->input_id <=> (int) $b->input_id, |
| 71 | ); |
| 72 | return $filteredEntries; |
| 73 | } |
| 74 | } |