Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
16 / 16
100.00% covered (success)
100.00%
3 / 3
CRAP
100.00% covered (success)
100.00%
1 / 1
Metadata
100.00% covered (success)
100.00%
16 / 16
100.00% covered (success)
100.00%
3 / 3
9
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 getVocabularies
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
4
 generateMetatags
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
4
1<?php
2
3/**
4 * Metadata view helper
5 *
6 * PHP version 8
7 *
8 * Copyright (C) University of Tübingen 2019.
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  Metadata_Vocabularies
25 * @author   Mario Trojan <mario.trojan@uni-tuebingen.de>
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
32/**
33 * Metadata view helper
34 *
35 * @category VuFind
36 * @package  Metadata_Vocabularies
37 * @author   Mario Trojan <mario.trojan@uni-tuebingen.de>
38 * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
39 * @link     https://vufind.org/wiki/development Wiki
40 */
41class Metadata extends \Laminas\View\Helper\AbstractHelper
42{
43    /**
44     * Metadata configuration entries
45     *
46     * @var \Laminas\Config\Config
47     */
48    protected $config;
49
50    /**
51     * Laminas meta helper, used to embed html tags in the generated page
52     *
53     * @var \Laminas\View\Helper\HeadMeta
54     */
55    protected $metaHelper;
56
57    /**
58     * Plugin Manager for vocabularies
59     *
60     * @var \VuFind\MetadataVocabulary\PluginManager
61     */
62    protected $pluginManager;
63
64    /**
65     * Constructor
66     *
67     * @param \VuFind\MetadataVocabulary\PluginManager $pm         Plugin manager
68     * @param \Laminas\Config\Config                   $config     Configuration
69     * @param \Laminas\View\Helper\HeadMeta            $metaHelper Head meta helper
70     */
71    public function __construct(
72        \VuFind\MetadataVocabulary\PluginManager $pm,
73        \Laminas\Config\Config $config,
74        \Laminas\View\Helper\HeadMeta $metaHelper
75    ) {
76        $this->pluginManager = $pm;
77        $this->config = $config;
78        $this->metaHelper = $metaHelper;
79    }
80
81    /**
82     * Get all active vocabularies for the current record.
83     *
84     * @param \VuFind\RecordDriver\AbstractBase $driver Record driver
85     *
86     * @return array
87     */
88    protected function getVocabularies(\VuFind\RecordDriver\AbstractBase $driver)
89    {
90        $recordDriverConfigs = isset($this->config->Vocabularies)
91            ? $this->config->Vocabularies->toArray() : [];
92        $retVal = [];
93        foreach ($recordDriverConfigs as $className => $vocabs) {
94            if ($driver instanceof $className) {
95                $retVal = array_merge($retVal, $vocabs);
96            }
97        }
98        return array_unique($retVal);
99    }
100
101    /**
102     * Generate all metatags for RecordDriver and add to page
103     *
104     * Decide which Plugins to load for the given RecordDriver
105     * dependant on configuration. (only by class name,
106     * namespace will not be considered)
107     *
108     * @param \VuFind\RecordDriver\AbstractBase $driver Record driver
109     *
110     * @return void
111     */
112    public function generateMetatags(\VuFind\RecordDriver\AbstractBase $driver)
113    {
114        foreach ($this->getVocabularies($driver) as $metatagType) {
115            $vocabulary = $this->pluginManager->get($metatagType);
116            $mappedFields = $vocabulary->getMappedData($driver);
117            foreach ($mappedFields as $field => $values) {
118                foreach ($values as $value) {
119                    $this->metaHelper->appendName($field, $value);
120                }
121            }
122        }
123    }
124}