opensrs\Ops::encode PHP Метод

encode() публичный Метод

Converts a PHP array into an OPS message.
public encode ( $array ) : string
Результат string OPS XML message
    public function encode($array)
    {
        ++$this->_MSGCNT;
        $msg_id = $this->_SESSID + $this->_MSGCNT;
        /* addition removes the leading zero */
        $msg_type = $this->_MSGTYPE_STD;
        if ($array['protocol']) {
            $array['protocol'] = strtoupper($array['protocol']);
        }
        if ($array['action']) {
            $array['action'] = strtoupper($array['action']);
        }
        if ($array['object']) {
            $array['object'] = strtoupper($array['object']);
        }
        $xml_data_block = $this->PHP2XML($array);
        $ops_msg = '<?xml version="1.0" encoding="UTF-8" standalone="no" ?>' . $this->_CRLF . '<!DOCTYPE OPS_envelope SYSTEM "ops.dtd">' . $this->_CRLF . '<OPS_envelope>' . $this->_CRLF . $this->_SPACER . '<header>' . $this->_CRLF . $this->_SPACER . $this->_SPACER . '<version>' . $this->_OPS_VERSION . '</version>' . $this->_CRLF . $this->_SPACER . $this->_SPACER . '<msg_id>' . $msg_id . '</msg_id>' . $this->_CRLF . $this->_SPACER . $this->_SPACER . '<msg_type>' . $msg_type . '</msg_type>' . $this->_CRLF . $this->_SPACER . '</header>' . $this->_CRLF . $this->_SPACER . '<body>' . $this->_CRLF . $xml_data_block . $this->_CRLF . $this->_SPACER . '</body>' . $this->_CRLF . '</OPS_envelope>';
        return $ops_msg;
    }

Usage Example

Пример #1
0
 /**
  * Should convert php arrays to valid xml.
  */
 public function testEncode()
 {
     $ops = new Ops();
     $data = array('protocol' => '', 'action' => '', 'object' => '');
     $result = $ops->encode($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());
 }