From 7abc95456056b20ebc88a5a8127fcb4e42043d65 Mon Sep 17 00:00:00 2001 From: Bruna Anacleto Pimenta Date: Wed, 18 Mar 2020 12:00:48 -0300 Subject: [PATCH] Add list and count videos by folder on CMS --- lib/Brightcove/API/API.php | 28 ++++++++++++++++++++++ lib/Brightcove/API/CMS.php | 48 ++++++++++++++++++++++++-------------- 2 files changed, 59 insertions(+), 17 deletions(-) diff --git a/lib/Brightcove/API/API.php b/lib/Brightcove/API/API.php index 52a7166..fdf56ae 100644 --- a/lib/Brightcove/API/API.php +++ b/lib/Brightcove/API/API.php @@ -23,4 +23,32 @@ public function __construct(Client $client, $account) { $this->client = $client; $this->account = $account; } + + /** + * Formats search terms. + * + * @return string + * Basic search tearms formatted (e.g ?q=tag:example) + */ + protected function formatSearchTerms($search = NULL, $sort = NULL, $limit = NULL, $offset = NULL) { + $query = ''; + if ($search) { + $query .= '&q=' . urlencode($search); + } + if ($sort) { + $query .= "&sort={$sort}"; + } + if ($limit) { + $query .= "&limit={$limit}"; + } + if ($offset) { + $query .= "&offset={$offset}"; + } + if (strlen($query) > 0) { + $query = '?' . substr($query, 1); + } + + return $query; + } + } diff --git a/lib/Brightcove/API/CMS.php b/lib/Brightcove/API/CMS.php index 87e327a..4eae798 100644 --- a/lib/Brightcove/API/CMS.php +++ b/lib/Brightcove/API/CMS.php @@ -23,25 +23,11 @@ protected function cmsRequest($method, $endpoint, $result, $is_array = FALSE, $p /** * Lists video objects with the given restrictions. * - * @return Video[] + * @return Brightcove\Object\Video[] + * Video objects. */ public function listVideos($search = NULL, $sort = NULL, $limit = NULL, $offset = NULL) { - $query = ''; - if ($search) { - $query .= '&q=' . urlencode($search); - } - if ($sort) { - $query .= "&sort={$sort}"; - } - if ($limit) { - $query .= "&limit={$limit}"; - } - if ($offset) { - $query .= "&offset={$offset}"; - } - if (strlen($query) > 0) { - $query = '?' . substr($query, 1); - } + $query = $this->formatSearchTerms($search, $sort, $limit, $offset); return $this->cmsRequest('GET', "/videos{$query}", Video::class, TRUE); } @@ -59,6 +45,34 @@ public function countVideos($search = NULL) { return NULL; } + /** + * Lists video objects in a folder with the given restrictions. + * + * @return Brightcove\Object\Video[] + * Video objects in a folder. + */ + public function listVideosInFolder($folder_id, $search = NULL, $sort = NULL, $limit = NULL, $offset = NULL) { + $query = $this->formatSearchTerms($search, $sort, $limit, $offset); + return $this->cmsRequest('GET', "/folders/{$folder_id}/videos{$query}", Video::class, TRUE); + } + + /** + * Returns the amount of a searched folder video's result. + * + * @param int $folder_id + * Folder ID. + * + * @return int|null + * Amount of a searched folder video's result. + */ + public function countVideosInFolder($folder_id) { + $result = $this->cmsRequest('GET', "/folders/{$folder_id}", NULL); + if ($result && !empty($result['video_count'])) { + return $result['video_count']; + } + return NULL; + } + /** * Gets the images for a single video. *