CnameConfig.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <?php
  2. namespace OSS\Model;
  3. use OSS\Core\OssException;
  4. /**
  5. * Class CnameConfig
  6. * @package OSS\Model
  7. *
  8. * TODO: fix link
  9. * @link http://help.aliyun.com/document_detail/oss/api-reference/cors/PutBucketcors.html
  10. */
  11. class CnameConfig implements XmlConfig
  12. {
  13. public function __construct()
  14. {
  15. $this->cnameList = array();
  16. }
  17. /**
  18. * @return array
  19. * @example
  20. * array(2) {
  21. * [0]=>
  22. * array(3) {
  23. * ["Domain"]=>
  24. * string(11) "www.foo.com"
  25. * ["Status"]=>
  26. * string(7) "enabled"
  27. * ["LastModified"]=>
  28. * string(8) "20150101"
  29. * }
  30. * [1]=>
  31. * array(3) {
  32. * ["Domain"]=>
  33. * string(7) "bar.com"
  34. * ["Status"]=>
  35. * string(8) "disabled"
  36. * ["LastModified"]=>
  37. * string(8) "20160101"
  38. * }
  39. * }
  40. */
  41. public function getCnames()
  42. {
  43. return $this->cnameList;
  44. }
  45. public function addCname($cname)
  46. {
  47. if (count($this->cnameList) >= self::OSS_MAX_RULES) {
  48. throw new OssException(
  49. "num of cname in the config exceeds self::OSS_MAX_RULES: " . strval(self::OSS_MAX_RULES));
  50. }
  51. $this->cnameList[] = array('Domain' => $cname);
  52. }
  53. public function parseFromXml($strXml)
  54. {
  55. $xml = simplexml_load_string($strXml);
  56. if (!isset($xml->Cname)) return;
  57. foreach ($xml->Cname as $entry) {
  58. $cname = array();
  59. foreach ($entry as $key => $value) {
  60. $cname[strval($key)] = strval($value);
  61. }
  62. $this->cnameList[] = $cname;
  63. }
  64. }
  65. public function serializeToXml()
  66. {
  67. $strXml = <<<EOF
  68. <?xml version="1.0" encoding="utf-8"?>
  69. <BucketCnameConfiguration>
  70. </BucketCnameConfiguration>
  71. EOF;
  72. $xml = new \SimpleXMLElement($strXml);
  73. foreach ($this->cnameList as $cname) {
  74. $node = $xml->addChild('Cname');
  75. foreach ($cname as $key => $value) {
  76. $node->addChild($key, $value);
  77. }
  78. }
  79. return $xml->asXML();
  80. }
  81. public function __toString()
  82. {
  83. return $this->serializeToXml();
  84. }
  85. const OSS_MAX_RULES = 10;
  86. private $cnameList = array();
  87. }