Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
5 / 5
CRAP
100.00% covered (success)
100.00%
1 / 1
SchemaOrg
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
5 / 5
7
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getAttributes
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
2
 getTag
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
2
 getLink
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getMeta
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3/**
4 * View helper for injecting schema.org metadata
5 *
6 * PHP version 8
7 *
8 * Copyright (C) Villanova University 2024.
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   Ere Maijala <ere.maijala@helsinki.fi>
26 * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
27 * @link     http://vufind.org/wiki/vufind2:developer_manual Wiki
28 */
29
30namespace VuFind\View\Helper\Root;
31
32use Laminas\View\Helper\HtmlAttributes;
33
34/**
35 * View helper for injecting schema.org metadata
36 *
37 * @category VuFind
38 * @package  View_Helpers
39 * @author   Ere Maijala <ere.maijala@helsinki.fi>
40 * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
41 * @link     http://vufind.org/wiki/vufind2:developer_manual Wiki
42 */
43class SchemaOrg extends \Laminas\View\Helper\AbstractHelper
44{
45    /**
46     * Constructor
47     *
48     * @param HtmlAttributes $htmlAttributes HtmlAttributes view helper
49     * @param bool           $enabled        Is schema.org metadata enabled?
50     */
51    public function __construct(protected HtmlAttributes $htmlAttributes, protected bool $enabled = true)
52    {
53    }
54
55    /**
56     * Format schema.org attributes (if enabled).
57     *
58     * @param array $attributes HTML attributes (in key/value format)
59     *
60     * @return string
61     */
62    public function getAttributes(array $attributes): string
63    {
64        return $this->enabled ? ($this->htmlAttributes)($attributes) : '';
65    }
66
67    /**
68     * Get a schema.org tag (if enabled). Note that this only generates an open (or void)
69     * tag; if a close tag is required, you will need to add it yourself separately.
70     *
71     * @param string $tag        Tag name
72     * @param array  $attributes Tag attributes
73     *
74     * @return string
75     */
76    public function getTag(string $tag, array $attributes): string
77    {
78        $attributes = $this->getAttributes($attributes);
79        return $attributes ? "<$tag$attributes>" : '';
80    }
81
82    /**
83     * Create a schema.org link tag (if enabled).
84     *
85     * @param string $href       Link target
86     * @param string $property   Property attribute
87     * @param array  $attributes Additional attributes (optional)
88     *
89     * @return string
90     */
91    public function getLink(string $href, string $property, array $attributes = []): string
92    {
93        return $this->getTag('link', compact('href', 'property') + $attributes);
94    }
95
96    /**
97     * Create a schema.org meta tag (if enabled).
98     *
99     * @param string $property   Property name
100     * @param string $content    Property value
101     * @param array  $attributes Additional attributes (optional)
102     *
103     * @return string
104     */
105    public function getMeta(string $property, string $content, array $attributes = []): string
106    {
107        return $this->getTag('meta', compact('property', 'content') + $attributes);
108    }
109}