ride / lib-media
Library for abstraction of media services in the Ride framework
Installs: 4 575
Dependents: 3
Suggesters: 0
Security: 0
Stars: 0
Watchers: 9
Forks: 0
Open Issues: 0
Requires
- ride/lib-http-client: ^1.0.0
- ride/lib-validation: ^1.0.0
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