Skip to content
This repository has been archived by the owner on Apr 29, 2019. It is now read-only.

Commit

Permalink
Merge pull request #4 from paulm17/master
Browse files Browse the repository at this point in the history
Changes to listContents
  • Loading branch information
ryanvade authored Jun 14, 2018
2 parents a057132 + 641fed3 commit 3d3c8a6
Showing 1 changed file with 39 additions and 3 deletions.
42 changes: 39 additions & 3 deletions src/BoxAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ public function readStream($path)
*/
public function listContents($directory = '', $recursive = false): array
{
$path = $this->applyPathPrefix($path);
$path = $this->applyPathPrefix($directory);
$count = $this->client->getFolderItemsCount($path);
if ($count < 0) {
return false;
Expand All @@ -212,15 +212,31 @@ public function listContents($directory = '', $recursive = false): array
$items = array();
do {
$resp = $this->client->getFolderItems($path, $offset, $limit);

if ($resp->isError()) {
return false;
}
array_push($items, $resp->getJson()->entries);

foreach($resp->getJson()->entries as $entry) {
$items[] = array(
"type" => $entry->type,
"id" => $entry->id,
"etag" => 0,
"modified_at" => $entry->modified_at,
"name" => $entry->name,
"size" => $entry->size,
);
}

$offset = $offset + $limit;
$limit = ($count - $offset < 1000) ? $count - $offset : 1000;
} while ($offset != $count && $count > 0);

return $items;
return array_map(function ($entry) {
$path = $this->removePathPrefix($entry['name']);

return $this->normalizeResponse($entry, $path);
}, $items);
}

/**
Expand Down Expand Up @@ -312,4 +328,24 @@ public function getTimestamp($path)

return $arr;
}

protected function normalizeResponse(array $response): array
{
$normalizedPath = ltrim($this->removePathPrefix($response['name']), '/');

$normalizedResponse = ['path' => $normalizedPath];

if (isset($response['modified_at'])) {
$normalizedResponse['timestamp'] = strtotime($response['modified_at']);
}

if (isset($response['size'])) {
$normalizedResponse['size'] = $response['size'];
}

$type = ($response['type'] === 'folder' ? 'dir' : 'file');
$normalizedResponse['type'] = $type;

return $normalizedResponse;
}
}

0 comments on commit 3d3c8a6

Please sign in to comment.