|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Njasm\Soundcloud\Factory; |
|
4
|
|
|
|
|
5
|
|
|
|
|
6
|
|
|
|
|
7
|
|
|
class Factory implements FactoryInterface |
|
8
|
|
|
{ |
|
9
|
|
|
private $map = array( |
|
10
|
|
|
'AuthInterface' => 'Njasm\\Soundcloud\\Auth\\Auth', |
|
11
|
|
|
'RequestInterface' => 'Njasm\\Soundcloud\\Request\\Request', |
|
12
|
|
|
'ResponseInterface' => 'Njasm\\Soundcloud\\Request\\Response', |
|
13
|
|
|
'ResourceInterface' => 'Njasm\\Soundcloud\\Resource\\Resource', |
|
14
|
|
|
'UrlBuilderInterface' => 'Njasm\\Soundcloud\\UrlBuilder\\UrlBuilder', |
|
15
|
|
|
'FactoryInterface' => 'Njasm\\Soundcloud\\Factory\\Factory' |
|
16
|
|
|
); |
|
17
|
|
|
|
|
18
|
1 |
|
public function register($interface, $class) |
|
19
|
|
|
{ |
|
20
|
1 |
|
$this->validate($interface); |
|
21
|
1 |
|
$this->map[$interface] = $class; |
|
22
|
|
|
|
|
23
|
1 |
|
return $this; |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @param string $interface |
|
28
|
|
|
* @param array $params |
|
29
|
|
|
* @return object |
|
30
|
|
|
*/ |
|
31
|
9 |
|
public function make($interface, array $params = array()) |
|
32
|
|
|
{ |
|
33
|
9 |
|
$this->validate($interface); |
|
34
|
|
|
|
|
35
|
8 |
|
if ($this->has($interface) === false) { |
|
36
|
1 |
|
throw new \InvalidArgumentException("You should register $interface in the Factory first."); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
7 |
|
$reflected = new \ReflectionClass($this->map[$interface]); |
|
40
|
|
|
|
|
41
|
7 |
|
if (empty($params)) { |
|
42
|
1 |
|
return $reflected->newInstanceArgs(); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
6 |
|
return $reflected->newInstanceArgs($params); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
10 |
|
public function has($interface) |
|
49
|
|
|
{ |
|
50
|
10 |
|
return isset($this->map[$interface]) === true ? true : false; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @param string $interface |
|
55
|
|
|
* @throws \InvalidArgumentException |
|
56
|
|
|
*/ |
|
57
|
10 |
|
private function validate($interface) |
|
58
|
|
|
{ |
|
59
|
10 |
|
if (empty($interface)) { |
|
60
|
1 |
|
throw new \InvalidArgumentException("Invalid interface requested"); |
|
61
|
|
|
} |
|
62
|
9 |
|
} |
|
63
|
|
|
} |
|
64
|
|
|
|