Skip to content

Commit bc9561b

Browse files
a lot of fixes. almost ready to go live
1 parent c136bd7 commit bc9561b

11 files changed

+148
-22
lines changed

config/config.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,13 @@
1414
'patterns' => array(
1515
'#(https?://vimeo.com)/([0-9]+)#i'
1616
),
17-
'factory' => 'RicardoFiorani\\Container\\Factory\\VimeoVideoContainerFactory',
17+
'factory' => 'RicardoFiorani\\Adapter\\Vimeo\\Factory\\VimeoServiceAdapterFactory',
1818
),
1919
'Youtube' => array(
2020
'patterns' => array(
2121
'#(?:<\>]+href=\")?(?:http://)?((?:[a-zA-Z]{1,4}\.)?youtube.com/(?:watch)?\?v=(.{11}?))[^"]*(?:\"[^\<\>]*>)?([^\<\>]*)(?:)?#',
2222
'%(?:youtube\.com/(?:[^/]+/.+/|(?:v|e(?:mbed)?)/|.*[?&]v=)|youtu\.be/)([^"&?/ ]{11})%i',
2323
),
24-
'factory' => 'RicardoFiorani\\Container\\Factory\\YoutubeVideoContainerFactory',
24+
'factory' => 'RicardoFiorani\\Container\\Youtube\\Factory\\YoutubeVideoContainerFactory',
2525
),
2626
);

src/Adapter/AbstractServiceContainer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ abstract class AbstractServiceAdapter implements VideoAdapterInterface
2525
* AbstractVideoAdapter constructor.
2626
* @param string $url
2727
*/
28-
public function __construct($url, $regex)
28+
public function __construct($url, $pattern)
2929
{
3030
$this->rawUrl = $url;
3131
}

src/Adapter/Factory/CallableFactoryInterface.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ interface CallableFactoryInterface
1515
{
1616
/**
1717
* @param string $url
18-
* @param string $regex
18+
* @param string $pattern
1919
* @return VideoAdapterInterface
2020
*/
21-
public function __invoke($url, $regex);
21+
public function __invoke($url, $pattern);
2222
}

src/Adapter/VideoAdapterInterface.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,9 @@ interface VideoAdapterInterface
1313
{
1414
/**
1515
* @param string $url
16+
* @param string $pattern
1617
*/
17-
public function __construct($url, $regex);
18+
public function __construct($url, $pattern);
1819

1920
/**
2021
* Returns the service name (ie: "Youtube" or "Vimeo")

src/Adapter/Vimeo/Factory/VimeoServiceAdapterFactory.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,13 @@ class VimeoServiceAdapterFactory implements CallableFactoryInterface
1616
{
1717
/**
1818
* @param string $url
19-
* @param string $regex
19+
* @param string $pattern
2020
* @return VimeoServiceAdapter
2121
*/
22-
public function __invoke($url, $regex)
22+
public function __invoke($url, $pattern)
2323
{
24-
$vimeoAdapter = new VimeoServiceAdapter($url,$regex);
25-
return $vimeoAdapter;
24+
$adapter = new VimeoServiceAdapter($url, $pattern);
25+
26+
return $adapter;
2627
}
2728
}

src/Adapter/Vimeo/VimeoServiceAdapter.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@ class VimeoServiceAdapter extends AbstractServiceAdapter
2020
const THUMBNAIL_LARGE = 'thumbnail_large';
2121

2222

23-
public function __construct($url, $regex)
23+
public function __construct($url, $pattern)
2424
{
2525
$match = array();
26-
preg_match($regex, $url, $match);
26+
preg_match($pattern, $url, $match);
2727
/*Gets the Video ID*/
2828
$videoId = $match[2];
2929
if (empty($videoId)) {
@@ -46,7 +46,7 @@ public function __construct($url, $regex)
4646
$this->setTitle($data['title']);
4747
$this->setDescription($data['description']);
4848

49-
return parent::__construct($url, $regex);
49+
return parent::__construct($url, $pattern);
5050
}
5151

5252
/**

src/Adapter/Youtube/Factory/YoutubeServiceAdapterFactory.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,13 @@ class YoutubeServiceAdapterFactory implements CallableFactoryInterface
1717

1818
/**
1919
* @param string $url
20-
* @param string $regex
20+
* @param string $pattern
2121
* @return YoutubeServiceAdapter
2222
*/
23-
public function __invoke($url, $regex)
23+
public function __invoke($url, $pattern)
2424
{
25-
$youtubeVideoAdapter = new YoutubeServiceAdapter($url, $regex);
25+
$adapter = new YoutubeServiceAdapter($url, $pattern);
2626

27-
return $youtubeVideoAdapter;
27+
return $adapter;
2828
}
2929
}

