Skip to content

Commit

Permalink
Add Writer
Browse files Browse the repository at this point in the history
  • Loading branch information
webeweb committed Mar 11, 2020
1 parent 093b062 commit 8dbc2cf
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 0 deletions.
48 changes: 48 additions & 0 deletions src/IO/Writer.php
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);
}
}
62 changes: 62 additions & 0 deletions tests/IO/WriterTest.php
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));
}
}

0 comments on commit 8dbc2cf

Please sign in to comment.