Skip to content

Commit f9c77ce

Browse files
committed
Merge branch 'main' of github.com:mintyphp/forms
2 parents a7981cc + f78be88 commit f9c77ce

File tree

2 files changed

+30
-30
lines changed

2 files changed

+30
-30
lines changed

src/Form.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff 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
}

src/HtmlElement.php

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff 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
}

0 commit comments

Comments
 (0)