LoggingConfig.php 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. <?php
  2. namespace OSS\Model;
  3. /**
  4. * Class LoggingConfig
  5. * @package OSS\Model
  6. * @link http://help.aliyun.com/document_detail/oss/api-reference/bucket/PutBucketLogging.html
  7. */
  8. class LoggingConfig implements XmlConfig
  9. {
  10. /**
  11. * LoggingConfig constructor.
  12. * @param null $targetBucket
  13. * @param null $targetPrefix
  14. */
  15. public function __construct($targetBucket = null, $targetPrefix = null)
  16. {
  17. $this->targetBucket = $targetBucket;
  18. $this->targetPrefix = $targetPrefix;
  19. }
  20. /**
  21. * @param $strXml
  22. * @return null
  23. */
  24. public function parseFromXml($strXml)
  25. {
  26. $xml = simplexml_load_string($strXml);
  27. if (!isset($xml->LoggingEnabled)) return;
  28. foreach ($xml->LoggingEnabled as $status) {
  29. foreach ($status as $key => $value) {
  30. if ($key === 'TargetBucket') {
  31. $this->targetBucket = strval($value);
  32. } elseif ($key === 'TargetPrefix') {
  33. $this->targetPrefix = strval($value);
  34. }
  35. }
  36. break;
  37. }
  38. }
  39. /**
  40. * 序列化成xml字符串
  41. *
  42. */
  43. public function serializeToXml()
  44. {
  45. $xml = new \SimpleXMLElement('<?xml version="1.0" encoding="utf-8"?><BucketLoggingStatus></BucketLoggingStatus>');
  46. if (isset($this->targetBucket) && isset($this->targetPrefix)) {
  47. $loggingEnabled = $xml->addChild('LoggingEnabled');
  48. $loggingEnabled->addChild('TargetBucket', $this->targetBucket);
  49. $loggingEnabled->addChild('TargetPrefix', $this->targetPrefix);
  50. }
  51. return $xml->asXML();
  52. }
  53. /**
  54. * @return string
  55. */
  56. public function __toString()
  57. {
  58. return $this->serializeToXml();
  59. }
  60. /**
  61. * @return string
  62. */
  63. public function getTargetBucket()
  64. {
  65. return $this->targetBucket;
  66. }
  67. /**
  68. * @return string
  69. */
  70. public function getTargetPrefix()
  71. {
  72. return $this->targetPrefix;
  73. }
  74. private $targetBucket = "";
  75. private $targetPrefix = "";
  76. }