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\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
|
12 |
|
public function __construct(array $configurations = array()) |
31
|
|
|
{ |
32
|
12 |
|
$this->configurations = array(); |
33
|
|
|
|
34
|
12 |
|
foreach ($configurations as $configuration) { |
35
|
4 |
|
$this->add($configuration); |
36
|
12 |
|
} |
37
|
12 |
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* {@inheritdoc} |
41
|
|
|
*/ |
42
|
4 |
|
public function add(Configuration $configuration) |
43
|
|
|
{ |
44
|
4 |
|
$this->configurations[] = $configuration; |
45
|
4 |
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* {@inheritdoc} |
49
|
|
|
*/ |
50
|
4 |
|
public function all(array $filter = array()) |
51
|
|
|
{ |
52
|
4 |
|
if (count($filter) === 0) { |
53
|
2 |
|
return $this->configurations; |
54
|
|
|
} |
55
|
|
|
|
56
|
2 |
|
return $this->filter($this->configurations, $filter); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* {@inheritdoc} |
61
|
|
|
*/ |
62
|
2 |
|
public function getIterator() |
63
|
|
|
{ |
64
|
2 |
|
return new \ArrayIterator($this->configurations); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Get configurations that matches given filter criteria. |
69
|
|
|
* |
70
|
|
|
* @param Configuration[] $configurations Configurations to filter. |
71
|
|
|
* @param array $criteria Filter criteria. |
72
|
|
|
* @return Configuration[] Matched configurations. |
73
|
|
|
*/ |
74
|
2 |
|
private function filter($configurations, array $criteria) |
75
|
|
|
{ |
76
|
2 |
|
$result = array(); |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @var Configuration $configuration |
80
|
|
|
*/ |
81
|
2 |
|
foreach ($configurations as $configuration) { |
82
|
|
|
|
83
|
2 |
|
if (ConfigurationFilterUtil::matches($configuration, $criteria)) { |
84
|
2 |
|
$result[] = $configuration; |
85
|
2 |
|
} |
86
|
2 |
|
} |
87
|
|
|
|
88
|
2 |
|
return $result; |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|