Completed
Push — master ( 518055...b43ef9 )
by Craig
10:18 queued 03:07
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 Zikula\ExtensionsModule\Api\VariableApi;
15
16
/**
17
 * Twig extension class.
18
 */
19
class TwigExtension extends \Twig_Extension
20
{
21
    /**
22
     * @var VariableApi
23
     */
24
    private $variableApi;
25
26
    /**
27
     * TwigExtension constructor.
28
     *
29
     * @param VariableApi $variableApi VariableApi service instance
30
     */
31
    public function __construct(VariableApi $variableApi)
32
    {
33
        $this->variableApi = $variableApi;
34
    }
35
36
    /**
37
     * Returns a list of custom Twig functions.
38
     *
39
     * @return array
40
     */
41
    public function getFunctions()
42
    {
43
        return [
44
            new \Twig_SimpleFunction('zikulasearchmodule_searchVarToFieldNames', [$this, 'searchVarToFieldNames']),
45
        ];
46
    }
47
48
    /**
49
     * Returns a list of custom Twig filters.
50
     *
51
     * @return array
52
     */
53
    public function getFilters()
54
    {
55
        return [
56
            new \Twig_SimpleFilter('zikulasearchmodule_highlightGoogleKeywords', [$this, 'highlightGoogleKeywords']),
57
        ];
58
    }
59
60
    /**
61
     * The zikulasearchmodule_searchVarToFieldNames function generates a flat lost of field names
62
     * for hidden form fields from a nested array set
63
     *
64
     * @param array|string $data            The data that should be stored in hidden fields (nested arrays allowed).
65
     *                                      If an empty string is given and $isRecursiveCall is false the module vars are used by default
66
     * @param string       $prefix          Optional prefix
67
     * @param bool         $isRecursiveCall Flag to determine whether this method has been called recursively
68
     *
69
     * @return array List of hidden form fields
70
     */
71
    public function searchVarToFieldNames($data = '', $prefix = 'modvar', $isRecursiveCall = false)
72
    {
73
        $dataValues = $data != '' && $isRecursiveCall ? $data : $this->variableApi->getAll('ZikulaSearchModule');
74
75
        $fields = [];
76
        if (empty($dataValues)) {
77
            return $fields;
78
        }
79
80
        if (is_array($dataValues)) {
81
            foreach ($dataValues as $key => $entryData) {
82
                if (empty($entryData)) {
83
                    continue;
84
                }
85
                $subFields = $this->searchVarToFieldNames($entryData, $prefix . '[' . $key . ']', true);
86
                $fields = array_merge($fields, $subFields);
87
            }
88
        } else {
89
            $fields[$prefix] = $dataValues;
90
        }
91
92
        return $fields;
93
    }
94
95
    /**
96
     * Highlights case insensitive google search phrase.
97
     *
98
     * @param string  $text         The string to operate on
99
     * @param string  $searchPhrase The search phrase
100
     * @param integer $contextSize  The number of chars shown as context around the search phrase
101
     *
102
     * @return string
103
     */
104
    public function highlightGoogleKeywords($text, $searchPhrase, $contextSize)
105
    {
106
        return \StringUtil::highlightWords($text, $searchPhrase, $contextSize);
107
    }
108
}
109