Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
97.56% covered (success)
97.56%
40 / 41
75.00% covered (warning)
75.00%
3 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
PrintArrayHtml
97.56% covered (success)
97.56%
40 / 41
75.00% covered (warning)
75.00%
3 / 4
28
0.00% covered (danger)
0.00%
0 / 1
 __invoke
100.00% covered (success)
100.00%
25 / 25
100.00% covered (success)
100.00%
1 / 1
17
 isArrayList
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
4
 isArrayFlat
83.33% covered (warning)
83.33%
5 / 6
0.00% covered (danger)
0.00%
0 / 1
4.07
 isSingleKeyList
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
3
1<?php
2
3/**
4 * View helper to print an array formatted for HTML display.
5 *
6 * PHP version 8
7 *
8 * Copyright (C) Michigan State University 2023.
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   Nathan Collins <colli372@msu.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
34use function count;
35use function is_array;
36
37/**
38 * View helper to print an array formatted for HTML display.
39 *
40 * @category VuFind
41 * @package  View_Helpers
42 * @author   Nathan Collins <colli372@msu.edu>
43 * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
44 * @link     https://vufind.org/wiki/development Wiki
45 */
46class PrintArrayHtml extends AbstractHelper
47{
48    /**
49     * Print an array formatted for HTML display.
50     * Function uses recursion to achieve desired results, so entry can be
51     * either an array or a value to display.
52     *
53     * @param array|string $entry       An array or string to output
54     * @param int          $indentLevel How many spaces to indent output
55     * @param bool         $indentFirst Should first item in array be indented
56     *
57     * @return string                   The formatted HTML for output
58     */
59    public function __invoke($entry, $indentLevel = 0, $indentFirst = true)
60    {
61        $makeTag = $this->getView()->plugin('makeTag');
62        $html = '';
63        if (is_array($entry)) {
64            $isList = $this->isArrayList($entry);
65            $isFlat = $this->isArrayFlat($entry);
66            foreach ($entry as $key => $value) {
67                if ($indentFirst || $key != array_key_first($entry)) {
68                    $html .= str_repeat('&ensp;', $indentLevel);
69                }
70                $valueIsSingleKeyList = $this->isSingleKeyList($value);
71
72                $nextIndentLevel = $indentLevel;
73                // Indent first line unless we're continuing from hyphen
74                $nextIndentFirst = ($isFlat || !$isList || !is_array($value))
75                                   && !$valueIsSingleKeyList;
76                if (!$isFlat || !$isList) {
77                    // Increase indent if entering new array
78                    $nextIndentLevel = is_array($value) ? $indentLevel + 2 : 0;
79                }
80                if (!$isFlat && $isList && !$valueIsSingleKeyList) {
81                    // Integer keyed arrays use a hyphen list unless they're flat
82                    $html .= '&ndash;&ensp;';
83                } elseif (!$isList) {
84                    $html .= $makeTag('span', $key . ':', ['class' => 'term'])
85                             . (
86                                 (is_array($value) && !$valueIsSingleKeyList)
87                                 ? "<br>\n" : ' '
88                             );
89                }
90
91                $html .= $this->__invoke($value, $nextIndentLevel, $nextIndentFirst);
92            }
93        } else {
94            $html = $makeTag('span', $entry, ['class' => 'detail']) . "<br>\n";
95        }
96        return $html;
97    }
98
99    /**
100     * Check is a variable is and array and all keys are sequential
101     * integers starting from index 0.
102     * TODO This function can be replaced by array_is_list() in PHP8
103     *
104     * @param mixed $var A variable to perform the check on.
105     *
106     * @return bool
107     */
108    protected function isArrayList($var)
109    {
110        if (!is_array($var)) {
111            return false;
112        }
113        $i = 0;
114        foreach ($var as $key => $_) {
115            if ($key !== $i++) {
116                return false;
117            }
118        }
119        return true;
120    }
121
122    /**
123     * Check if variable is an array and has no arrays as values.
124     *
125     * @param mixed $var A variable to perform the check on.
126     *
127     * @return bool
128     */
129    protected function isArrayFlat($var)
130    {
131        if (!is_array($var)) {
132            return false;
133        }
134        foreach ($var as $val) {
135            if (is_array($val)) {
136                return false;
137            }
138        }
139        return true;
140    }
141
142    /**
143     * Check if variable is an integer keyed array of size 1
144     * whose single value is not an array.
145     *
146     * @param mixed $var A variable to perform the check on.
147     *
148     * @return bool
149     */
150    protected function isSingleKeyList($var)
151    {
152        return $this->isArrayList($var)
153               && $this->isArrayFlat($var)
154               && count($var) == 1;
155    }
156}