1 | <?php |
||||
2 | |||||
3 | /** |
||||
4 | * @package toolkit |
||||
5 | */ |
||||
6 | /** |
||||
7 | * XSLTPage extends the Page class to provide an object representation |
||||
8 | * of a Page that will be generated using XSLT. |
||||
9 | */ |
||||
10 | |||||
11 | class XSLTPage extends Page |
||||
12 | { |
||||
13 | /** |
||||
14 | * An instance of the XsltProcess class |
||||
15 | * @var XsltProcess |
||||
16 | */ |
||||
17 | public $Proc; |
||||
18 | |||||
19 | /** |
||||
20 | * The XML to be transformed |
||||
21 | * @since Symphony 2.4 this variable may be a string or an XMLElement |
||||
22 | * @var string|XMLElement |
||||
23 | */ |
||||
24 | protected $_xml; |
||||
25 | |||||
26 | /** |
||||
27 | * The XSL to apply to the `$this->_xml`. |
||||
28 | * @var string |
||||
29 | */ |
||||
30 | protected $_xsl; |
||||
31 | |||||
32 | /** |
||||
33 | * An array of all the parameters to be made available during the XSLT |
||||
34 | * transform |
||||
35 | * @var array |
||||
36 | */ |
||||
37 | protected $_param = array(); |
||||
38 | |||||
39 | /** |
||||
40 | * An array of the PHP functions to be made available during the XSLT |
||||
41 | * transform |
||||
42 | * @var array |
||||
43 | */ |
||||
44 | protected $_registered_php_functions = array(); |
||||
45 | |||||
46 | /** |
||||
47 | * The constructor for the `XSLTPage` ensures that an `XSLTProcessor` |
||||
48 | * is available, and then sets an instance of it to `$this->Proc`, otherwise |
||||
49 | * it will throw a `SymphonyErrorPage` exception. |
||||
50 | */ |
||||
51 | public function __construct() |
||||
52 | { |
||||
53 | if (!XsltProcess::isXSLTProcessorAvailable()) { |
||||
54 | Symphony::Engine()->throwCustomError(__('No suitable XSLT processor was found.')); |
||||
55 | } |
||||
56 | |||||
57 | $this->Proc = new XsltProcess; |
||||
58 | } |
||||
59 | |||||
60 | /** |
||||
61 | * Setter for `$this->_xml`, can optionally load the XML from a file. |
||||
62 | * |
||||
63 | * @param string|XMLElement $xml |
||||
64 | * The XML for this XSLT page |
||||
65 | * @param boolean $isFile |
||||
66 | * If set to true, the XML will be loaded from a file. It is false by default |
||||
67 | */ |
||||
68 | public function setXML($xml, $isFile = false) |
||||
0 ignored issues
–
show
Coding Style
introduced
by
![]() |
|||||
69 | { |
||||
70 | $this->_xml = ($isFile ? file_get_contents($xml) : $xml); |
||||
0 ignored issues
–
show
It seems like
$xml can also be of type XMLElement ; however, parameter $filename of file_get_contents() does only seem to accept string , maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
71 | } |
||||
72 | |||||
73 | /** |
||||
74 | * Accessor for the XML of this page |
||||
75 | * |
||||
76 | * @return string|XMLElement |
||||
77 | */ |
||||
78 | public function getXML() |
||||
79 | { |
||||
80 | return $this->_xml; |
||||
81 | } |
||||
82 | |||||
83 | /** |
||||
84 | * Setter for `$this->_xsl`, can optionally load the XSLT from a file. |
||||
85 | * |
||||
86 | * @param string $xsl |
||||
87 | * The XSLT for this XSLT page |
||||
88 | * @param boolean $isFile |
||||
89 | * If set to true, the XSLT will be loaded from a file. It is false by default |
||||
90 | */ |
||||
91 | public function setXSL($xsl, $isFile = false) |
||||
0 ignored issues
–
show
|
|||||
92 | { |
||||
93 | $this->_xsl = ($isFile ? file_get_contents($xsl) : $xsl); |
||||
0 ignored issues
–
show
|
|||||
94 | } |
||||
95 | |||||
96 | /** |
||||
97 | * Accessor for the XSL of this page |
||||
98 | * |
||||
99 | * @return string |
||||
100 | */ |
||||
101 | public function getXSL() |
||||
102 | { |
||||
103 | return $this->_xsl; |
||||
104 | } |
||||
105 | |||||
106 | /** |
||||
107 | * Sets the parameters that will output with the resulting page |
||||
108 | * and be accessible in the XSLT. This function translates all ' into |
||||
109 | * `'`, with the tradeoff being that a <xsl:value-of select='$param' /> |
||||
110 | * that has a ' will output `'` but the benefit that ' and " can be |
||||
111 | * in the params |
||||
112 | * |
||||
113 | * @link http://www.php.net/manual/en/xsltprocessor.setparameter.php#81077 |
||||
114 | * @param array $param |
||||
115 | * An associative array of params for this page |
||||
116 | */ |
||||
117 | public function setRuntimeParam($param) |
||||
118 | { |
||||
119 | $this->_param = str_replace("'", "'", $param); |
||||
0 ignored issues
–
show
Coding Style
Comprehensibility
introduced
by
The string literal
' does not require double quotes, as per coding-style, please use single quotes.
PHP provides two ways to mark string literals. Either with single quotes String literals in single quotes on the other hand are evaluated very literally and the only two
characters that needs escaping in the literal are the single quote itself ( Double quoted string literals may contain other variables or more complex escape sequences. <?php
$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";
print $doubleQuoted;
will print an indented: If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear. For more information on PHP string literals and available escape sequences see the PHP core documentation. ![]() |
|||||
120 | } |
||||
121 | |||||
122 | /** |
||||
123 | * Returns an iterator of errors from the `XsltProcess`. Use this function |
||||
124 | * inside a loop to get all the errors that occurring when transforming |
||||
125 | * `$this->_xml` with `$this->_xsl`. |
||||
126 | * |
||||
127 | * @return array |
||||
128 | * An associative array containing the errors details from the |
||||
129 | * `XsltProcessor` |
||||
130 | */ |
||||
131 | public function getError() |
||||
132 | { |
||||
133 | return $this->Proc->getError(); |
||||
134 | } |
||||
135 | |||||
136 | /** |
||||
137 | * Allows the registration of PHP functions to be used on the Frontend |
||||
138 | * by passing the function name or an array of function names |
||||
139 | * |
||||
140 | * @param mixed $function |
||||
141 | * Either an array of function names, or just the function name as a |
||||
142 | * string |
||||
143 | */ |
||||
144 | public function registerPHPFunction($function) |
||||
145 | { |
||||
146 | if (is_array($function)) { |
||||
147 | $this->_registered_php_functions = array_unique( |
||||
148 | array_merge($this->_registered_php_functions, $function) |
||||
149 | ); |
||||
150 | } else { |
||||
151 | $this->_registered_php_functions[] = $function; |
||||
152 | } |
||||
153 | } |
||||
154 | |||||
155 | /** |
||||
156 | * The generate function calls on the `XsltProcess` to transform the |
||||
157 | * XML with the given XSLT passing any parameters or functions |
||||
158 | * If no errors occur, the parent generate function is called to add |
||||
159 | * the page headers and a string containing the transformed result |
||||
160 | * is result. |
||||
161 | * |
||||
162 | * @param null $page |
||||
0 ignored issues
–
show
|
|||||
163 | * @return string |
||||
164 | */ |
||||
165 | public function generate($page = null) |
||||
0 ignored issues
–
show
|
|||||
166 | { |
||||
167 | $result = $this->Proc->process($this->_xml, $this->_xsl, $this->_param, $this->_registered_php_functions); |
||||
0 ignored issues
–
show
It seems like
$this->_xml can also be of type XMLElement ; however, parameter $xml of XsltProcess::process() does only seem to accept string , maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
168 | |||||
169 | if ($this->Proc->isErrors()) { |
||||
170 | $this->setHttpStatus(Page::HTTP_STATUS_ERROR); |
||||
171 | return false; |
||||
0 ignored issues
–
show
|
|||||
172 | } |
||||
173 | |||||
174 | parent::generate($page); |
||||
175 | |||||
176 | return $result; |
||||
0 ignored issues
–
show
The expression
return $result could also return false which is incompatible with the documented return type string . Did you maybe forget to handle an error condition?
If the returned type also contains false, it is an indicator that maybe an error condition leading to the specific return statement remains unhandled. ![]() |
|||||
177 | } |
||||
178 | } |
||||
179 |