Test Setup Failed
Pull Request — master (#4522)
by Craig
08:26 queued 03:47
created

SearchRuntime   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 74
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 27
dl 0
loc 74
rs 10
c 0
b 0
f 0
wmc 12

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B searchVarToFieldNames() 0 22 7
A highlightWords() 0 8 2
A generateUrl() 0 9 2
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Zikula package.
7
 *
8
 * Copyright Zikula - https://ziku.la/
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Zikula\SearchModule\Twig;
15
16
use Symfony\Component\Routing\Exception\RouteNotFoundException;
17
use Symfony\Component\Routing\RouterInterface;
18
use Twig\Extension\RuntimeExtensionInterface;
19
use Zikula\Bundle\CoreBundle\RouteUrl;
20
use Zikula\ExtensionsModule\Api\ApiInterface\VariableApiInterface;
21
22
class SearchRuntime implements RuntimeExtensionInterface
23
{
24
    /**
25
     * @var VariableApiInterface
26
     */
27
    private $variableApi;
28
29
    /**
30
     * @var RouterInterface
31
     */
32
    private $router;
33
34
    public function __construct(VariableApiInterface $variableApi, RouterInterface $router)
35
    {
36
        $this->variableApi = $variableApi;
37
        $this->router = $router;
38
    }
39
40
    /**
41
     * The zikulasearchmodule_searchVarToFieldNames function generates a flat lost of field names
42
     * for hidden form fields from a nested array set.
43
     *
44
     * @param array|string $data The data that should be stored in hidden fields (nested arrays allowed).
45
     *                           If an empty string is given and $isRecursiveCall is false the module vars are used by default
46
     */
47
    public function searchVarToFieldNames($data = '', string $prefix = 'modvar', bool $isRecursiveCall = false): array
48
    {
49
        $dataValues = '' !== $data && $isRecursiveCall ? $data : $this->variableApi->getAll('ZikulaSearchModule');
50
51
        $fields = [];
52
        if (empty($dataValues)) {
53
            return $fields;
54
        }
55
56
        if (is_array($dataValues)) {
57
            foreach ($dataValues as $key => $entryData) {
58
                if (empty($entryData)) {
59
                    continue;
60
                }
61
                $subFields = $this->searchVarToFieldNames($entryData, $prefix . '[' . $key . ']', true);
62
                $fields = array_merge($fields, $subFields);
63
            }
64
        } else {
65
            $fields[$prefix] = $dataValues;
66
        }
67
68
        return $fields;
69
    }
70
71
    /**
72
     * Generate the url from a search result.
73
     */
74
    public function generateUrl(RouteUrl $routeUrl): string
75
    {
76
        try {
77
            $url = $this->router->generate($routeUrl->getRoute(), $routeUrl->getArgs()) . $routeUrl->getFragment();
78
        } catch (RouteNotFoundException $exception) {
79
            $url = '';
80
        }
81
82
        return $url;
83
    }
84
85
    /**
86
     * Highlight words in a string by adding `class="highlight1"`.
87
     */
88
    public function highlightWords(string $string, string $words, int $highlightType = 1): string
89
    {
90
        $singleWords = explode(' ', $words);
91
        foreach ($singleWords as $word) {
92
            $string = str_ireplace($word, '<strong class="highlight' . $highlightType . '">' . $word . '</strong>', $string);
93
        }
94
95
        return $string;
96
    }
97
}
98