Skip to content

Commit 36272ce

Browse files
Added the Registering a New Service Example
1 parent 0ec3c8c commit 36272ce

File tree

1 file changed

+182
-0
lines changed

1 file changed

+182
-0
lines changed

example/RegisteringANewService.md

Lines changed: 182 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,182 @@
1+
# Functional Example of Registering a DailyMotion Service
2+
3+
## DailyMotionServiceAdapter Class (The Service Adapter Itself)
4+
```php
5+
<?php
6+
namespace MyVendor\ServiceAdapter;
7+
8+
use RicardoFiorani\Adapter\AbstractServiceAdapter;
9+
use RicardoFiorani\Renderer\EmbedRendererInterface;
10+
11+
//Your service Adapter must implement VideoAdapterInterface or Extend AbstractServiceAdapter
12+
class DailyMotionServiceAdapter extends AbstractServiceAdapter
13+
{
14+
const THUMBNAIL_DEFAULT = 'thumbnail';
15+
16+
/**
17+
* AbstractVideoAdapter constructor.
18+
* @param string $url
19+
* @param string $pattern
20+
* @param EmbedRendererInterface $renderer
21+
*/
22+
public function __construct($url, $pattern, EmbedRendererInterface $renderer)
23+
{
24+
$videoId = strtok(basename($url), '_');
25+
$this->setVideoId($videoId);
26+
27+
return parent::__construct($url, $pattern, $renderer);
28+
}
29+
30+
31+
/**
32+
* Returns the service name (ie: "Youtube" or "Vimeo")
33+
* @return string
34+
*/
35+
public function getServiceName()
36+
{
37+
return 'DailyMotion';
38+
}
39+
40+
/**
41+
* Returns if the service has a thumbnail image
42+
* @return bool
43+
*/
44+
public function hasThumbnail()
45+
{
46+
return true;
47+
}
48+
49+
/**
50+
* Returns all thumbnails available sizes
51+
* @return array
52+
*/
53+
public function getThumbNailSizes()
54+
{
55+
return array(
56+
self::THUMBNAIL_DEFAULT,
57+
);
58+
}
59+
60+
/**
61+
* @param string $size
62+
* @return string
63+
*/
64+
public function getThumbnail($size)
65+
{
66+
if (false == in_array($size, $this->getThumbNailSizes())) {
67+
throw new \RicardoFiorani\Exception\InvalidThumbnailSizeException;
68+
}
69+
70+
return 'http://www.dailymotion.com/' . $size . '/video/' . $this->videoId;
71+
}
72+
73+
/**
74+
* Returns the small thumbnail's url
75+
* @return string
76+
*/
77+
public function getSmallThumbnail()
78+
{
79+
//Since this service does not provide other thumbnails sizes we just return the default size
80+
return $this->getThumbnail(self::THUMBNAIL_DEFAULT);
81+
}
82+
83+
/**
84+
* Returns the medium thumbnail's url
85+
* @return string
86+
*/
87+
public function getMediumThumbnail()
88+
{
89+
//Since this service does not provide other thumbnails sizes we just return the default size
90+
return $this->getThumbnail(self::THUMBNAIL_DEFAULT);
91+
}
92+
93+
/**
94+
* Returns the large thumbnail's url
95+
* @return string
96+
*/
97+
public function getLargeThumbnail()
98+
{
99+
//Since this service does not provide other thumbnails sizes we just return the default size
100+
return $this->getThumbnail(self::THUMBNAIL_DEFAULT);
101+
}
102+
103+
/**
104+
* Returns the largest thumnbnaail's url
105+
* @return string
106+
*/
107+
public function getLargestThumbnail()
108+
{
109+
//Since this service does not provide other thumbnails sizes we just return the default size
110+
return $this->getThumbnail(self::THUMBNAIL_DEFAULT);
111+
}
112+
113+
/**
114+
* @param bool $autoplay
115+
* @return string
116+
*/
117+
public function getEmbedUrl($autoplay = false)
118+
{
119+
return '//www.dailymotion.com/embed/video/' . $this->videoId . ($autoplay ? '?amp&autoplay=1' : '');
120+
}
121+
122+
/**
123+
* @return bool
124+
*/
125+
public function isEmbeddable()
126+
{
127+
return true;
128+
}
129+
}
130+
```
131+
## The DailyMotionServiceAdapterFactory
132+
```php
133+
<?php
134+
namespace MyVendor\ServiceAdapter\Factory;
135+
136+
137+
use MyVendor\ServiceAdapter\DailyMotionServiceAdapter;
138+
use RicardoFiorani\Adapter\VideoAdapterInterface;
139+
use RicardoFiorani\Renderer\EmbedRendererInterface;
140+
141+
class DailyMotionServiceAdapterFactory implements \RicardoFiorani\Adapter\Factory\CallableServiceAdapterFactoryInterface
142+
{
143+
/**
144+
* @param string $url
145+
* @param string $pattern
146+
* @param EmbedRendererInterface $renderer
147+
* @return VideoAdapterInterface
148+
*/
149+
public function __invoke($url, $pattern, EmbedRendererInterface $renderer)
150+
{
151+
$dailyMotionServiceAdapter = new DailyMotionServiceAdapter($url, $pattern, $renderer);
152+
153+
return $dailyMotionServiceAdapter;
154+
}
155+
}
156+
```
157+
158+
## Making things work (gluing all together)
159+
160+
```php
161+
<?php
162+
use RicardoFiorani\Detector\VideoServiceDetector;
163+
164+
require __DIR__ . '/vendor/autoload.php';
165+
166+
$vsd = new VideoServiceDetector();
167+
//The Service Name
168+
$serviceName = 'DailyMotion';
169+
170+
//The Pattern used to identify this service
171+
//You can use multiple, but make sure your ServiceAdapter can handle it properly
172+
$patterns = array(
173+
'#https?://www.dailymotion.com/video/([A-Za-z0-9]+)#s'
174+
);
175+
176+
//Register the new service
177+
$vsd->registerService($serviceName, $patterns, "\\MyVendor\\ServiceAdapter\\Factory\\DailyMotionServiceAdapterFactory");
178+
179+
//This will get you an DailyMotionServiceAdapter
180+
$video = $vsd->parse('http://www.dailymotion.com/video/x33ncwc_kittens-fight-in-tiny-boxing-ring_animals');
181+
182+
```

0 commit comments

Comments
 (0)