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\Session\Session; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* PgArray |
16
|
|
|
* |
17
|
|
|
* Converter for arrays. |
18
|
|
|
* |
19
|
|
|
* @package Foundation |
20
|
|
|
* @copyright 2014 - 2015 Grégoire HUBERT |
21
|
|
|
* @author Grégoire HUBERT |
22
|
|
|
* @license X11 {@link http://opensource.org/licenses/mit-license.php} |
23
|
|
|
* @see ArrayTypeConverter |
24
|
|
|
*/ |
25
|
|
|
class PgArray extends ArrayTypeConverter |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* getSubType |
29
|
|
|
* |
30
|
|
|
* Extract subtype from a formatted string (ie int4[] or _text). |
31
|
|
|
* |
32
|
|
|
* @static |
33
|
|
|
* @access public |
34
|
|
|
* @param string $type |
35
|
|
|
* @return string |
36
|
|
|
*/ |
37
|
|
|
public static function getSubType($type) |
38
|
|
|
{ |
39
|
|
|
if (preg_match('/^(.+)\[\]$/', $type, $matches) || preg_match('/^_(.+)$/', $type, $matches)) { |
40
|
|
|
return $matches[1]; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
return $type; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @see ConverterInterface |
48
|
|
|
*/ |
49
|
|
|
public function fromPg($data, $type, Session $session) |
50
|
|
|
{ |
51
|
|
|
if ($data === null || $data === 'NULL') { |
52
|
|
|
return null; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
$type = static::getSubType($type); |
56
|
|
|
|
57
|
|
|
if ($data !== "{}") { |
58
|
|
|
$converter = $this->getSubtypeConverter($type, $session); |
59
|
|
|
|
60
|
|
|
return array_map(function ($val) use ($converter, $type, $session) { |
61
|
|
|
if ($val !== "NULL") { |
62
|
|
|
return preg_match('/\\\\/', $val) |
63
|
|
|
? $converter->fromPg(stripslashes($val), $type, $session) |
64
|
|
|
: $converter->fromPg($val, $type, $session); |
65
|
|
|
} else { |
66
|
|
|
return null; |
67
|
|
|
} |
68
|
|
|
}, str_getcsv(trim($data, "{}"))); |
69
|
|
|
} else { |
70
|
|
|
return []; |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @see ConverterInterface |
76
|
|
|
*/ |
77
|
|
|
public function toPg($data, $type, Session $session) |
78
|
|
|
{ |
79
|
|
|
$type = static::getSubType($type); |
80
|
|
|
|
81
|
|
|
if ($data === null) { |
82
|
|
|
return sprintf("NULL::%s[]", $type); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
$converter = $this->getSubtypeConverter($type, $session); |
86
|
|
|
$data = $this->checkArray($data); |
87
|
|
|
|
88
|
|
|
return sprintf('ARRAY[%s]::%s[]', join(',', array_map(function ($val) use ($converter, $type, $session) { |
89
|
|
|
return $converter->toPg($val, $type, $session); |
90
|
|
|
}, $data)), $type); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @see ConverterInterface |
95
|
|
|
*/ |
96
|
|
|
public function toPgStandardFormat($data, $type, Session $session) |
97
|
|
|
{ |
98
|
|
|
if ($data === null) { |
99
|
|
|
return null; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
$type = static::getSubType($type); |
103
|
|
|
$converter = $this->getSubtypeConverter($type, $session); |
104
|
|
|
$data = $this->checkArray($data); |
105
|
|
|
|
106
|
|
|
return |
107
|
|
|
sprintf('{%s}', join(',', |
108
|
|
|
array_map(function ($val) use ($converter, $type, $session) { |
109
|
|
|
if ($val === null) { |
110
|
|
|
return 'NULL'; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
$val = $converter->toPgStandardFormat($val, $type, $session); |
114
|
|
|
|
115
|
|
|
if ($val !== '') { |
116
|
|
|
if (preg_match('/[,\\"\s]/', $val)) { |
117
|
|
|
$val = sprintf('"%s"', addcslashes($val, '"\\')); |
118
|
|
|
} |
119
|
|
|
} else { |
120
|
|
|
$val = '""'; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
return $val; |
124
|
|
|
}, $data) |
125
|
|
|
)); |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
|