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\Configuration; |
13
|
|
|
use RunOpenCode\ExchangeRate\Contract\RatesConfigurationRegistryInterface; |
14
|
|
|
use RunOpenCode\ExchangeRate\Utils\ConfigurationFilterUtil; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Class RatesConfigurationRegistry |
18
|
|
|
* |
19
|
|
|
* Default implementation of rates configuration registry. |
20
|
|
|
* |
21
|
|
|
* @package RunOpenCode\ExchangeRate\Registry |
22
|
|
|
*/ |
23
|
|
|
final class RatesConfigurationRegistry implements RatesConfigurationRegistryInterface |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* @var Configuration[] |
27
|
|
|
*/ |
28
|
|
|
private $configurations; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* RatesConfigurationRegistry constructor. |
32
|
|
|
* |
33
|
|
|
* @param Configuration[] $configurations |
34
|
|
|
*/ |
35
|
19 |
|
public function __construct(array $configurations = array()) |
36
|
|
|
{ |
37
|
19 |
|
$this->configurations = array(); |
38
|
|
|
|
39
|
19 |
|
foreach ($configurations as $configuration) { |
40
|
2 |
|
$this->add($configuration); |
41
|
|
|
} |
42
|
19 |
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* {@inheritdoc} |
46
|
|
|
*/ |
47
|
2 |
|
public function add(Configuration $configuration) |
48
|
|
|
{ |
49
|
2 |
|
$this->configurations[] = $configuration; |
50
|
2 |
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* {@inheritdoc} |
54
|
|
|
*/ |
55
|
2 |
|
public function all(array $filter = array()) |
56
|
|
|
{ |
57
|
2 |
|
if (count($filter) === 0) { |
58
|
1 |
|
return $this->configurations; |
59
|
|
|
} |
60
|
|
|
|
61
|
1 |
|
return $this->filter($this->configurations, $filter); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* {@inheritdoc} |
66
|
|
|
*/ |
67
|
1 |
|
public function getIterator() |
68
|
|
|
{ |
69
|
1 |
|
return new \ArrayIterator($this->configurations); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Get configurations that matches given filter criteria. |
74
|
|
|
* |
75
|
|
|
* @param Configuration[] $configurations Configurations to filter. |
76
|
|
|
* @param array $criteria Filter criteria. |
77
|
|
|
* @return Configuration[] Matched configurations. |
78
|
|
|
*/ |
79
|
1 |
|
private function filter($configurations, array $criteria) |
80
|
|
|
{ |
81
|
1 |
|
$result = array(); |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @var Configuration $configuration |
85
|
|
|
*/ |
86
|
1 |
|
foreach ($configurations as $configuration) { |
87
|
|
|
|
88
|
1 |
|
if (ConfigurationFilterUtil::matches($configuration, $criteria)) { |
89
|
1 |
|
$result[] = $configuration; |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|
93
|
1 |
|
return $result; |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|