1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* This file is part of the Exchange Rate package, an RunOpenCode project. |
4
|
|
|
* |
5
|
|
|
* (c) 2016 RunOpenCode |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
namespace RunOpenCode\ExchangeRate\Registry; |
11
|
|
|
|
12
|
|
|
use RunOpenCode\ExchangeRate\Contract\SourceInterface; |
13
|
|
|
use RunOpenCode\ExchangeRate\Contract\SourcesRegistryInterface; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Class SourcesRegistry |
17
|
|
|
* |
18
|
|
|
* Default implementation of sources registry. |
19
|
|
|
* |
20
|
|
|
* @package RunOpenCode\ExchangeRate\Registry |
21
|
|
|
*/ |
22
|
|
|
final class SourcesRegistry implements SourcesRegistryInterface |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* @var SourceInterface[] |
26
|
|
|
*/ |
27
|
|
|
private $sources; |
28
|
|
|
|
29
|
|
|
public function __construct(array $sources = array()) |
30
|
|
|
{ |
31
|
|
|
$this->sources = array(); |
32
|
|
|
|
33
|
|
|
foreach ($sources as $source) { |
34
|
|
|
$this->add($source); |
35
|
|
|
} |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* {@inheritdoc} |
40
|
|
|
*/ |
41
|
|
|
public function add(SourceInterface $source) |
42
|
|
|
{ |
43
|
|
|
if ($this->has($source->getName())) { |
44
|
|
|
throw new \RuntimeException(sprintf('Source "%s" is already registered.', $source->getName())); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
$this->sources[$source->getName()] = $source; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* {@inheritdoc} |
52
|
|
|
*/ |
53
|
|
|
public function has($name) |
54
|
|
|
{ |
55
|
|
|
return array_key_exists($name, $this->sources); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* {@inheritdoc} |
60
|
|
|
*/ |
61
|
|
|
public function get($name) |
62
|
|
|
{ |
63
|
|
|
return $this->sources[$name]; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* {@inheritdoc} |
68
|
|
|
*/ |
69
|
|
|
public function all() |
70
|
|
|
{ |
71
|
|
|
return $this->sources; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* {@inheritdoc} |
76
|
|
|
*/ |
77
|
|
|
public function getIterator() |
78
|
|
|
{ |
79
|
|
|
return new \ArrayIterator($this->sources); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Filter sources. |
84
|
|
|
* |
85
|
|
|
* @param SourcesRegistryInterface $registry Sources collection. |
86
|
|
|
* @param string|array|null $names Source names to keep. |
87
|
|
|
* @return SourceInterface[] |
88
|
|
|
*/ |
89
|
|
|
public static function filter(SourcesRegistryInterface $registry, $names = null) |
90
|
|
|
{ |
91
|
|
|
if ($names === null) { |
92
|
|
|
return $registry->all(); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
if (!is_array($names)) { |
96
|
|
|
$names = array($names); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
if (count($names) === 0) { |
100
|
|
|
throw new \InvalidArgumentException('You have to provide either name of source to keep, of array of names of sources to keep.'); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
$result = array(); |
104
|
|
|
|
105
|
|
|
foreach ($names as $name) { |
106
|
|
|
$result[] = $registry->get($name); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
return $result; |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|