1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Hateoas\UrlGenerator; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* @author Adrien Brault <[email protected]> |
7
|
|
|
*/ |
8
|
|
|
class UrlGeneratorRegistry |
9
|
|
|
{ |
10
|
|
|
const DEFAULT_URL_GENERATOR_KEY = 'default'; |
11
|
|
|
|
12
|
|
|
private $urlGenerators; |
13
|
|
|
|
14
|
|
|
public function __construct(UrlGeneratorInterface $defaultUrlGenerator = null) |
15
|
|
|
{ |
16
|
|
|
$this->urlGenerators = array(); |
17
|
|
|
|
18
|
|
|
if (null !== $defaultUrlGenerator) { |
19
|
|
|
$this->urlGenerators = array( |
20
|
|
|
self::DEFAULT_URL_GENERATOR_KEY => $defaultUrlGenerator, |
21
|
|
|
); |
22
|
|
|
} |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @param string|null $name If null it will return the default url generator |
27
|
|
|
* |
28
|
|
|
* @return UrlGeneratorInterface |
29
|
|
|
*/ |
30
|
|
|
public function get($name = null) |
31
|
|
|
{ |
32
|
|
|
if (null === $name) { |
33
|
|
|
$name = self::DEFAULT_URL_GENERATOR_KEY; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
if (!isset($this->urlGenerators[$name])) { |
37
|
|
|
throw new \InvalidArgumentException( |
38
|
|
|
sprintf( |
39
|
|
|
'The "%s" url generator is not set. Available url generators are: %s.', |
40
|
|
|
$name, |
41
|
|
|
join(', ', array_keys($this->urlGenerators)) |
42
|
|
|
) |
43
|
|
|
); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
return $this->urlGenerators[$name]; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @param string|null $name |
51
|
|
|
* @param UrlGeneratorInterface $urlGenerator |
52
|
|
|
*/ |
53
|
|
|
public function set($name, UrlGeneratorInterface $urlGenerator) |
54
|
|
|
{ |
55
|
|
|
if (null === $name) { |
56
|
|
|
$name = self::DEFAULT_URL_GENERATOR_KEY; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
$this->urlGenerators[$name] = $urlGenerator; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @return boolean |
64
|
|
|
*/ |
65
|
|
|
public function hasGenerators() |
66
|
|
|
{ |
67
|
|
|
return count($this->urlGenerators) > 0; |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|