-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathPHXSLT.class.php
More file actions
executable file
·156 lines (134 loc) · 4.46 KB
/
Copy pathPHXSLT.class.php
File metadata and controls
executable file
·156 lines (134 loc) · 4.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
<?php
/**
* @title PH XSLT
* @desc XSLT PHP template engine.
*
* @author Pierre-Henry Soria <ph7software@gmail.com>
* @copyright (c) 2012, Pierre-Henry Soria. All Rights Reserved.
* @license GNU General Public License.
* @package PH7 / Framework / Layout / Tpl / Engine / PHXSLT
* @version 1.1
*/
namespace PH7\Framework\Layout\Tpl\Engine\PHXSLT;
class PHXSLT
{
const ROOT_NAMESPACE = 'template';
private
$_oXml,
$_oXsl,
$_oXslProcessor,
$_sOutput,
$_oRoot,
$_sFile,
$_bPhpFunc = true;
/**
* Constructor.
*
* @param string $sFile The XSL file.
*/
public function __construct($sFile)
{
// Creating objects
$this->_oXml = new \DOMDocument('1.0', 'UTF-8');
$this->_oXsl = new \DOMDocument;
$this->_oXslProcessor = new \XsltProcessor;
$this->_sFile = $sFile;
$this->load();
// Creation of the XML root node mandatory
$this->_oRoot = $this->_oXml->createElement(static::ROOT_NAMESPACE);
// Insertion of this node in the tree view of the XML file
$this->_oXml->appendChild($this->_oRoot);
}
/**
* Enable or disable the PHP functions in the XSTL template.
*
* @param boolean $bEnable Default TRUE
* @return object this
*/
public function enablePhpFunctions($bEnable = true)
{
$this->_bPhpFunc = (bool) $bEnable;
return $this;
}
/**
* Generate XML Node.
*
* @param mixed $aData array
* @param string $sNamespace Optional. Default ''
* @return object this
* @throws \PH7\Framework\Layout\Tpl\Engine\PHXSLT\Exception If the data value is not an array.
*/
public function generateXMLNode($aData, $sNamespace = '')
{
if(!is_array($aData)) throw new Exception('The data value must be of type array!');
$sRoot = $this->_oXml->createElement($sNamespace);
$this->_oRoot->appendChild($sRoot);
foreach($aData as $sKey => $sValue) {
$sNode = $this->_oXml->createElement($sKey);
$sRoot->appendChild($sNode);
$sContent = $this->_oXml->createTextNode(utf8_encode($sValue));
$sNode->appendChild($sContent);
}
return $this;
}
/**
* Output.
*
* @return void
* @throws \PH7\Framework\Layout\Tpl\Engine\PHXSLT\Exception If the XSL file contains syntax errors.
*/
public function render()
{
if(!@$this->_sOutput = $this->_oXslProcessor->transformToXML($this->_oXml))
throw new Exception('Transformation syntax!');
header('Content-Type: text/xml; charset=utf-8');
echo $this->_sOutput;
}
/**
* Set variables.
*
* @param mixed $mKey (array | string)
* @param string $sValue Optional only if the variables are passed through an array. Default ''
* @param string $sNamespace Optional. Default ''
* @return object this
*/
public function setParam($mKey, $sValue = '', $sNamespace = '')
{
if(is_array($mKey)) {
foreach($mKey as $sKey => $sVal)
$this->setParam($sKey, $sVal, $sNamespace); // Recursive method
} else {
$this->_oXslProcessor->setParameter($sNamespace, $mKey, $sValue);
}
return $this;
}
/**
* Escape function, uses the PHP native htmlspecialchars but improves.
*
* @param string $sText
* @param boolean $bStrip If true, the text will be passed through the strip_tags function PHP.
* @return string The text to HTML entities.
*/
public static function escape($sText, $bStrip = false)
{
return ($bStrip) ? strip_tags($sText) : htmlspecialchars($sText, ENT_QUOTES);
}
/**
* Load XSL file.
*
* @return object this
* @internal We use realpath() function because forward slashes can cause significant performance degradation on Windows OS.
* @throws \PH7\Framework\Layout\Tpl\Engine\PHXSLT\Exception If the XSL file does not exist.
*/
protected function load()
{
$sPath = realpath($this->_sFile);
if(!@$this->_oXsl->load($sPath)) {
throw new Exception('While loading file: "' . static::escape($sPath) . '"');
} else {
if($this->_bPhpFunc) $this->_oXslProcessor->registerPHPFunctions();
$this->_oXslProcessor->importStylesheet($this->_oXsl);
}
return $this;
}
}