## Usage
### Creation
You can easily create valid YouTube resource object:
``` php
use LapaLabs\YoutubeExtractor\Resource\YoutubeResource;
// Build resource object from valid YouTube resource ID
$resource = new YoutubeResource('5qanlirrRWs');
// or with static method
$resource = YoutubeResource::create('5qanlirrRWs');
// or create from valid YouTube resource URL
$resource = YoutubeResource::createFromUrl('https://www.youtube.com/watch?v=5qanlirrRWs');
There are a few valid YouTube resource URLs, supported by this library,
that should be used in YoutubeResource::createFromUrl() method:, (*5)
https://youtube.com/watch?v=5qanlirrRWs
https://m.youtube.com/watch?v=5qanlirrRWs
https://www.youtube.com/watch?v=5qanlirrRWs
https://www.youtube.com/embed/5qanlirrRWs
https://youtu.be/5qanlirrRWs
Advanced usage
After resource was successfully created you get access to a bunch of useful methods:, (*6)
// other useful methods to build various valid URLs
$resource->buildUrl(); // shortcut alias for buildDefaultUrl
$resource->buildDefaultUrl(); // https://www.youtube.com/watch?v=5qanlirrRWs
$resource->buildAliasUrl(); // https://youtube.com/watch?v=5qanlirrRWs
$resource->buildMobileUrl(); // https://m.youtube.com/watch?v=5qanlirrRWs
$resource->buildShortUrl(); // https://youtu.be/5qanlirrRWs, (*8)
You can get array of valid YouTube resource URLs which could used in `createFromUrl` method:
``` php
YoutubeResource::getValidHosts(); // array of valid YouTube resource URLs
To check whether YouTube resource ID or host is valid use follow methods:, (*9)
``` php
YoutubeResource::isValidId('5qanlirrRWs'); // return true if ID is valid
YoutubeResource::isValidHost('youtu.be'); // return true if host is valid, (*10)
You can easily get HTML code to embed YouTube resource on your page:
``` php
$resource->buildEmbedCode(); // with default attributes returns:
// to pass any other parameters or override defaults with your own use:
$resource->buildEmbedCode([
'width' => 800, // override default 560
'height' => 600, // override default 315
'class' => 'video', // add new attribute
]);
The passed attributes to buildEmbedCode() methods applies for current embed HTML code only.
To change default attributes globally for specific resource you should pass an array of attributes
to setAttributes() method. To get current default HTML attributes of specific resource use
getAttributes() method., (*11)
There are a few attributes by default:, (*12)
php
[
'width' => 560,
'height' => 315,
'src' => '', // hold position for specific order
'frameborder' => 0,
'allowfullscreen' => null,
];, (*13)
NOTE: The src attribute is only required for building embed HTML code
and will be injected automatically by calling buildEmbedUrl() method behind the scenes.
So every given src value to buildEmbedCode() or setAttributes() will be ignoring
and replacing with correct URL for current resource. You could give it like in example above
to point its position in HTML code., (*14)
Links
Feel free to create an Issue or [Pull Request][2] if you find a bug
or just want to propose improvement suggestion., (*15)