Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
88.89% covered (warning)
88.89%
8 / 9
80.00% covered (warning)
80.00%
4 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
AbstractJsStrings
88.89% covered (warning)
88.89%
8 / 9
80.00% covered (warning)
80.00%
4 / 5
7.07
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 mapValue
n/a
0 / 0
n/a
0 / 0
0
 addStrings
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
2
 getJSONFromArray
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
 getJSON
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getScript
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2
3/**
4 * AbstractJsStrings helper for passing transformed text to Javascript
5 *
6 * PHP version 8
7 *
8 * Copyright (C) Villanova University 2021.
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2,
12 * as published by the Free Software Foundation.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
22 *
23 * @category VuFind
24 * @package  View_Helpers
25 * @author   Demian Katz <demian.katz@villanova.edu>
26 * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
27 * @link     https://vufind.org/wiki/development Wiki
28 */
29
30namespace VuFind\View\Helper\Root;
31
32use Laminas\View\Helper\AbstractHelper;
33
34/**
35 * AbstractJsStrings helper for passing transformed text to Javascript
36 *
37 * @category VuFind
38 * @package  View_Helpers
39 * @author   Demian Katz <demian.katz@villanova.edu>
40 * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
41 * @link     https://vufind.org/wiki/development Wiki
42 */
43abstract class AbstractJsStrings extends AbstractHelper
44{
45    /**
46     * Variable name to store values
47     *
48     * @var string
49     */
50    protected $varName;
51
52    /**
53     * Strings to convey (key = js key, value = value to map)
54     *
55     * @var array
56     */
57    protected $strings = [];
58
59    /**
60     * Constructor
61     *
62     * @param string $varName Variable name to store values
63     */
64    public function __construct($varName = 'vufindString')
65    {
66        $this->varName = $varName;
67    }
68
69    /**
70     * Transform strings before JSON encoding
71     *
72     * @param string|array $str String to transform
73     * @param string       $key JSON object key
74     *
75     * @return string
76     */
77    abstract protected function mapValue($str, string $key): string;
78
79    /**
80     * Add strings to the internal array.
81     *
82     * @param array $new Strings to add
83     *
84     * @return void
85     */
86    public function addStrings($new)
87    {
88        foreach ($new as $k => $v) {
89            $this->strings[$k] = $v;
90        }
91    }
92
93    /**
94     * Generate JSON from an array
95     *
96     * @param array $strings Strings to convey (key = js key, value = value to map)
97     *
98     * @return string
99     */
100    public function getJSONFromArray(array $strings): string
101    {
102        $ret = [];
103        foreach ($strings as $key => $str) {
104            $ret[$key] = $this->mapValue($str, $key);
105        }
106        return json_encode($ret);
107    }
108
109    /**
110     * Generate JSON from the internal strings
111     *
112     * @return string
113     */
114    public function getJSON()
115    {
116        return $this->getJSONFromArray($this->strings);
117    }
118
119    /**
120     * Assign JSON to a variable.
121     *
122     * @return string
123     */
124    public function getScript()
125    {
126        return $this->varName . ' = ' . $this->getJSON() . ';';
127    }
128}