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

Allow requests for multiple asset metadata. #15

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 32 additions & 1 deletion src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -591,6 +591,14 @@ public function downloadAsset($assetID) {

/**
* Get asset metadata.
*
* @param int|string $assetId
* Multiple IDs can be passed in separated by a comma, or a single ID can
* be used.
*
* @return array
* The metadata for the asset. If multiple assets are passed in, this will
* be an array of arrays of metadata keyed by asset ID.
*/
public function getAssetMetadata($assetId) {
$this->checkAuth();
Expand All @@ -603,6 +611,30 @@ public function getAssetMetadata($assetId) {

$response = json_decode((string) $response->getBody());

// Check if this contains multiple metadatas.
$metadata = [];
if (!isset($response->active_fields)) {
foreach ($response as $asset_id => $asset_metadata) {
$metadata[$asset_id] = $this->parseMetadata($asset_metadata);
}
}
else {
$metadata = $this->parseMetadata($response);
}

return $metadata;
}

/**
* Helper to parse metadata response for a single asset.
*
* @param \stdClass $metadata
* The metadata response object.
*
* @return array
* The parsed metadata.
*/
protected function parseMetadata($response) {
$metadata = [];
foreach ($response->active_fields as $field) {
if (!empty($field->value)) {
Expand All @@ -612,7 +644,6 @@ public function getAssetMetadata($assetId) {
];
}
}

return $metadata;
}

Expand Down