| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132 |
- <?php
- namespace MGModule\DNSManager2\mgLibs\custom\dns\submodules\OpenProvider;
- use \MGModule\DNSManager2\mgLibs\custom\dns\exceptions;
- use MGModule\DNSManager2\mgLibs\custom\dns\submodules\OpenProvider\OPApi;
- use \DOMDocument;
- class OPReply
- {
- protected $faultCode = 0;
- protected $faultString = null;
- public $value = array();
- protected $warnings = array();
- protected $raw = null;
- protected $dom = null;
- protected $filters = [];
- protected $maintenance = null;
- public function __construct ($str = null) {
- if ($str) {
- $this->raw = $str;
- $this->_parseReply($str);
- }
- }
- protected function _parseReply ($str = '')
- {
- $dom = new DOMDocument;
- $result = $dom->loadXML(trim($str));
- if (!$result) {
- error_log("Cannot parse xml: '$str'");
- }
- $arr = OPApi::convertXmlToPhpObj($dom->documentElement);
- if ((!is_array($arr) && trim($arr) == '') ||
- $arr['reply']['code'] == 4005)
- {
- throw new exceptions\DNSSubmoduleException("API is temporarily unavailable due to maintenance", 4005);
- }
- $this->faultCode = (int) $arr['reply']['code'];
- $this->faultString = $arr['reply']['desc'];
- $this->value = $arr['reply']['data'];
- if (isset($arr['reply']['warnings'])) {
- $this->warnings = $arr['reply']['warnings'];
- }
- if (isset($arr['reply']['maintenance'])) {
- $this->maintenance = $arr['reply']['maintenance'];
- }
- }
- public function encode ($str)
- {
- return OPApi::encode($str);
- }
- public function setFaultCode ($v)
- {
- $this->faultCode = $v;
- return $this;
- }
- public function setFaultString ($v)
- {
- $this->faultString = $v;
- return $this;
- }
- public function setValue ($v)
- {
- $this->value = $v;
- return $this;
- }
- public function getValue ()
- {
- return $this->value;
- }
- public function setWarnings ($v)
- {
- $this->warnings = $v;
- return $this;
- }
- public function getDom ()
- {
- return $this->dom;
- }
- public function getWarnings ()
- {
- return $this->warnings;
- }
- public function getMaintenance ()
- {
- return $this->maintenance;
- }
- public function getFaultString () {
- return $this->faultString;
- }
- public function getFaultCode ()
- {
- return $this->faultCode;
- }
- public function getRaw ()
- {
- if (!$this->raw) {
- $this->raw .= $this->_getReply ();
- }
- return $this->raw;
- }
- public function addFilter($filter)
- {
- $this->filters[] = $filter;
- }
- public function _getReply ()
- {
- $dom = new DOMDocument('1.0', OPApi::$encoding);
- $rootNode = $dom->appendChild($dom->createElement('openXML'));
- $replyNode = $rootNode->appendChild($dom->createElement('reply'));
- $codeNode = $replyNode->appendChild($dom->createElement('code'));
- $codeNode->appendChild($dom->createTextNode($this->faultCode));
- $descNode = $replyNode->appendChild($dom->createElement('desc'));
- $descNode->appendChild(
- $dom->createTextNode(OPApi::encode($this->faultString))
- );
- $dataNode = $replyNode->appendChild($dom->createElement('data'));
- OPApi::convertPhpObjToDom($this->value, $dataNode, $dom);
- if (0 < count($this->warnings)) {
- $warningsNode = $replyNode->appendChild($dom->createElement('warnings'));
- OPApi::convertPhpObjToDom($this->warnings, $warningsNode, $dom);
- }
- $this->dom = $dom;
- foreach ($this->filters as $f) {
- $f->filter($this);
- }
- return $dom->saveXML();
- }
- }
|