CorsRule.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. <?php
  2. namespace OSS\Model;
  3. use OSS\Core\OssException;
  4. /**
  5. * Class CorsRule
  6. * @package OSS\Model
  7. * @link http://help.aliyun.com/document_detail/oss/api-reference/cors/PutBucketcors.html
  8. */
  9. class CorsRule
  10. {
  11. /**
  12. * Rule中增加一条allowedOrigin
  13. *
  14. * @param string $allowedOrigin
  15. */
  16. public function addAllowedOrigin($allowedOrigin)
  17. {
  18. if (!empty($allowedOrigin)) {
  19. $this->allowedOrigins[] = $allowedOrigin;
  20. }
  21. }
  22. /**
  23. * Rule中增加一条allowedMethod
  24. *
  25. * @param string $allowedMethod
  26. */
  27. public function addAllowedMethod($allowedMethod)
  28. {
  29. if (!empty($allowedMethod)) {
  30. $this->allowedMethods[] = $allowedMethod;
  31. }
  32. }
  33. /**
  34. * Rule中增加一条allowedHeader
  35. *
  36. * @param string $allowedHeader
  37. */
  38. public function addAllowedHeader($allowedHeader)
  39. {
  40. if (!empty($allowedHeader)) {
  41. $this->allowedHeaders[] = $allowedHeader;
  42. }
  43. }
  44. /**
  45. * Rule中增加一条exposeHeader
  46. *
  47. * @param string $exposeHeader
  48. */
  49. public function addExposeHeader($exposeHeader)
  50. {
  51. if (!empty($exposeHeader)) {
  52. $this->exposeHeaders[] = $exposeHeader;
  53. }
  54. }
  55. /**
  56. * @return int
  57. */
  58. public function getMaxAgeSeconds()
  59. {
  60. return $this->maxAgeSeconds;
  61. }
  62. /**
  63. * @param int $maxAgeSeconds
  64. */
  65. public function setMaxAgeSeconds($maxAgeSeconds)
  66. {
  67. $this->maxAgeSeconds = $maxAgeSeconds;
  68. }
  69. /**
  70. * 得到AllowedHeaders列表
  71. *
  72. * @return string[]
  73. */
  74. public function getAllowedHeaders()
  75. {
  76. return $this->allowedHeaders;
  77. }
  78. /**
  79. * 得到AllowedOrigins列表
  80. *
  81. * @return string[]
  82. */
  83. public function getAllowedOrigins()
  84. {
  85. return $this->allowedOrigins;
  86. }
  87. /**
  88. * 得到AllowedMethods列表
  89. *
  90. * @return string[]
  91. */
  92. public function getAllowedMethods()
  93. {
  94. return $this->allowedMethods;
  95. }
  96. /**
  97. * 得到ExposeHeaders列表
  98. *
  99. * @return string[]
  100. */
  101. public function getExposeHeaders()
  102. {
  103. return $this->exposeHeaders;
  104. }
  105. /**
  106. * 根据提供的xmlRule, 把this按照一定的规则插入到$xmlRule中
  107. *
  108. * @param \SimpleXMLElement $xmlRule
  109. * @throws OssException
  110. */
  111. public function appendToXml(&$xmlRule)
  112. {
  113. if (!isset($this->maxAgeSeconds)) {
  114. throw new OssException("maxAgeSeconds is not set in the Rule");
  115. }
  116. foreach ($this->allowedOrigins as $allowedOrigin) {
  117. $xmlRule->addChild(CorsConfig::OSS_CORS_ALLOWED_ORIGIN, $allowedOrigin);
  118. }
  119. foreach ($this->allowedMethods as $allowedMethod) {
  120. $xmlRule->addChild(CorsConfig::OSS_CORS_ALLOWED_METHOD, $allowedMethod);
  121. }
  122. foreach ($this->allowedHeaders as $allowedHeader) {
  123. $xmlRule->addChild(CorsConfig::OSS_CORS_ALLOWED_HEADER, $allowedHeader);
  124. }
  125. foreach ($this->exposeHeaders as $exposeHeader) {
  126. $xmlRule->addChild(CorsConfig::OSS_CORS_EXPOSE_HEADER, $exposeHeader);
  127. }
  128. $xmlRule->addChild(CorsConfig::OSS_CORS_MAX_AGE_SECONDS, strval($this->maxAgeSeconds));
  129. }
  130. private $allowedHeaders = array();
  131. private $allowedOrigins = array();
  132. private $allowedMethods = array();
  133. private $exposeHeaders = array();
  134. private $maxAgeSeconds = null;
  135. }