Your IP : 216.73.216.68


Current Path : /home/jeromecohp/www/plugins/system/yooessentials/modules/api/src/Google/
Upload File :
Current File : /home/jeromecohp/www/plugins/system/yooessentials/modules/api/src/Google/YouTubeApi.php

<?php
/**
 * @package   Essentials YOOtheme Pro (Freemium) 2.0.19 build 1026.0920
 * @author    ZOOlanders https://www.zoolanders.com
 * @copyright Copyright (C) Joolanders, SL
 * @license   http://www.gnu.org/licenses/gpl.html GNU/GPL
 */

namespace ZOOlanders\YOOessentials\Api\Google;

// https://developers.google.com/youtube/v3
class YouTubeApi extends AbstractGoogleApi implements YouTubeApiInterface
{
    protected $apiEndpoint = 'https://www.googleapis.com/youtube/v3';

    public function search(array $args = []): array
    {
        if (!empty($args['channelId'])) {
            $channel = $this->channel($args['channelId']);

            if (!isset($channel['id'])) {
                throw new \Exception(sprintf('Channel ID not valid.', $args['channelId']));
            }
        }

        return $this->get(
            'search',
            array_merge(
                [
                    'maxResults' => 50,
                    'part' => 'snippet',
                ],
                $args
            )
        );
    }

    public function video(string $id): array
    {
        return $this->videos([$id])['items'][0] ?? [];
    }

    public function videos(array $ids, array $args = []): array
    {
        if (!count($ids)) {
            return ['items' => []];
        }

        return $this->get(
            'videos',
            array_merge(
                [
                    'id' => implode(',', $ids),
                    'part' => 'snippet,contentDetails,statistics',
                ],
                $args
            )
        );
    }

    public function channel(string $channelId, array $args = []): array
    {
        return $this->channels(['id' => $channelId] + $args)['items'][0] ?? [];
    }

    public function channels(array $args = []): array
    {
        return $this->get(
            'channels',
            array_merge(
                [
                    'maxResults' => 50,
                    'part' => 'snippet',
                ],
                $args
            )
        );
    }

    public function playlists(array $args = []): array
    {
        return $this->get(
            'playlists',
            array_merge(
                [
                    'maxResults' => 50,
                    'part' => 'snippet',
                ],
                $args
            )
        );
    }

    public function channelVideos(string $channelId, array $args = []): array
    {
        $channel = $this->channel($channelId, ['part' => 'contentDetails']);
        $playlistId = $channel['contentDetails']['relatedPlaylists']['uploads'] ?? '';

        if (!$playlistId) {
            return ['items' => []];
        }

        $items = $this->playlistItems($playlistId, $args)['items'] ?? [];

        return $this->videos(
            array_map(function ($item) {
                return $item['snippet']['resourceId']['videoId'];
            }, $items),
            $args
        );
    }

    public function playlistVideos(string $playlistId, array $args = []): array
    {
        $items = $this->playlistItems($playlistId, $args)['items'] ?? [];

        return $this->videos(
            array_map(function ($item) {
                return $item['snippet']['resourceId']['videoId'];
            }, $items),
            $args
        );
    }

    protected function playlistItems(string $playlistId, array $args = []): array
    {
        return $this->get(
            'playlistItems',
            array_merge(
                [
                    'maxResults' => 50,
                    'part' => 'snippet',
                    'playlistId' => $playlistId,
                ],
                $args
            )
        );
    }
}