Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 21
0.00% covered (danger)
0.00%
0 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
Json
0.00% covered (danger)
0.00%
0 / 21
0.00% covered (danger)
0.00%
0 / 3
90
0.00% covered (danger)
0.00%
0 / 1
 getData
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 formatNode
0.00% covered (danger)
0.00%
0 / 10
0.00% covered (danger)
0.00%
0 / 1
20
 mapChildren
0.00% covered (danger)
0.00%
0 / 10
0.00% covered (danger)
0.00%
0 / 1
20
1<?php
2
3/**
4 * Hierarchy Tree Data Formatter (JSON)
5 *
6 * PHP version 8
7 *
8 * Copyright (C) Villanova University 2015.
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  HierarchyTree_DataFormatter
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:plugins:hierarchy_components Wiki
28 */
29
30namespace VuFind\Hierarchy\TreeDataFormatter;
31
32/**
33 * Hierarchy Tree Data Formatter (JSON)
34 *
35 * @category VuFind
36 * @package  HierarchyTree_DataFormatter
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:plugins:hierarchy_components Wiki
40 */
41class Json extends AbstractBase
42{
43    /**
44     * Get the formatted metadata.
45     *
46     * @return string
47     */
48    public function getData()
49    {
50        return json_encode($this->formatNode($this->topNode));
51    }
52
53    /**
54     * Get Solr Children for JSON
55     *
56     * @param object $record   Solr record to format
57     * @param string $parentID The starting point for the current recursion
58     * (equivalent to Solr field hierarchy_parent_id)
59     *
60     * @return string
61     */
62    protected function formatNode($record, $parentID = null)
63    {
64        $raw = [
65            'id' => $record->id,
66            'type' => $this->isCollection($record) ? 'collection' : 'record',
67            'title' => $this->pickTitle($record, $parentID),
68        ];
69
70        if (isset($this->childMap[$record->id])) {
71            $children = $this->mapChildren($record->id);
72            if (!empty($children)) {
73                $raw['children'] = $children;
74            }
75        }
76
77        return (object)$raw;
78    }
79
80    /**
81     * Get Solr Children for JSON
82     *
83     * @param string $parentID The starting point for the current recursion
84     * (equivalent to Solr field hierarchy_parent_id)
85     *
86     * @return string
87     */
88    protected function mapChildren($parentID)
89    {
90        $json = [];
91        foreach ($this->childMap[$parentID] as $current) {
92            ++$this->count;
93
94            $childNode = $this->formatNode($current, $parentID);
95
96            // If we're in sorting mode, we need to create key-value arrays;
97            // otherwise, we can just collect flat values.
98            if ($this->sort) {
99                $positions = $this->getHierarchyPositionsInParents($current);
100                $sequence = $positions[$parentID] ?? 0;
101                $json[] = [$sequence, $childNode];
102            } else {
103                $json[] = $childNode;
104            }
105        }
106
107        return $this->sort ? $this->sortNodes($json) : $json;
108    }
109}