Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
86.96% covered (warning)
86.96%
20 / 23
75.00% covered (warning)
75.00%
6 / 8
CRAP
0.00% covered (danger)
0.00%
0 / 1
SpecBuilder
86.96% covered (warning)
86.96%
20 / 23
75.00% covered (warning)
75.00%
6 / 8
15.50
0.00% covered (danger)
0.00%
0 / 1
 __construct
50.00% covered (danger)
50.00%
2 / 4
0.00% covered (danger)
0.00%
0 / 1
6.00
 setLine
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
2
 removeLine
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setMultiLine
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 setCombineAltLine
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 setTemplateLine
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 reorderKeys
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
4
 getArray
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3/**
4 * Specification builder for record driver data formatting view helper
5 *
6 * PHP version 8
7 *
8 * Copyright (C) Villanova University 2016.
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\RecordDataFormatter;
31
32/**
33 * Specification builder for record driver data formatting view helper
34 *
35 * @category VuFind
36 * @package  View_Helpers
37 * @author   Demian Katz <demian.katz@villanova.edu>
38 * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
39 * @link     https://vufind.org/wiki/development Wiki
40 */
41class SpecBuilder
42{
43    /**
44     * Spec
45     *
46     * @var array
47     */
48    protected $spec = [];
49
50    /**
51     * Highest position value so far.
52     *
53     * @var int
54     */
55    protected $maxPos = 0;
56
57    /**
58     * Constructor
59     *
60     * @param array $spec Existing specification lines (optional)
61     */
62    public function __construct($spec = [])
63    {
64        $this->spec = $spec;
65        foreach ($spec as $current) {
66            if (isset($current['pos']) && $current['pos'] > $this->maxPos) {
67                $this->maxPos = $current['pos'];
68            }
69        }
70    }
71
72    /**
73     * Set a generic spec line.
74     *
75     * @param string $key        Label to associate with this spec line
76     * @param string $dataMethod Method of data retrieval for rendering element
77     * @param string $renderType Type of rendering to use to generate output
78     * @param array  $options    Additional options
79     *
80     * @return void
81     */
82    public function setLine($key, $dataMethod, $renderType = null, $options = [])
83    {
84        $options['dataMethod'] = $dataMethod;
85        $options['renderType'] = $renderType;
86        if (!isset($options['pos'])) {
87            $this->maxPos += 100;
88            $options['pos'] = $this->maxPos;
89        }
90        $this->spec[$key] = $options;
91    }
92
93    /**
94     * Remove a previously set generic spec line.
95     *
96     * @param string $key Label associated with this spec line
97     *
98     * @return void
99     */
100    public function removeLine($key)
101    {
102        unset($this->spec[$key]);
103    }
104
105    /**
106     * Construct a multi-function template spec line.
107     *
108     * @param string   $key        Label to associate with this spec line
109     * @param string   $dataMethod Method of data retrieval for rendering element
110     * @param callable $callback   Callback function for multi-processing
111     * @param array    $options    Additional options
112     *
113     * @return void
114     */
115    public function setMultiLine($key, $dataMethod, $callback, $options = [])
116    {
117        $options['multiFunction'] = $callback;
118        $this->setLine($key, $dataMethod, 'Multi', $options);
119    }
120
121    /**
122     * Construct a combine alt template spec line.
123     *
124     * @param string $key        Label to associate with this spec line
125     * @param string $dataMethod Method of data retrieval for rendering element
126     * @param array  $options    Additional options
127     *
128     * @return void
129     */
130    public function setCombineAltLine($key, $dataMethod, $options = [])
131    {
132        $this->setLine($key, $dataMethod, 'CombineAlt', $options);
133    }
134
135    /**
136     * Construct a record driver template spec line.
137     *
138     * @param string $key        Label to associate with this spec line
139     * @param string $dataMethod Method of data retrieval for rendering element
140     * @param string $template   Record driver template to render with data
141     * @param array  $options    Additional options
142     *
143     * @return void
144     */
145    public function setTemplateLine($key, $dataMethod, $template, $options = [])
146    {
147        $options['template'] = $template;
148        $this->setLine($key, $dataMethod, 'RecordDriverTemplate', $options);
149    }
150
151    /**
152     * Reorder the specs to match the provided array of keys.
153     *
154     * @param array $orderedKeys Keys in the desired order
155     * @param int   $defaultPos  Position to use for elements not included in
156     * $orderedKeys (null to put unrecognized items at end of list).
157     *
158     * @return void
159     */
160    public function reorderKeys($orderedKeys, $defaultPos = null)
161    {
162        $lookup = array_flip($orderedKeys);
163        if (null === $defaultPos) {
164            $defaultPos = (max($lookup) + 2) * 100;
165        }
166        foreach (array_keys($this->spec) as $key) {
167            $this->spec[$key]['pos'] = isset($lookup[$key])
168                ? ($lookup[$key] + 1) * 100 : $defaultPos;
169        }
170    }
171
172    /**
173     * Get the spec.
174     *
175     * @return array
176     */
177    public function getArray()
178    {
179        return $this->spec;
180    }
181}