File indexing completed on 2024-12-22 05:37:14

0001 <?php
0002 /**
0003  * Zend Framework
0004  *
0005  * LICENSE
0006  *
0007  * This source file is subject to the new BSD license that is bundled
0008  * with this package in the file LICENSE.txt.
0009  * It is also available through the world-wide-web at this URL:
0010  * http://framework.zend.com/license/new-bsd
0011  * If you did not receive a copy of the license and are unable to
0012  * obtain it through the world-wide-web, please send an email
0013  * to license@zend.com so we can send you a copy immediately.
0014  *
0015  * @category   Zend
0016  * @package    Zend_XmlRpc
0017  * @subpackage Generator
0018  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0019  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0020  * @version    $Id$
0021  */
0022 
0023 /**
0024  * Abstract XML generator adapter
0025  */
0026 abstract class Zend_XmlRpc_Generator_GeneratorAbstract
0027 {
0028     /**
0029      * XML encoding string
0030      *
0031      * @var string
0032      */
0033     protected $_encoding;
0034 
0035     /**
0036      * Construct new instance of the generator
0037      *
0038      * @param string $encoding XML encoding, default UTF-8
0039      */
0040     public function __construct($encoding = 'UTF-8')
0041     {
0042         $this->_encoding = $encoding;
0043         $this->_init();
0044     }
0045 
0046     /**
0047      * Start XML element
0048      *
0049      * Method opens a new XML element with an element name and an optional value
0050      *
0051      * @param string $name XML tag name
0052      * @param string $value Optional value of the XML tag
0053      * @return Zend_XmlRpc_Generator_Abstract Fluent interface
0054      */
0055     public function openElement($name, $value = null)
0056     {
0057         $this->_openElement($name);
0058         if ($value !== null) {
0059             $this->_writeTextData($value);
0060         }
0061 
0062         return $this;
0063     }
0064 
0065     /**
0066      * End of an XML element
0067      *
0068      * Method marks the end of an XML element
0069      *
0070      * @param string $name XML tag name
0071      * @return Zend_XmlRpc_Generator_Abstract Fluent interface
0072      */
0073     public function closeElement($name)
0074     {
0075         $this->_closeElement($name);
0076 
0077         return $this;
0078     }
0079 
0080     /**
0081      * Return XML as a string
0082      *
0083      * @return string
0084      */
0085     abstract public function saveXml();
0086 
0087     /**
0088      * Return encoding
0089      *
0090      * @return string
0091      */
0092     public function getEncoding()
0093     {
0094         return $this->_encoding;
0095     }
0096 
0097     /**
0098      * Returns the XML as a string and flushes all internal buffers
0099      *
0100      * @return string
0101      */
0102     public function flush()
0103     {
0104         $xml = $this->saveXml();
0105         $this->_init();
0106         return $xml;
0107     }
0108 
0109     /**
0110      * Returns XML without document declaration
0111      *
0112      * @return string
0113      */
0114     public function __toString()
0115     {
0116         return $this->stripDeclaration($this->saveXml());
0117     }
0118 
0119     /**
0120      * Removes XML declaration from a string
0121      *
0122      * @param string $xml
0123      * @return string
0124      */
0125     public function stripDeclaration($xml)
0126     {
0127         return preg_replace('/<\?xml version="1.0"( encoding="[^\"]*")?\?>\n/u', '', $xml);
0128     }
0129 
0130     /**
0131      * Start XML element
0132      *
0133      * @param string $name XML element name
0134      */
0135     abstract protected function _openElement($name);
0136 
0137     /**
0138      * Write XML text data into the currently opened XML element
0139      *
0140      * @param string $text
0141      */
0142     abstract protected function _writeTextData($text);
0143 
0144     /**
0145      * End XML element
0146      *
0147      * @param string $name
0148      */
0149     abstract protected function _closeElement($name);
0150 }