Skip to content

Commit 9fddf12

Browse files
committed
Updated htmldoc::save() to return the compiled code instead of true if saved, as it returns the code if no file is specified.
Updated tests.
1 parent 837005e commit 9fddf12

File tree

2 files changed

+6
-10
lines changed

2 files changed

+6
-10
lines changed

src/htmldoc.php

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -666,8 +666,9 @@ public function remove(string $selector = null) : htmldoc {
666666
/**
667667
* Compile the document as an HTML string and save it to the specified location
668668
*
669+
* @param string|null $file The file location to save the document to, or null to just return the compiled code
669670
* @param array $options An array indicating output options, this is merged with htmldoc::$output
670-
* @return string|bool The compiled HTML, or false on error
671+
* @return string|bool The compiled HTML, or false if the file could not be saved
671672
*/
672673
public function save(string $file = null, array $options = []) {
673674

@@ -686,17 +687,12 @@ public function save(string $file = null, array $options = []) {
686687
$html = (string) \mb_convert_encoding($html, $options['charset']);
687688
}
688689

689-
// send back as string
690-
if (!$file) {
691-
return $html;
692-
693690
// save file
694-
} elseif (\file_put_contents($file, $html) === false) {
691+
if ($file && \file_put_contents($file, $html) === false) {
695692
\trigger_error('File could not be written', E_USER_WARNING);
696-
} else {
697-
return true;
693+
return false;
698694
}
699-
return false;
695+
return $html;
700696
}
701697

702698
/**

tests/htmldocTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ public function testCanSaveDocument() {
152152
if (file_exists($file)) {
153153
unlink($file);
154154
}
155-
$this->assertEquals(true, $doc->save($file), 'Can save document');
155+
$this->assertEquals($doc->html(), $doc->save($file), 'Can save document');
156156
$this->assertEquals(true, file_exists($file), 'Saved document ecists');
157157
$this->assertEquals('<div>Hello world</div>', file_get_contents($file), 'Saved document has the correct content');
158158
unlink($file);

0 commit comments

Comments
 (0)