opensrs\Ops::_convertData PHP Method

_convertData() public method

Recursivly converts PHP data into XML.
public _convertData ( &$array, $indent ) : string
return string XML string
    public function _convertData(&$array, $indent = 0)
    {
        $string = '';
        $IND = str_repeat($this->_SPACER, $indent);
        if (is_array($array)) {
            if ($this->_is_assoc($array)) {
                # HASH REFERENCE
                $string .= $this->_CRLF . $IND . '<dt_assoc>';
                $end = '</dt_assoc>';
            } else {
                # ARRAY REFERENCE
                $string .= $this->_CRLF . $IND . '<dt_array>';
                $end = '</dt_array>';
            }
            foreach ($array as $k => $v) {
                ++$indent;
                /* don't encode some types of stuff */
                if (gettype($v) == 'resource' || gettype($v) == 'user function' || gettype($v) == 'unknown type') {
                    continue;
                }
                $string .= $this->_CRLF . $IND . '<item key="' . $k . '"';
                if (gettype($v) == 'object' && get_class($v)) {
                    $string .= ' class="' . get_class($v) . '"';
                }
                $string .= '>';
                if (is_array($v) || is_object($v)) {
                    $string .= $this->_convertData($v, $indent + 1);
                    $string .= $this->_CRLF . $IND . '</item>';
                } else {
                    $string .= $this->_quoteXMLChars($v) . '</item>';
                }
                --$indent;
            }
            $string .= $this->_CRLF . $IND . $end;
        } else {
            # SCALAR
            $string .= $this->_CRLF . $IND . '<dt_scalar>' . $this->_quoteXMLChars($array) . '</dt_scalar>';
        }
        return $string;
    }

Usage Example

コード例 #1
0
ファイル: OpsTest.php プロジェクト: opensrs/osrs-toolkit-php
 /**
  * Should convert php arrays to valid xml recursivly.
  */
 public function testConvertData()
 {
     $ops = new Ops();
     $data = array('please' => 'convert', 'me');
     $result = $ops->_convertData($data);
     $xml = XMLReader::xml($result);
     // The validate parser option must be enabled for
     // this method to work properly
     $xml->setParserProperty(XMLReader::VALIDATE, true);
     // make sure this is xml
     $this->assertTrue($xml->isValid());
 }