3 * Zend Framework (http://framework.zend.com/)
5 * @link http://github.com/zendframework/zf2 for the canonical source repository
6 * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
7 * @license http://framework.zend.com/license/new-bsd New BSD License
10 namespace Zend\XmlRpc\Generator
;
13 * Abstract XML generator adapter
15 abstract class AbstractGenerator
implements GeneratorInterface
25 * Construct new instance of the generator
27 * @param string $encoding XML encoding, default UTF-8
29 public function __construct($encoding = 'UTF-8')
31 $this->setEncoding($encoding);
36 * Initialize internal objects
40 abstract protected function _init();
45 * Method opens a new XML element with an element name and an optional value
47 * @param string $name XML tag name
48 * @param string $value Optional value of the XML tag
49 * @return AbstractGenerator Fluent interface
51 public function openElement($name, $value = null)
53 $this->_openElement($name);
54 if ($value !== null) {
55 $this->_writeTextData($value);
62 * End of an XML element
64 * Method marks the end of an XML element
66 * @param string $name XML tag name
67 * @return AbstractGenerator Fluent interface
69 public function closeElement($name)
71 $this->_closeElement($name);
81 public function getEncoding()
83 return $this->encoding
;
89 * @param string $encoding
90 * @return AbstractGenerator
92 public function setEncoding($encoding)
94 $this->encoding
= $encoding;
99 * Returns the XML as a string and flushes all internal buffers
103 public function flush()
105 $xml = $this->saveXml();
111 * Returns XML without document declaration
115 public function __toString()
117 return $this->stripDeclaration($this->saveXml());
121 * Removes XML declaration from a string
126 public function stripDeclaration($xml)
128 return preg_replace('/<\?xml version="1.0"( encoding="[^\"]*")?\?>\n/u', '', $xml);
134 * @param string $name XML element name
136 abstract protected function _openElement($name);
139 * Write XML text data into the currently opened XML element
141 * @param string $text
143 abstract protected function _writeTextData($text);
148 * @param string $name
150 abstract protected function _closeElement($name);