1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* This file is part of Pomm's Foundation package. |
4
|
|
|
* |
5
|
|
|
* (c) 2014 - 2015 Grégoire HUBERT <[email protected]> |
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 PommProject\Foundation\Converter; |
11
|
|
|
|
12
|
|
|
use PommProject\Foundation\Exception\ConverterException; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* ConverterHolder |
16
|
|
|
* |
17
|
|
|
* Responsible of holding all converters associated with their corresponding |
18
|
|
|
* types. |
19
|
|
|
* |
20
|
|
|
* @package Foundation |
21
|
|
|
* @copyright 2014 - 2015 Grégoire HUBERT |
22
|
|
|
* @author Grégoire HUBERT |
23
|
|
|
* @license X11 {@link http://opensource.org/licenses/mit-license.php} |
24
|
|
|
*/ |
25
|
|
|
class ConverterHolder |
26
|
|
|
{ |
27
|
|
|
protected $converters = []; |
28
|
|
|
protected $types = []; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* registerConverter |
32
|
|
|
* |
33
|
|
|
* Declare a converter and assign types to it. |
34
|
|
|
* |
35
|
|
|
* @access public |
36
|
|
|
* @param string $name |
37
|
|
|
* @param ConverterInterface $converter |
38
|
|
|
* @param array $types |
39
|
|
|
* @param bool $strict |
40
|
|
|
* @return ConverterHolder $this |
41
|
|
|
*/ |
42
|
|
|
public function registerConverter($name, ConverterInterface $converter, array $types, $strict = null) |
43
|
|
|
{ |
44
|
|
|
$this->addConverter($name, $converter, $strict); |
45
|
|
|
|
46
|
|
|
foreach ($types as $type) { |
47
|
|
|
$this->addTypeToConverter($name, $type); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
return $this; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* addConverter |
55
|
|
|
* |
56
|
|
|
* Add a converter with a new name. If strict is set to true and the |
57
|
|
|
* converter for this type has already been registered, then it throws and |
58
|
|
|
* exception. |
59
|
|
|
* |
60
|
|
|
* @access protected |
61
|
|
|
* @param string $name |
62
|
|
|
* @param ConverterInterface $converter |
63
|
|
|
* @param bool $strict (default true) |
64
|
|
|
* @throws ConverterException if $name already exists and strict. |
65
|
|
|
* @return ConverterHolder $this |
66
|
|
|
*/ |
67
|
|
|
protected function addConverter($name, ConverterInterface $converter, $strict = null) |
68
|
|
|
{ |
69
|
|
|
$strict = $strict === null ? true : (bool) $strict; |
70
|
|
|
|
71
|
|
View Code Duplication |
if ($strict && $this->hasConverterName($name)) { |
72
|
|
|
throw new ConverterException( |
73
|
|
|
sprintf( |
74
|
|
|
"A converter named '%s' already exists. (Known converters are {%s}).", |
75
|
|
|
$name, |
76
|
|
|
join(', ', $this->getConverterNames()) |
77
|
|
|
) |
78
|
|
|
); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
$this->converters[$name] = $converter; |
82
|
|
|
|
83
|
|
|
return $this; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* hasConverterName |
88
|
|
|
* |
89
|
|
|
* Tell if the converter exists or not. |
90
|
|
|
* |
91
|
|
|
* @access public |
92
|
|
|
* @param string $name |
93
|
|
|
* @return bool |
94
|
|
|
*/ |
95
|
|
|
public function hasConverterName($name) |
96
|
|
|
{ |
97
|
|
|
return (bool) isset($this->converters[$name]); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* getConverter |
102
|
|
|
* |
103
|
|
|
* Return the converter associated with this name. If no converters found, |
104
|
|
|
* NULL is returned. |
105
|
|
|
* |
106
|
|
|
* @access public |
107
|
|
|
* @param string $name |
108
|
|
|
* @return ConverterInterface |
109
|
|
|
*/ |
110
|
|
|
public function getConverter($name) |
111
|
|
|
{ |
112
|
|
|
return $this->hasConverterName($name) ? $this->converters[$name] : null; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* getConverterNames |
117
|
|
|
* |
118
|
|
|
* Returns an array with the names of the registered converters. |
119
|
|
|
* |
120
|
|
|
* @access public |
121
|
|
|
* @return array |
122
|
|
|
*/ |
123
|
|
|
public function getConverterNames() |
124
|
|
|
{ |
125
|
|
|
return array_keys($this->converters); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* addTypeToConverter |
130
|
|
|
* |
131
|
|
|
* Make the given converter to support a new PostgreSQL type. If the given |
132
|
|
|
* type is already defined, it is overrided with the new converter. |
133
|
|
|
* |
134
|
|
|
* @access public |
135
|
|
|
* @param string $name |
136
|
|
|
* @param string $type |
137
|
|
|
* @throws ConverterException if $name does not exist. |
138
|
|
|
* @return ConverterHolder $this |
139
|
|
|
*/ |
140
|
|
|
public function addTypeToConverter($name, $type) |
141
|
|
|
{ |
142
|
|
View Code Duplication |
if (!$this->hasConverterName($name)) { |
143
|
|
|
throw new ConverterException( |
144
|
|
|
sprintf( |
145
|
|
|
"No such converter name '%s'. Registered converters are {%s}.", |
146
|
|
|
$name, |
147
|
|
|
join(', ', $this->getConverterNames()) |
148
|
|
|
) |
149
|
|
|
); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
$this->types[$type] = $name; |
153
|
|
|
|
154
|
|
|
return $this; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* getConverterForType |
159
|
|
|
* |
160
|
|
|
* Returns the converter instance for the given type. |
161
|
|
|
* |
162
|
|
|
* @access public |
163
|
|
|
* @param string $type |
164
|
|
|
* @throws ConverterException if there are no converters associated. |
165
|
|
|
* @return ConverterInterface |
166
|
|
|
*/ |
167
|
|
|
public function getConverterForType($type) |
168
|
|
|
{ |
169
|
|
|
if (!$this->hasType($type)) { |
170
|
|
|
throw new ConverterException( |
171
|
|
|
sprintf( |
172
|
|
|
"No converters associated with type '%s'. Handled types are {%s}.", |
173
|
|
|
$type, |
174
|
|
|
join(', ', $this->getTypes()) |
175
|
|
|
) |
176
|
|
|
); |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
return $this->converters[$this->types[$type]]; |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* hasType |
184
|
|
|
* |
185
|
|
|
* Does the type exist ? |
186
|
|
|
* |
187
|
|
|
* @access public |
188
|
|
|
* @param string $type |
189
|
|
|
* @return bool |
190
|
|
|
*/ |
191
|
|
|
public function hasType($type) |
192
|
|
|
{ |
193
|
|
|
return (bool) isset($this->types[$type]); |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* getTypes |
198
|
|
|
* |
199
|
|
|
* Return the list of handled types. |
200
|
|
|
* |
201
|
|
|
* @access public |
202
|
|
|
* @return array |
203
|
|
|
*/ |
204
|
|
|
public function getTypes() |
205
|
|
|
{ |
206
|
|
|
return array_keys($this->types); |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* getTypesWithConverterName |
211
|
|
|
* |
212
|
|
|
* Return the list of types with the related converter name. |
213
|
|
|
* |
214
|
|
|
* @access public |
215
|
|
|
* @return array |
216
|
|
|
*/ |
217
|
|
|
public function getTypesWithConverterName() |
218
|
|
|
{ |
219
|
|
|
return $this->types; |
220
|
|
|
} |
221
|
|
|
} |
222
|
|
|
|