1 | <?php |
||||
2 | |||||
3 | declare(strict_types=1); |
||||
4 | |||||
5 | namespace Locastic\SymfonyTranslationBundle\Utils; |
||||
6 | |||||
7 | use Locastic\SymfonyTranslationBundle\Model\SearchTranslation; |
||||
8 | use Locastic\SymfonyTranslationBundle\Model\TranslationInterface; |
||||
9 | use Locastic\SymfonyTranslationBundle\Provider\TranslationsProviderInterface; |
||||
10 | use Locastic\SymfonyTranslationBundle\Transformer\TranslationKeyToTranslationTransformerInterface; |
||||
11 | use Pagerfanta\Adapter\ArrayAdapter; |
||||
12 | use Pagerfanta\Pagerfanta; |
||||
13 | use Pagerfanta\PagerfantaInterface; |
||||
14 | use Symfony\Component\Form\FormInterface; |
||||
15 | use Symfony\Component\HttpFoundation\Request; |
||||
16 | |||||
17 | use function array_filter; |
||||
18 | use function mb_strpos; |
||||
19 | |||||
20 | final class SearchTranslationsUtils implements SearchTranslationsUtilsInterface |
||||
21 | { |
||||
22 | private TranslationsProviderInterface $translationsProvider; |
||||
23 | |||||
24 | private TranslationKeyToTranslationTransformerInterface $translationTransformer; |
||||
25 | |||||
26 | private string $localeCode; |
||||
27 | |||||
28 | private array $locales; |
||||
29 | |||||
30 | public function __construct( |
||||
31 | TranslationsProviderInterface $translationsProvider, |
||||
32 | TranslationKeyToTranslationTransformerInterface $translationTransformer, |
||||
33 | string $localeCode, |
||||
34 | array $locales |
||||
35 | ) { |
||||
36 | $this->translationsProvider = $translationsProvider; |
||||
37 | $this->translationTransformer = $translationTransformer; |
||||
38 | $this->localeCode = $localeCode; |
||||
39 | $this->locales = $locales; |
||||
40 | } |
||||
41 | |||||
42 | public function searchTranslationsFromRequest( |
||||
43 | Request $request, |
||||
44 | SearchTranslation $search, |
||||
45 | FormInterface $searchForm |
||||
46 | ): PagerfantaInterface { |
||||
47 | $translations = $this->translationsProvider->getTranslations($this->localeCode, $this->locales); |
||||
48 | $translations = $this->translationsProvider->defineAllKeys($translations, $this->locales); |
||||
49 | $translations = $this->translationTransformer->transformMultiple($translations); |
||||
50 | if ($searchForm->isSubmitted() && $searchForm->isValid()) { |
||||
51 | $translations = array_filter($translations, function (TranslationInterface $translation) use ($search): bool { |
||||
52 | if (false !== mb_strpos($translation->getKey(), $search->getSearch())) { |
||||
0 ignored issues
–
show
Bug
introduced
by
![]() It seems like
$translation->getKey() can also be of type null ; however, parameter $haystack of mb_strpos() does only seem to accept string , maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
53 | return true; |
||||
54 | } |
||||
55 | foreach ($translation->getValues() as $translationValue) { |
||||
56 | if (false !== mb_strpos($translationValue->getValue(), $search->getSearch())) { |
||||
57 | return true; |
||||
58 | } |
||||
59 | } |
||||
60 | |||||
61 | return false; |
||||
62 | }); |
||||
63 | } |
||||
64 | |||||
65 | $adapter = new ArrayAdapter($translations); |
||||
66 | $pagerFanta = new Pagerfanta($adapter); |
||||
67 | |||||
68 | if (null !== $search->getSearch() && $pagerFanta->getNbResults() > 0) { |
||||
69 | $pagerFanta->setMaxPerPage($pagerFanta->getNbResults()); |
||||
70 | $request->query->remove('page'); |
||||
71 | } else { |
||||
72 | $pagerFanta->setMaxPerPage(50); |
||||
73 | } |
||||
74 | $pagerFanta->setCurrentPage($request->query->getInt('page', 1)); |
||||
75 | |||||
76 | return $pagerFanta; |
||||
77 | } |
||||
78 | } |
||||
79 |