for testing and deploying your application
for finding and fixing issues
for empowering human code reviews
<?php
namespace Port\Xml;
use Port\Writer;
use Port\Writer\FlushableWriter;
/**
* Write data to XML
*
* @author Márk Sági-Kazár <[email protected]>
*/
class XmlWriter implements Writer, FlushableWriter
{
* @var \XmlWriter
protected $xmlWriter;
* @var string
protected $file;
protected $rootElement;
protected $itemElement;
protected $version;
* @var string|null
protected $encoding;
* @param string $file
public function __construct(
\XmlWriter $xmlWriter,
$file,
$rootElement = 'items',
$itemElement = 'item',
$version = '1.0',
$encoding = null
) {
$this->xmlWriter = $xmlWriter;
$this->file = $file;
$this->rootElement = $rootElement;
$this->itemElement = $itemElement;
$this->version = $version;
$this->encoding = $encoding;
}
* {@inheritdoc}
public function prepare()
$this->xmlWriter->openUri($this->file);
$this->xmlWriter->startDocument($this->version, $this->encoding);
$this->xmlWriter->startElement($this->rootElement);
public function writeItem(array $item)
$this->xmlWriter->startElement($this->itemElement);
foreach ($item as $key => $value) {
$this->xmlWriter->writeElement($key, $value);
$this->xmlWriter->endElement();
public function finish()
$this->xmlWriter->endDocument();
$this->flush();
public function flush()
$this->xmlWriter->flush();