CorsConfig.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <?php
  2. namespace OSS\Model;
  3. use OSS\Core\OssException;
  4. /**
  5. * Class CorsConfig
  6. * @package OSS\Model
  7. *
  8. * @link http://help.aliyun.com/document_detail/oss/api-reference/cors/PutBucketcors.html
  9. */
  10. class CorsConfig implements XmlConfig
  11. {
  12. /**
  13. * CorsConfig constructor.
  14. */
  15. public function __construct()
  16. {
  17. $this->rules = array();
  18. }
  19. /**
  20. * Get CorsRule list
  21. *
  22. * @return CorsRule[]
  23. */
  24. public function getRules()
  25. {
  26. return $this->rules;
  27. }
  28. /**
  29. * Add a new CorsRule
  30. *
  31. * @param CorsRule $rule
  32. * @throws OssException
  33. */
  34. public function addRule($rule)
  35. {
  36. if (count($this->rules) >= self::OSS_MAX_RULES) {
  37. throw new OssException("num of rules in the config exceeds self::OSS_MAX_RULES: " . strval(self::OSS_MAX_RULES));
  38. }
  39. $this->rules[] = $rule;
  40. }
  41. /**
  42. * Parse CorsConfig from the xml.
  43. *
  44. * @param string $strXml
  45. * @throws OssException
  46. * @return null
  47. */
  48. public function parseFromXml($strXml)
  49. {
  50. $xml = simplexml_load_string($strXml);
  51. if (!isset($xml->CORSRule)) return;
  52. foreach ($xml->CORSRule as $rule) {
  53. $corsRule = new CorsRule();
  54. foreach ($rule as $key => $value) {
  55. if ($key === self::OSS_CORS_ALLOWED_HEADER) {
  56. $corsRule->addAllowedHeader(strval($value));
  57. } elseif ($key === self::OSS_CORS_ALLOWED_METHOD) {
  58. $corsRule->addAllowedMethod(strval($value));
  59. } elseif ($key === self::OSS_CORS_ALLOWED_ORIGIN) {
  60. $corsRule->addAllowedOrigin(strval($value));
  61. } elseif ($key === self::OSS_CORS_EXPOSE_HEADER) {
  62. $corsRule->addExposeHeader(strval($value));
  63. } elseif ($key === self::OSS_CORS_MAX_AGE_SECONDS) {
  64. $corsRule->setMaxAgeSeconds(strval($value));
  65. }
  66. }
  67. $this->addRule($corsRule);
  68. }
  69. return;
  70. }
  71. /**
  72. * Serialize the object into xml string.
  73. *
  74. * @return string
  75. */
  76. public function serializeToXml()
  77. {
  78. $xml = new \SimpleXMLElement('<?xml version="1.0" encoding="utf-8"?><CORSConfiguration></CORSConfiguration>');
  79. foreach ($this->rules as $rule) {
  80. $xmlRule = $xml->addChild('CORSRule');
  81. $rule->appendToXml($xmlRule);
  82. }
  83. return $xml->asXML();
  84. }
  85. public function __toString()
  86. {
  87. return $this->serializeToXml();
  88. }
  89. const OSS_CORS_ALLOWED_ORIGIN = 'AllowedOrigin';
  90. const OSS_CORS_ALLOWED_METHOD = 'AllowedMethod';
  91. const OSS_CORS_ALLOWED_HEADER = 'AllowedHeader';
  92. const OSS_CORS_EXPOSE_HEADER = 'ExposeHeader';
  93. const OSS_CORS_MAX_AGE_SECONDS = 'MaxAgeSeconds';
  94. const OSS_MAX_RULES = 10;
  95. /**
  96. * CorsRule list
  97. *
  98. * @var CorsRule[]
  99. */
  100. private $rules = array();
  101. }