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
|
|
|
use RunOpenCode\ExchangeRate\Exception\SourceNotAvailableException; |
15
|
|
|
use RunOpenCode\ExchangeRate\Utils\SourceFilterUtil; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Class SourcesRegistry |
19
|
|
|
* |
20
|
|
|
* Default implementation of sources registry. |
21
|
|
|
* |
22
|
|
|
* @package RunOpenCode\ExchangeRate\Registry |
23
|
|
|
*/ |
24
|
|
|
final class SourcesRegistry implements SourcesRegistryInterface |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* @var SourceInterface[] |
28
|
|
|
*/ |
29
|
|
|
private $sources; |
30
|
|
|
|
31
|
10 |
|
public function __construct(array $sources = array()) |
32
|
|
|
{ |
33
|
10 |
|
$this->sources = array(); |
34
|
|
|
|
35
|
10 |
|
foreach ($sources as $source) { |
36
|
10 |
|
$this->add($source); |
37
|
10 |
|
} |
38
|
10 |
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* {@inheritdoc} |
42
|
|
|
*/ |
43
|
10 |
|
public function add(SourceInterface $source) |
44
|
|
|
{ |
45
|
10 |
|
if ($this->has($source->getName())) { |
46
|
2 |
|
throw new \RuntimeException(sprintf('Source "%s" is already registered.', $source->getName())); |
47
|
|
|
} |
48
|
|
|
|
49
|
10 |
|
$this->sources[$source->getName()] = $source; |
50
|
10 |
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* {@inheritdoc} |
54
|
|
|
*/ |
55
|
10 |
|
public function has($name) |
56
|
|
|
{ |
57
|
10 |
|
return array_key_exists($name, $this->sources); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* {@inheritdoc} |
62
|
|
|
* |
63
|
|
|
* @throws SourceNotAvailableException |
64
|
|
|
*/ |
65
|
4 |
|
public function get($name) |
66
|
|
|
{ |
67
|
4 |
|
if ($this->has($name)) { |
68
|
2 |
|
return $this->sources[$name]; |
69
|
|
|
} |
70
|
|
|
|
71
|
2 |
|
throw new SourceNotAvailableException(sprintf('Unknown source requested: "%s".', $name)); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* {@inheritdoc} |
76
|
|
|
*/ |
77
|
4 |
|
public function all(array $filter = array()) |
78
|
|
|
{ |
79
|
4 |
|
if (count($filter) === 0) { |
80
|
2 |
|
return $this->sources; |
81
|
|
|
} |
82
|
|
|
|
83
|
2 |
|
return $this->filter($this->sources, $filter); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* {@inheritdoc} |
88
|
|
|
*/ |
89
|
2 |
|
public function getIterator() |
90
|
|
|
{ |
91
|
2 |
|
return new \ArrayIterator($this->sources); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Filter sources. |
96
|
|
|
* |
97
|
|
|
* Available filter criteria: |
98
|
|
|
* * name: string |
99
|
|
|
* * names: string[] |
100
|
|
|
* |
101
|
|
|
* @param SourceInterface[] $sources Sources to filter. |
102
|
|
|
* @param array $filters Filter criteria. |
103
|
|
|
* |
104
|
|
|
* @return SourceInterface[] |
105
|
|
|
*/ |
106
|
2 |
|
private function filter($sources, array $filters = array()) |
107
|
|
|
{ |
108
|
2 |
|
$result = array(); |
109
|
|
|
|
110
|
2 |
|
foreach ($sources as $source) { |
111
|
|
|
|
112
|
2 |
|
if (SourceFilterUtil::matches($source, $filters)) { |
113
|
2 |
|
$result[] = $source; |
114
|
2 |
|
} |
115
|
2 |
|
} |
116
|
|
|
|
117
|
2 |
|
return $result; |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|