Completed
Push — master ( a32fd9...cce3a1 )
by Craig
06:00
created

TwigExtension::highlightGoogleKeywords()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 3
dl 0
loc 4
rs 10
1
<?php
2
3
/*
4
 * This file is part of the Zikula package.
5
 *
6
 * Copyright Zikula Foundation - http://zikula.org/
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Zikula\SearchModule\Twig;
13
14
use Symfony\Component\Routing\Exception\RouteNotFoundException;
15
use Symfony\Component\Routing\RouterInterface;
16
use Zikula\Core\RouteUrl;
17
use Zikula\ExtensionsModule\Api\VariableApi;
18
19
/**
20
 * Twig extension class.
21
 */
22
class TwigExtension extends \Twig_Extension
23
{
24
    /**
25
     * @var VariableApi
26
     */
27
    private $variableApi;
28
29
    /**
30
     * @var RouterInterface
31
     */
32
    private $router;
33
34
    /**
35
     * TwigExtension constructor.
36
     *
37
     * @param VariableApi $variableApi VariableApi service instance
38
     * @param RouterInterface $router
39
     */
40
    public function __construct(VariableApi $variableApi, RouterInterface $router)
41
    {
42
        $this->variableApi = $variableApi;
43
        $this->router = $router;
44
    }
45
46
    /**
47
     * Returns a list of custom Twig functions.
48
     *
49
     * @return array
50
     */
51
    public function getFunctions()
52
    {
53
        return [
54
            new \Twig_SimpleFunction('zikulasearchmodule_searchVarToFieldNames', [$this, 'searchVarToFieldNames']),
55
        ];
56
    }
57
58
    /**
59
     * Returns a list of custom Twig filters.
60
     *
61
     * @return array
62
     */
63
    public function getFilters()
64
    {
65
        return [
66
            new \Twig_SimpleFilter('zikulasearchmodule_highlightGoogleKeywords', [$this, 'highlightGoogleKeywords']), // @deprecated
67
            new \Twig_SimpleFilter('zikulasearchmodule_highlightWords', [$this, 'highlightWords'], ['is_safe' => ['html']]),
68
            new \Twig_SimpleFilter('zikulasearchmodule_generateUrl', [$this, 'generateUrl']),
69
        ];
70
    }
71
72
    /**
73
     * The zikulasearchmodule_searchVarToFieldNames function generates a flat lost of field names
74
     * for hidden form fields from a nested array set
75
     *
76
     * @param array|string $data            The data that should be stored in hidden fields (nested arrays allowed).
77
     *                                      If an empty string is given and $isRecursiveCall is false the module vars are used by default
78
     * @param string       $prefix          Optional prefix
79
     * @param bool         $isRecursiveCall Flag to determine whether this method has been called recursively
80
     *
81
     * @return array List of hidden form fields
82
     */
83
    public function searchVarToFieldNames($data = '', $prefix = 'modvar', $isRecursiveCall = false)
84
    {
85
        $dataValues = $data != '' && $isRecursiveCall ? $data : $this->variableApi->getAll('ZikulaSearchModule');
86
87
        $fields = [];
88
        if (empty($dataValues)) {
89
            return $fields;
90
        }
91
92
        if (is_array($dataValues)) {
93
            foreach ($dataValues as $key => $entryData) {
94
                if (empty($entryData)) {
95
                    continue;
96
                }
97
                $subFields = $this->searchVarToFieldNames($entryData, $prefix . '[' . $key . ']', true);
98
                $fields = array_merge($fields, $subFields);
99
            }
100
        } else {
101
            $fields[$prefix] = $dataValues;
102
        }
103
104
        return $fields;
105
    }
106
107
    /**
108
     * Generate the url from a search result
109
     * @param RouteUrl $url
110
     * @return string
111
     */
112
    public function generateUrl(RouteUrl $url)
113
    {
114
        try {
115
            $url = $this->router->generate($url->getRoute(), $url->getArgs()) . $url->getFragment();
116
        } catch (RouteNotFoundException $exception) {
1 ignored issue
show
Bug introduced by
The class Symfony\Component\Routin...\RouteNotFoundException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
117
            $url = '';
118
        }
119
120
        return $url;
121
    }
122
123
    /**
124
     * Highlight words in a string by adding `class="highlight1"`
125
     * @param string $string
126
     * @param string $words
127
     * @param int $highlightType
128
     * @return string
129
     */
130
    public function highlightWords($string, $words, $highlightType = 1)
131
    {
132
        $words = explode(' ', $words);
133
        foreach ($words as $word) {
134
            $string = str_ireplace($word, '<strong class="highlight' . $highlightType . '">' . $word . '</strong>', $string);
135
        }
136
137
        return $string;
138
    }
139
}
140