|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace RicardoFiorani\Matcher; |
|
4
|
|
|
|
|
5
|
|
|
use RicardoFiorani\Adapter\VideoAdapterInterface; |
|
6
|
|
|
use RicardoFiorani\Container\Factory\ServicesContainerFactory; |
|
7
|
|
|
use RicardoFiorani\Container\ServicesContainer; |
|
8
|
|
|
use RicardoFiorani\Exception\ServiceNotAvailableException; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* @author Ricardo Fiorani |
|
12
|
|
|
*/ |
|
13
|
|
|
class VideoServiceMatcher |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* @var ServicesContainer |
|
17
|
|
|
*/ |
|
18
|
|
|
private $serviceContainer; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @var array |
|
22
|
|
|
*/ |
|
23
|
|
|
private $parsedUrls = array(); |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* VideoServiceMatcher constructor. |
|
27
|
|
|
*/ |
|
28
|
|
|
public function __construct() |
|
29
|
|
|
{ |
|
30
|
|
|
$this->serviceContainer = ServicesContainerFactory::createNewServiceMatcher(); |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @param string $url |
|
35
|
|
|
* |
|
36
|
|
|
* @return VideoAdapterInterface |
|
37
|
|
|
* |
|
38
|
|
|
* @throws ServiceNotAvailableException |
|
39
|
|
|
*/ |
|
40
|
|
|
public function parse($url) |
|
41
|
|
|
{ |
|
42
|
|
|
if (isset($this->parsedUrls[$url])) { |
|
43
|
|
|
return $this->parsedUrls[$url]; |
|
44
|
|
|
} |
|
45
|
|
|
/** @var array $patterns */ |
|
46
|
|
|
/** @var string $serviceName */ |
|
47
|
|
|
foreach ($this->getServiceContainer()->getPatterns() as $serviceName => $patterns) { |
|
48
|
|
|
/** @var string $pattern */ |
|
49
|
|
|
foreach ($patterns as $pattern) { |
|
50
|
|
|
if (false != preg_match($pattern, $url)) { |
|
|
|
|
|
|
51
|
|
|
$factory = $this->getServiceContainer()->getFactory($serviceName); |
|
52
|
|
|
|
|
53
|
|
|
return $this->parsedUrls[$url] = $factory($url, $pattern, |
|
54
|
|
|
$this->getServiceContainer()->getRenderer()); |
|
55
|
|
|
} |
|
56
|
|
|
} |
|
57
|
|
|
} |
|
58
|
|
|
throw new ServiceNotAvailableException(sprintf('The url "%s" could not be parsed by any of the services available.', |
|
59
|
|
|
$url)); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* @return ServicesContainer |
|
64
|
|
|
*/ |
|
65
|
|
|
public function getServiceContainer() |
|
66
|
|
|
{ |
|
67
|
|
|
return $this->serviceContainer; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* @param ServicesContainer $serviceContainer |
|
72
|
|
|
*/ |
|
73
|
|
|
public function setServiceContainer(ServicesContainer $serviceContainer) |
|
74
|
|
|
{ |
|
75
|
|
|
$this->serviceContainer = $serviceContainer; |
|
76
|
|
|
} |
|
77
|
|
|
} |
|
78
|
|
|
|