ListObjectsResult.php 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. <?php
  2. namespace OSS\Result;
  3. use OSS\Core\OssUtil;
  4. use OSS\Model\ObjectInfo;
  5. use OSS\Model\ObjectListInfo;
  6. use OSS\Model\PrefixInfo;
  7. /**
  8. * Class ListObjectsResult
  9. * @package OSS\Result
  10. */
  11. class ListObjectsResult extends Result
  12. {
  13. /**
  14. * Parse the xml data returned by the ListObjects interface
  15. *
  16. * return ObjectListInfo
  17. */
  18. protected function parseDataFromResponse()
  19. {
  20. $xml = new \SimpleXMLElement($this->rawResponse->body);
  21. $encodingType = isset($xml->EncodingType) ? strval($xml->EncodingType) : "";
  22. $objectList = $this->parseObjectList($xml, $encodingType);
  23. $prefixList = $this->parsePrefixList($xml, $encodingType);
  24. $bucketName = isset($xml->Name) ? strval($xml->Name) : "";
  25. $prefix = isset($xml->Prefix) ? strval($xml->Prefix) : "";
  26. $prefix = OssUtil::decodeKey($prefix, $encodingType);
  27. $marker = isset($xml->Marker) ? strval($xml->Marker) : "";
  28. $marker = OssUtil::decodeKey($marker, $encodingType);
  29. $maxKeys = isset($xml->MaxKeys) ? intval($xml->MaxKeys) : 0;
  30. $delimiter = isset($xml->Delimiter) ? strval($xml->Delimiter) : "";
  31. $delimiter = OssUtil::decodeKey($delimiter, $encodingType);
  32. $isTruncated = isset($xml->IsTruncated) ? strval($xml->IsTruncated) : "";
  33. $nextMarker = isset($xml->NextMarker) ? strval($xml->NextMarker) : "";
  34. $nextMarker = OssUtil::decodeKey($nextMarker, $encodingType);
  35. return new ObjectListInfo($bucketName, $prefix, $marker, $nextMarker, $maxKeys, $delimiter, $isTruncated, $objectList, $prefixList);
  36. }
  37. private function parseObjectList($xml, $encodingType)
  38. {
  39. $retList = array();
  40. if (isset($xml->Contents)) {
  41. foreach ($xml->Contents as $content) {
  42. $key = isset($content->Key) ? strval($content->Key) : "";
  43. $key = OssUtil::decodeKey($key, $encodingType);
  44. $lastModified = isset($content->LastModified) ? strval($content->LastModified) : "";
  45. $eTag = isset($content->ETag) ? strval($content->ETag) : "";
  46. $type = isset($content->Type) ? strval($content->Type) : "";
  47. $size = isset($content->Size) ? intval($content->Size) : 0;
  48. $storageClass = isset($content->StorageClass) ? strval($content->StorageClass) : "";
  49. $retList[] = new ObjectInfo($key, $lastModified, $eTag, $type, $size, $storageClass);
  50. }
  51. }
  52. return $retList;
  53. }
  54. private function parsePrefixList($xml, $encodingType)
  55. {
  56. $retList = array();
  57. if (isset($xml->CommonPrefixes)) {
  58. foreach ($xml->CommonPrefixes as $commonPrefix) {
  59. $prefix = isset($commonPrefix->Prefix) ? strval($commonPrefix->Prefix) : "";
  60. $prefix = OssUtil::decodeKey($prefix, $encodingType);
  61. $retList[] = new PrefixInfo($prefix);
  62. }
  63. }
  64. return $retList;
  65. }
  66. }