|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of php-simple-conversion. |
|
4
|
|
|
* |
|
5
|
|
|
* php-simple-conversion is free software: you can redistribute it and/or modify |
|
6
|
|
|
* it under the terms of the GNU Lesser General Public License as published by |
|
7
|
|
|
* the Free Software Foundation, either version 3 of the License, or |
|
8
|
|
|
* (at your option) any later version. |
|
9
|
|
|
* |
|
10
|
|
|
* php-simple-conversion is distributed in the hope that it will be useful, |
|
11
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
13
|
|
|
* GNU General Public License for more details. |
|
14
|
|
|
* |
|
15
|
|
|
* You should have received a copy of the GNU General Public License |
|
16
|
|
|
* along with php-simple-conversion. If not, see <http://www.gnu.org/licenses/>. |
|
17
|
|
|
*/ |
|
18
|
|
|
|
|
19
|
|
|
namespace Mcustiel\Conversion; |
|
20
|
|
|
|
|
21
|
|
|
use Mcustiel\Conversion\Exception\ConverterDoesNotExistException; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Singleton class the holds all the registered converters, and allows to access them. |
|
25
|
|
|
* |
|
26
|
|
|
* @author mcustiel |
|
27
|
|
|
*/ |
|
28
|
|
|
class SimpleConverterContainer implements ConverterContainer |
|
29
|
|
|
{ |
|
30
|
|
|
/** |
|
31
|
|
|
* @var ConverterBuilder|Converter[][] |
|
32
|
|
|
*/ |
|
33
|
|
|
private $converters = []; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Registers a converter. |
|
37
|
|
|
* |
|
38
|
|
|
* @param ConverterBuilder $converterBuilder the builder of the converter to register |
|
39
|
|
|
* |
|
40
|
|
|
* @throws \InvalidArgumentException if from or to are unset |
|
41
|
|
|
*/ |
|
42
|
10 |
|
public function addConverter(ConverterBuilder $converterBuilder) |
|
43
|
|
|
{ |
|
44
|
10 |
|
$this->validateBuilder($converterBuilder); |
|
45
|
8 |
|
if (!isset($this->converters[$converterBuilder->getFrom()])) { |
|
46
|
7 |
|
$this->converters[$converterBuilder->getFrom()] = []; |
|
47
|
7 |
|
} |
|
48
|
8 |
|
$this->converters[$converterBuilder->getFrom()][$converterBuilder->getTo()] = $converterBuilder; |
|
49
|
8 |
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Access the implementation of the converter for the given from and to parameters. |
|
53
|
|
|
* |
|
54
|
|
|
* @param string $from the type from which the converter converts |
|
55
|
|
|
* @param string $to the type to which the converter converts to |
|
56
|
|
|
* |
|
57
|
|
|
* @throws \Mcustiel\Conversion\Exception\ConverterDoesNotExistException |
|
58
|
|
|
* |
|
59
|
|
|
* @return Converter |
|
60
|
|
|
*/ |
|
61
|
9 |
|
public function getConverter($from, $to) |
|
62
|
|
|
{ |
|
63
|
9 |
|
if (!isset($this->converters[$from][$to])) { |
|
64
|
1 |
|
throw new ConverterDoesNotExistException( |
|
65
|
1 |
|
"Converter from {$from} to {$to} does not exist" |
|
66
|
1 |
|
); |
|
67
|
|
|
} |
|
68
|
8 |
|
$converter = $this->converters[$from][$to]; |
|
69
|
8 |
|
if ($converter instanceof ConverterBuilder) { |
|
70
|
8 |
|
$this->converters[$from][$to] = $converter->getConverter(); |
|
71
|
5 |
|
} |
|
72
|
|
|
|
|
73
|
5 |
|
return $this->converters[$from][$to]; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* @param converter |
|
78
|
|
|
* @param mixed $converter |
|
79
|
|
|
*/ |
|
80
|
10 |
|
private function validateBuilder($converter) |
|
81
|
|
|
{ |
|
82
|
10 |
|
if (empty($converter->getFrom())) { |
|
83
|
1 |
|
throw new \InvalidArgumentException('From is unset'); |
|
84
|
|
|
} |
|
85
|
9 |
|
if (empty($converter->getTo())) { |
|
86
|
1 |
|
throw new \InvalidArgumentException('To is unset'); |
|
87
|
|
|
} |
|
88
|
8 |
|
} |
|
89
|
|
|
} |
|
90
|
|
|
|