-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
110 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the xmltv-library package. | ||
* | ||
* (c) 2020 WEBEWEB | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace WBW\Library\XMLTV\IO; | ||
|
||
use DOMDocument; | ||
use WBW\Library\XMLTV\Model\Tv; | ||
|
||
/** | ||
* Writer. | ||
* | ||
* @author webeweb <https://github.com/webeweb/> | ||
* @package WBW\Library\XMLTV\IO | ||
*/ | ||
class Writer { | ||
|
||
/** | ||
* Write XML. | ||
* | ||
* @param Tv $tv The tv. | ||
* @param string $filename The filename. | ||
* @return int Returns the number of bytes written. | ||
*/ | ||
public static function writeXml(Tv $tv, $filename) { | ||
|
||
$xml = [ | ||
'<?xml version="1.0" encoding="utf-8"?>', | ||
'<!DOCTYPE tv SYSTEM "xmltv.dtd">', | ||
$tv->xmlSerialize(), | ||
]; | ||
|
||
$document = new DOMDocument(); | ||
$document->loadXML(implode("", $xml)); | ||
@$document->schemaValidate(Reader::getDtd()); | ||
|
||
$document->formatOutput = true; | ||
|
||
return $document->save($filename); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the xmltv-library package. | ||
* | ||
* (c) 2020 WEBEWEB | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace WBW\Library\XMLTV\Tests\IO; | ||
|
||
use WBW\Library\XMLTV\IO\Reader; | ||
use WBW\Library\XMLTV\IO\Writer; | ||
use WBW\Library\XMLTV\Tests\AbstractTestCase; | ||
|
||
/** | ||
* Writer test. | ||
* | ||
* @author webeweb <https://github.com/webeweb/> | ||
* @package WBW\Library\XMLTV\Tests\IO | ||
*/ | ||
class WriterTest extends AbstractTestCase { | ||
|
||
/** | ||
* Output. | ||
* | ||
* @var string | ||
*/ | ||
private $output; | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
protected function setUp() { | ||
parent::setUp(); | ||
|
||
// Set an Output mock. | ||
$this->output = getcwd() . "/WriterTest.testWriteXML.xml"; | ||
|
||
if (true === file_exists($this->output)) { | ||
unlink($this->output); | ||
} | ||
} | ||
|
||
/** | ||
* Tests the writeXml() method. | ||
* | ||
* @return void | ||
*/ | ||
public function testWriteXml() { | ||
|
||
// Set a Tv mock. | ||
$tv = Reader::readXml($this->filename); | ||
|
||
Writer::writeXml($tv, $this->output); | ||
$this->assertFileExists($this->output); | ||
|
||
$this->assertEquals(preg_replace("/\ {4}/", " ",file_get_contents($this->filename)), file_get_contents($this->output)); | ||
} | ||
} |