OPReply.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. <?php
  2. namespace MGModule\DNSManager2\mgLibs\custom\dns\submodules\OpenProvider;
  3. use \MGModule\DNSManager2\mgLibs\custom\dns\exceptions;
  4. use MGModule\DNSManager2\mgLibs\custom\dns\submodules\OpenProvider\OPApi;
  5. use \DOMDocument;
  6. class OPReply
  7. {
  8. protected $faultCode = 0;
  9. protected $faultString = null;
  10. public $value = array();
  11. protected $warnings = array();
  12. protected $raw = null;
  13. protected $dom = null;
  14. protected $filters = [];
  15. protected $maintenance = null;
  16. public function __construct ($str = null) {
  17. if ($str) {
  18. $this->raw = $str;
  19. $this->_parseReply($str);
  20. }
  21. }
  22. protected function _parseReply ($str = '')
  23. {
  24. $dom = new DOMDocument;
  25. $result = $dom->loadXML(trim($str));
  26. if (!$result) {
  27. error_log("Cannot parse xml: '$str'");
  28. }
  29. $arr = OPApi::convertXmlToPhpObj($dom->documentElement);
  30. if ((!is_array($arr) && trim($arr) == '') ||
  31. $arr['reply']['code'] == 4005)
  32. {
  33. throw new exceptions\DNSSubmoduleException("API is temporarily unavailable due to maintenance", 4005);
  34. }
  35. $this->faultCode = (int) $arr['reply']['code'];
  36. $this->faultString = $arr['reply']['desc'];
  37. $this->value = $arr['reply']['data'];
  38. if (isset($arr['reply']['warnings'])) {
  39. $this->warnings = $arr['reply']['warnings'];
  40. }
  41. if (isset($arr['reply']['maintenance'])) {
  42. $this->maintenance = $arr['reply']['maintenance'];
  43. }
  44. }
  45. public function encode ($str)
  46. {
  47. return OPApi::encode($str);
  48. }
  49. public function setFaultCode ($v)
  50. {
  51. $this->faultCode = $v;
  52. return $this;
  53. }
  54. public function setFaultString ($v)
  55. {
  56. $this->faultString = $v;
  57. return $this;
  58. }
  59. public function setValue ($v)
  60. {
  61. $this->value = $v;
  62. return $this;
  63. }
  64. public function getValue ()
  65. {
  66. return $this->value;
  67. }
  68. public function setWarnings ($v)
  69. {
  70. $this->warnings = $v;
  71. return $this;
  72. }
  73. public function getDom ()
  74. {
  75. return $this->dom;
  76. }
  77. public function getWarnings ()
  78. {
  79. return $this->warnings;
  80. }
  81. public function getMaintenance ()
  82. {
  83. return $this->maintenance;
  84. }
  85. public function getFaultString () {
  86. return $this->faultString;
  87. }
  88. public function getFaultCode ()
  89. {
  90. return $this->faultCode;
  91. }
  92. public function getRaw ()
  93. {
  94. if (!$this->raw) {
  95. $this->raw .= $this->_getReply ();
  96. }
  97. return $this->raw;
  98. }
  99. public function addFilter($filter)
  100. {
  101. $this->filters[] = $filter;
  102. }
  103. public function _getReply ()
  104. {
  105. $dom = new DOMDocument('1.0', OPApi::$encoding);
  106. $rootNode = $dom->appendChild($dom->createElement('openXML'));
  107. $replyNode = $rootNode->appendChild($dom->createElement('reply'));
  108. $codeNode = $replyNode->appendChild($dom->createElement('code'));
  109. $codeNode->appendChild($dom->createTextNode($this->faultCode));
  110. $descNode = $replyNode->appendChild($dom->createElement('desc'));
  111. $descNode->appendChild(
  112. $dom->createTextNode(OPApi::encode($this->faultString))
  113. );
  114. $dataNode = $replyNode->appendChild($dom->createElement('data'));
  115. OPApi::convertPhpObjToDom($this->value, $dataNode, $dom);
  116. if (0 < count($this->warnings)) {
  117. $warningsNode = $replyNode->appendChild($dom->createElement('warnings'));
  118. OPApi::convertPhpObjToDom($this->warnings, $warningsNode, $dom);
  119. }
  120. $this->dom = $dom;
  121. foreach ($this->filters as $f) {
  122. $f->filter($this);
  123. }
  124. return $dom->saveXML();
  125. }
  126. }