|
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
|
|
|
use PommProject\Foundation\Session\Session; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* ArrayTypeConverter |
|
17
|
|
|
* |
|
18
|
|
|
* Array sub class for converters using a PHP array representation. |
|
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
|
|
|
* @see ConverterInterface |
|
25
|
|
|
* @abstract |
|
26
|
|
|
*/ |
|
27
|
|
|
abstract class ArrayTypeConverter implements ConverterInterface |
|
28
|
|
|
{ |
|
29
|
|
|
protected $converters = []; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* checkArray |
|
33
|
|
|
* |
|
34
|
|
|
* Check if the data is an array. |
|
35
|
|
|
* |
|
36
|
|
|
* @access protected |
|
37
|
|
|
* @param mixed $data |
|
38
|
|
|
* @throws ConverterException |
|
39
|
|
|
* @return array $data |
|
40
|
|
|
*/ |
|
41
|
|
|
protected function checkArray($data) |
|
42
|
|
|
{ |
|
43
|
|
|
if (!is_array($data)) { |
|
44
|
|
|
throw new ConverterException( |
|
45
|
|
|
sprintf( |
|
46
|
|
|
"Array converter data must be an array ('%s' given).", |
|
47
|
|
|
gettype($data) |
|
48
|
|
|
) |
|
49
|
|
|
); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
return $data; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* getSubtypeConverter |
|
57
|
|
|
* |
|
58
|
|
|
* Since the arrays in PostgreSQL have the same subtype, it is useful to |
|
59
|
|
|
* cache it here to avoid summoning the ClientHolder all the time. |
|
60
|
|
|
* |
|
61
|
|
|
* @access protected |
|
62
|
|
|
* @param string $type |
|
63
|
|
|
* @param Session $session |
|
64
|
|
|
* @return ConverterInterface |
|
65
|
|
|
*/ |
|
66
|
|
|
protected function getSubtypeConverter($type, Session $session) |
|
67
|
|
|
{ |
|
68
|
|
View Code Duplication |
if (!isset($this->converters[$type])) { |
|
69
|
|
|
$this->converters[$type] = $session |
|
70
|
|
|
->getClientUsingPooler('converter', $type) |
|
71
|
|
|
->getConverter() |
|
72
|
|
|
; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
return $this->converters[$type]; |
|
76
|
|
|
} |
|
77
|
|
|
} |
|
78
|
|
|
|