ride/lib-media

Library for abstraction of media services in the Ride framework

1.0.4 2019-10-04 09:38 UTC

This package is auto-updated.

Last update: 2024-11-04 20:30:12 UTC


README

This library adds support for custom media items, like Youtube videos, Soundcloud music or plain URL's.

MediaItem classes live in the item/ directory, and their factories in the factory directory.

What's In This Library

MediaItemFactory

The MediaItemFactory class is the interface for all media item factories. This factory has two responsibilities:

  • check a given URL if it is valid for the assosiated MediaItem class
  • instantiate a new MediaItem class

The AbstractMediaItemFactory has a default implementation for the createFormUrl, createFromId and setClientId methods. Its constructor requires an instance of ride\library\http\client\Client.

The isValidUrl method should always be implemented in the child class, and should contain logic in order to determine if an URL is parseable for the related media item.

MediaItem

The MediaItem class is the interface for all media items. There is an abstract implementation called AbstractMediaItem from which all classes can extend. Each MediaItem class should implement at least following methods:

abstract protected function parseUrl($url);
abstract protected function loadProperties();

The parseUrl method will take a given URL and parse it for this specific MediaItem implementation. It can be assumed that this URL is parseable because of the check done in the MediaItemFactory.

Each media item needs a factory for the MediaFactory to be able to use it.

MediaFactory

The MediaFactory interface glues all the MediaItemFactory instances together into an easy facade. It will use specific media item factories to test if a given URL can be parsed.

A generic implementation is provided through the SimpleMediaFactory class.

Code sample

You can check the following code sample to see some of the possibilities of this library:

use ride\library\media\factory\UrlMediaItemFactory;
use ride\library\media\factory\VimeoMediaItemFactory;
use ride\library\media\factory\YoutubeMediaItemFactory;
use ride\library\media\MediaFactory;
use ride\library\media\SimpleMediaFactory;
use ride\library\http\client\Client;

function createMediaFactory(Client $httpClient) {
    $youtubeMediaItemFactory = new YoutubeMediaItemFactory($httpClient);
    $youtubeMediaItemFactory->setClientId('client-id');
    
    $vimeoMediaItemFactory = new VimeoMediaItemFactory($httpClient);
    
    $urlMediaItemFactory = new UrlMediaItemFactory($httpClient);

    $mediaFactory = new SimpleMediaFactory($httpClient);
    $mediaFactory->setMediaItemFactory($youtubeMediaItemFactory);
    $mediaFactory->setMediaItemFactory($vimeoMediaItemFactory);
    $mediaFactory->setDefaultMediaItemFactory($urlMediaItemFactory);
    
    return $mediaFactory;
}

function useMediaFactory(MediaFactory $mediaFactory) {
    // create a MediaItem using a URL
    $youtubeMediaItem = $simpleMediaFactory->createMediaItem('https://www.youtube.com/watch?v=njos57IJf-0');
    
    $type = $youtubeMediaItem->getType();
    // youtube
    $id = $youtubeMediaItem->getId();
    // njos57IJf-0
    $title = $youtubeMediaItem->getTitle();
    $description = $youtubeMediaItem->getDescription();
    
    // if you know the type and id, you can fetch it like this
    $youtubeMediaItem = $simpleMediaFactory->getMediaItem($type, $id);
}

Related Modules

Installation

You can use Composer to install this library.

composer require ride/lib-media