File tree Expand file tree Collapse file tree 2 files changed +30
-30
lines changed
Expand file tree Collapse file tree 2 files changed +30
-30
lines changed Original file line number Diff line number Diff line change @@ -210,4 +210,34 @@ public function renderDom(\DOMDocument $doc): \DOMElement
210210 }
211211 return $ formElement ;
212212 }
213+
214+ public function toString (bool $ withRoot = true , bool $ asHtml = true ): string
215+ {
216+ // save the DOMElement to a string
217+ $ domDocument = new \DOMDocument ('1.0 ' , 'UTF-8 ' );
218+ $ form = $ this ->renderDom ($ domDocument );
219+ $ domDocument ->appendChild ($ form );
220+ if ($ asHtml ) {
221+ // save the HTML to a string
222+ $ str = $ domDocument ->saveHTML ($ form );
223+ } else {
224+ // format the output as XML (for testing purposes)
225+ $ domDocument ->formatOutput = true ;
226+ $ str = $ domDocument ->saveXML ($ form );
227+ }
228+ if (!$ str ) {
229+ throw new \RuntimeException ('Failed to save XML ' );
230+ }
231+ if (!$ withRoot ) {
232+ $ str = str_replace ("\n " , "\n" , $ str );
233+ $ str = preg_replace ('/^< ' . $ this ->tag . '[^>]*>/ ' , '' , $ str );
234+ $ str = preg_replace ('/<\/ ' . $ this ->tag . '>$/ ' , '' , $ str );
235+ }
236+ return trim ($ str );
237+ }
238+
239+ public function render (bool $ withRoot = true ): void
240+ {
241+ echo $ this ->toString ($ withRoot );
242+ }
213243}
Original file line number Diff line number Diff line change @@ -105,34 +105,4 @@ protected function renderElement(\DOMDocument $doc): \DOMElement
105105 }
106106 return $ element ;
107107 }
108-
109- public function toString (bool $ withRoot = true , bool $ asHtml = true ): string
110- {
111- // save the DOMElement to a string
112- $ domDocument = new \DOMDocument ('1.0 ' , 'UTF-8 ' );
113- $ form = $ this ->renderDom ($ domDocument );
114- $ domDocument ->appendChild ($ form );
115- if ($ asHtml ) {
116- // save the HTML to a string
117- $ str = $ domDocument ->saveHTML ($ form );
118- } else {
119- // format the output as XML (for testing purposes)
120- $ domDocument ->formatOutput = true ;
121- $ str = $ domDocument ->saveXML ($ form );
122- }
123- if (!$ str ) {
124- throw new \RuntimeException ('Failed to save XML ' );
125- }
126- if (!$ withRoot ) {
127- $ str = str_replace ("\n " , "\n" , $ str );
128- $ str = preg_replace ('/^< ' . $ this ->tag . '[^>]*>/ ' , '' , $ str );
129- $ str = preg_replace ('/<\/ ' . $ this ->tag . '>$/ ' , '' , $ str );
130- }
131- return trim ($ str );
132- }
133-
134- public function render (bool $ withRoot = true ): void
135- {
136- echo $ this ->toString ($ withRoot );
137- }
138108}
You can’t perform that action at this time.
0 commit comments