ServerSideEncryptionConfig.php 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. <?php
  2. namespace OSS\Model;
  3. use OSS\Core\OssException;
  4. /**
  5. * Class ServerSideEncryptionConfig
  6. * @package OSS\Model
  7. *
  8. * @link https://help.aliyun.com/document_detail/117914.htm
  9. */
  10. class ServerSideEncryptionConfig implements XmlConfig
  11. {
  12. /**
  13. * ServerSideEncryptionConfig constructor.
  14. * @param null $sseAlgorithm
  15. * @param null $kmsMasterKeyID
  16. */
  17. public function __construct($sseAlgorithm = null, $kmsMasterKeyID = null)
  18. {
  19. $this->sseAlgorithm = $sseAlgorithm;
  20. $this->kmsMasterKeyID = $kmsMasterKeyID;
  21. }
  22. /**
  23. * Parse ServerSideEncryptionConfig from the xml.
  24. *
  25. * @param string $strXml
  26. * @throws OssException
  27. * @return null
  28. */
  29. public function parseFromXml($strXml)
  30. {
  31. $xml = simplexml_load_string($strXml);
  32. if (!isset($xml->ApplyServerSideEncryptionByDefault)) return;
  33. foreach ($xml->ApplyServerSideEncryptionByDefault as $default) {
  34. foreach ($default as $key => $value) {
  35. if ($key === 'SSEAlgorithm') {
  36. $this->sseAlgorithm = strval($value);
  37. } elseif ($key === 'KMSMasterKeyID') {
  38. $this->kmsMasterKeyID = strval($value);
  39. }
  40. }
  41. break;
  42. }
  43. }
  44. /**
  45. * Serialize the object into xml string.
  46. *
  47. * @return string
  48. */
  49. public function serializeToXml()
  50. {
  51. $xml = new \SimpleXMLElement('<?xml version="1.0" encoding="utf-8"?><ServerSideEncryptionRule></ServerSideEncryptionRule>');
  52. $default = $xml->addChild('ApplyServerSideEncryptionByDefault');
  53. if (isset($this->sseAlgorithm)) {
  54. $default->addChild('SSEAlgorithm', $this->sseAlgorithm);
  55. }
  56. if (isset($this->kmsMasterKeyID)) {
  57. $default->addChild('KMSMasterKeyID', $this->kmsMasterKeyID);
  58. }
  59. return $xml->asXML();
  60. }
  61. public function __toString()
  62. {
  63. return $this->serializeToXml();
  64. }
  65. /**
  66. * @return string
  67. */
  68. public function getSSEAlgorithm()
  69. {
  70. return $this->sseAlgorithm;
  71. }
  72. /**
  73. * @return string
  74. */
  75. public function getKMSMasterKeyID()
  76. {
  77. return $this->kmsMasterKeyID;
  78. }
  79. private $sseAlgorithm = "";
  80. private $kmsMasterKeyID = "";
  81. }