src/Adapter/Youtube/YoutubeServiceAdapter.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,16 @@ class YoutubeServiceAdapter extends AbstractServiceAdapter
2222
const THUMBNAIL_MAX_QUALITY = 'maxresdefault';
2323

2424

25-
public function __construct($url,$regex){
26-
preg_match($regex, $url, $match);
25+
public function __construct($url, $pattern)
26+
{
27+
preg_match($pattern, $url, $match);
2728
$videoId = $match[2];
2829
if (empty($videoId)) {
2930
$videoId = $match[1];
3031
}
31-
$this->videoId($videoId);
32-
return parent::__construct($url,$regex);
32+
$this->setVideoId($videoId);
33+
34+
return parent::__construct($url, $pattern);
3335
}
3436

3537
/**

src/Detector/VideoDetector.php

Lines changed: 89 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,104 @@
22

33
namespace RicardoFiorani\Detector;
44

5+
use RicardoFiorani\Adapter\VideoAdapterInterface;
6+
use RicardoFiorani\Exception\DuplicatedServiceNameException;
7+
use RicardoFiorani\Exception\ServiceNotAvailableException;
8+
59
/**
610
* @author Ricardo Fiorani
711
*/
812
class VideoDetector
913
{
14+
/**
15+
* @var array
16+
*/
17+
private $services = array();
18+
19+
/**
20+
* @var array
21+
*/
22+
private $patterns = array();
23+
24+
/**
25+
* @var
26+
*/
27+
private $factories = array();
28+
1029
/**
1130
* VideoDetector constructor.
1231
*/
1332
public function __construct()
1433
{
15-
34+
$this->loadConfigFile();
35+
}
36+
37+
/**
38+
* Loads de default config file
39+
* @throws DuplicatedServiceNameException
40+
*/
41+
private function loadConfigFile()
42+
{
43+
$configFile = require __DIR__ . '../../config/config.php';
44+
foreach ($configFile as $serviceName => $serviceConfig) {
45+
$this->registerService($serviceName, $serviceConfig['patterns'], $serviceConfig['factory']);
46+
}
47+
}
48+
49+
/**
50+
* Register a Service
51+
* @param string $serviceName
52+
* @param array $regex
53+
* @param string|callable $factory
54+
* @throws DuplicatedServiceNameException
55+
*/
56+
public function registerService($serviceName, array $regex, $factory)
57+
{
58+
if ($this->hasService($serviceName)) {
59+
throw new DuplicatedServiceNameException();
60+
}
61+
62+
$this->services[] = $serviceName;
63+
$this->patterns[$serviceName] = $regex;
64+
$this->factories[$serviceName] = $factory;
65+
}
66+
67+
/**
68+
* @return array
69+
*/
70+
public function getServiceNameList()
71+
{
72+
return $this->services;
1673
}
74+
75+
76+
/**
77+
* @param string $serviceName
78+
* @return bool
79+
*/
80+
public function hasService($serviceName)
81+
{
82+
return in_array($serviceName, $this->services);
83+
}
84+
85+
/**
86+
* @param string $url
87+
* @return VideoAdapterInterface
88+
* @throws ServiceNotAvailableException
89+
*/
90+
public function parse($url)
91+
{
92+
/** @var array $patterns */
93+
/** @var string $serviceName */
94+
foreach ($this->patterns as $serviceName => $patterns) {
95+
/** @var string $pattern */
96+
foreach ($patterns as $pattern) {
97+
if (true === preg_match($pattern, $url)) {
98+
return $this->factories[$serviceName]($url, $pattern);
99+
}
100+
}
101+
}
102+
throw new ServiceNotAvailableException();
103+
}
104+
17105
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: Ricardo Fiorani
5+
* Date: 29/08/2015
6+
* Time: 23:28
7+
*/
8+
9+
namespace RicardoFiorani\Exception;
10+
11+
12+
use Exception;
13+
14+
class DuplicatedServiceNameException extends Exception
15+
{
16+
17+
}

0 commit comments

Comments
 (0)