RefererConfig.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. <?php
  2. namespace OSS\Model;
  3. /**
  4. * Class RefererConfig
  5. *
  6. * @package OSS\Model
  7. * @link http://help.aliyun.com/document_detail/oss/api-reference/bucket/PutBucketReferer.html
  8. */
  9. class RefererConfig implements XmlConfig
  10. {
  11. /**
  12. * @param string $strXml
  13. * @return null
  14. */
  15. public function parseFromXml($strXml)
  16. {
  17. $xml = simplexml_load_string($strXml);
  18. if (!isset($xml->AllowEmptyReferer)) return;
  19. if (!isset($xml->RefererList)) return;
  20. $this->allowEmptyReferer =
  21. (strval($xml->AllowEmptyReferer) === 'TRUE' || strval($xml->AllowEmptyReferer) === 'true') ? true : false;
  22. foreach ($xml->RefererList->Referer as $key => $refer) {
  23. $this->refererList[] = strval($refer);
  24. }
  25. }
  26. /**
  27. * serialize the RefererConfig object into xml string
  28. *
  29. * @return string
  30. */
  31. public function serializeToXml()
  32. {
  33. $xml = new \SimpleXMLElement('<?xml version="1.0" encoding="utf-8"?><RefererConfiguration></RefererConfiguration>');
  34. if ($this->allowEmptyReferer) {
  35. $xml->addChild('AllowEmptyReferer', 'true');
  36. } else {
  37. $xml->addChild('AllowEmptyReferer', 'false');
  38. }
  39. $refererList = $xml->addChild('RefererList');
  40. foreach ($this->refererList as $referer) {
  41. $refererList->addChild('Referer', $referer);
  42. }
  43. return $xml->asXML();
  44. }
  45. /**
  46. * @return string
  47. */
  48. function __toString()
  49. {
  50. return $this->serializeToXml();
  51. }
  52. /**
  53. * @param boolean $allowEmptyReferer
  54. */
  55. public function setAllowEmptyReferer($allowEmptyReferer)
  56. {
  57. $this->allowEmptyReferer = $allowEmptyReferer;
  58. }
  59. /**
  60. * @param string $referer
  61. */
  62. public function addReferer($referer)
  63. {
  64. $this->refererList[] = $referer;
  65. }
  66. /**
  67. * @return boolean
  68. */
  69. public function isAllowEmptyReferer()
  70. {
  71. return $this->allowEmptyReferer;
  72. }
  73. /**
  74. * @return array
  75. */
  76. public function getRefererList()
  77. {
  78. return $this->refererList;
  79. }
  80. private $allowEmptyReferer = true;
  81. private $refererList = array();
  82. }