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