Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
85.00% |
17 / 20 |
|
0.00% |
0 / 2 |
CRAP | |
0.00% |
0 / 1 |
Processor | |
85.00% |
17 / 20 |
|
0.00% |
0 / 2 |
6.12 | |
0.00% |
0 / 1 |
findXslt | |
88.89% |
8 / 9 |
|
0.00% |
0 / 1 |
3.01 | |||
process | |
81.82% |
9 / 11 |
|
0.00% |
0 / 1 |
3.05 |
1 | <?php |
2 | |
3 | /** |
4 | * VuFind XSLT wrapper |
5 | * |
6 | * PHP version 8 |
7 | * |
8 | * Copyright (C) Villanova University 2010. |
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 XSLT |
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/ Wiki |
28 | */ |
29 | |
30 | namespace VuFind\XSLT; |
31 | |
32 | use DOMDocument; |
33 | use XSLTProcessor; |
34 | |
35 | /** |
36 | * VuFind XSLT wrapper |
37 | * |
38 | * @category VuFind |
39 | * @package XSLT |
40 | * @author Demian Katz <demian.katz@villanova.edu> |
41 | * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License |
42 | * @link https://vufind.org/wiki/ Wiki |
43 | */ |
44 | class Processor |
45 | { |
46 | /** |
47 | * Locate an XSLT file and return its full path. |
48 | * |
49 | * @param string $xslt Filename |
50 | * |
51 | * @return string |
52 | * @throws \Exception |
53 | */ |
54 | protected static function findXslt($xslt) |
55 | { |
56 | $paths = [ |
57 | LOCAL_OVERRIDE_DIR . '/xsl/', |
58 | APPLICATION_PATH . '/module/VuFind/xsl/', |
59 | APPLICATION_PATH . '/xsl/', |
60 | ]; |
61 | foreach ($paths as $path) { |
62 | if (file_exists($path . $xslt)) { |
63 | return $path . $xslt; |
64 | } |
65 | } |
66 | throw new \Exception('Cannot locate ' . $xslt); |
67 | } |
68 | |
69 | /** |
70 | * Perform an XSLT transformation and return the results. |
71 | * |
72 | * @param string $xslt Name of stylesheet (in application/xsl directory) |
73 | * @param string $xml XML to transform with stylesheet |
74 | * @param string $params Associative array of XSLT parameters |
75 | * |
76 | * @return string Transformed XML |
77 | */ |
78 | public static function process($xslt, $xml, $params = []) |
79 | { |
80 | $style = new DOMDocument(); |
81 | $style->load(static::findXslt($xslt)); |
82 | $xsl = new XSLTProcessor(); |
83 | $xsl->importStyleSheet($style); |
84 | $doc = new DOMDocument(); |
85 | $sanitizeXmlRegEx |
86 | = '[^\x{0009}\x{000a}\x{000d}\x{0020}-\x{D7FF}\x{E000}-\x{FFFD}]+'; |
87 | $cleanXml = trim(preg_replace("/$sanitizeXmlRegEx/u", ' ', $xml)); |
88 | if ($doc->loadXML($cleanXml)) { |
89 | foreach ($params as $key => $value) { |
90 | $xsl->setParameter('', $key, $value); |
91 | } |
92 | return $xsl->transformToXML($doc); |
93 | } |
94 | return ''; |
95 | } |
96 | } |