|
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
|
|
|
/** |
|
22
|
|
|
* Singleton class the holds all the registered converters, and allows to access them. |
|
23
|
|
|
* |
|
24
|
|
|
* @author mcustiel |
|
25
|
|
|
*/ |
|
26
|
|
|
class SingletonConverterContainer implements ConverterContainer |
|
27
|
|
|
{ |
|
28
|
|
|
/** |
|
29
|
|
|
* @var ConverterContainer |
|
30
|
|
|
*/ |
|
31
|
|
|
private static $instance; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @var SimpleConverterContainer |
|
35
|
|
|
*/ |
|
36
|
|
|
private $container; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* This is singleton, can't be instantiated directly. |
|
40
|
|
|
*/ |
|
41
|
1 |
|
private function __construct() |
|
42
|
|
|
{ |
|
43
|
1 |
|
$this->container = new SimpleConverterContainer(); |
|
44
|
1 |
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Get the instance of this class. |
|
48
|
|
|
* |
|
49
|
|
|
* @return \Mcustiel\Conversion\ConverterContainer |
|
50
|
|
|
*/ |
|
51
|
6 |
|
public static function getInstance() |
|
52
|
|
|
{ |
|
53
|
6 |
|
if (self::$instance === null) { |
|
54
|
1 |
|
self::$instance = new self(); |
|
55
|
1 |
|
} |
|
56
|
|
|
|
|
57
|
6 |
|
return self::$instance; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Registers a converter. |
|
62
|
|
|
* |
|
63
|
|
|
* @param ConverterBuilder $converter the builder of the converter to register |
|
64
|
|
|
* |
|
65
|
|
|
* @throws \InvalidArgumentException if from or to are unset |
|
66
|
|
|
*/ |
|
67
|
5 |
|
public function addConverter(ConverterBuilder $converter) |
|
68
|
|
|
{ |
|
69
|
5 |
|
$this->container->addConverter($converter); |
|
70
|
3 |
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Access the implementation of the converter for the given from and to parameters. |
|
74
|
|
|
* |
|
75
|
|
|
* @param string $from the type from which the converter converts |
|
76
|
|
|
* @param string $to the type to which the converter converts to |
|
77
|
|
|
* |
|
78
|
|
|
* @throws \Mcustiel\Conversion\Exception\ConverterDoesNotExistException |
|
79
|
|
|
* |
|
80
|
|
|
* @return Converter |
|
81
|
|
|
*/ |
|
82
|
4 |
|
public function getConverter($from, $to) |
|
83
|
|
|
{ |
|
84
|
4 |
|
return $this->container->getConverter($from, $to); |
|
85
|
|
|
} |
|
86
|
|
|
} |
|
87
|
|
|
|