1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* This file is part of Pomm's Foundation package. |
4
|
|
|
* |
5
|
|
|
* (c) 2014 - 2017 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 - 2017 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
|
|
|
* @param string $type |
33
|
|
|
* @return string |
34
|
|
|
*/ |
35
|
|
|
public static function getSubType($type) |
36
|
|
|
{ |
37
|
|
|
if (preg_match('/^(.+)\[\]$/', $type, $matches) || preg_match('/^_(.+)$/', $type, $matches)) { |
38
|
|
|
return $matches[1]; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
return $type; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @see ConverterInterface |
46
|
|
|
*/ |
47
|
|
|
public function fromPg($data, $type, Session $session) |
48
|
|
|
{ |
49
|
|
|
if ($data === null || $data === 'NULL') { |
50
|
|
|
return null; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
$type = static::getSubType($type); |
54
|
|
|
|
55
|
|
|
if ($data !== "{}") { |
56
|
|
|
$converter = $this->getSubtypeConverter($type, $session); |
57
|
|
|
|
58
|
|
|
return array_map(function ($val) use ($converter, $type, $session) { |
59
|
|
|
if ($val !== "NULL") { |
60
|
|
|
return preg_match('/\\\\/', $val) |
61
|
|
|
? $converter->fromPg(stripslashes($val), $type, $session) |
62
|
|
|
: $converter->fromPg($val, $type, $session); |
63
|
|
|
} else { |
64
|
|
|
return null; |
65
|
|
|
} |
66
|
|
|
}, str_getcsv(trim($data, "{}"))); |
67
|
|
|
} else { |
68
|
|
|
return []; |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @see ConverterInterface |
74
|
|
|
*/ |
75
|
|
|
public function toPg($data, $type, Session $session) |
76
|
|
|
{ |
77
|
|
|
$type = static::getSubType($type); |
78
|
|
|
|
79
|
|
|
if ($data === null) { |
80
|
|
|
return sprintf("NULL::%s[]", $type); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
$converter = $this->getSubtypeConverter($type, $session); |
84
|
|
|
$data = $this->checkArray($data); |
85
|
|
|
|
86
|
|
|
return sprintf('ARRAY[%s]::%s[]', join(',', array_map(function ($val) use ($converter, $type, $session) { |
87
|
|
|
return $converter->toPg($val, $type, $session); |
88
|
|
|
}, $data)), $type); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @see ConverterInterface |
93
|
|
|
*/ |
94
|
|
|
public function toPgStandardFormat($data, $type, Session $session) |
95
|
|
|
{ |
96
|
|
|
if ($data === null) { |
97
|
|
|
return null; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
$type = static::getSubType($type); |
101
|
|
|
$converter = $this->getSubtypeConverter($type, $session); |
102
|
|
|
$data = $this->checkArray($data); |
103
|
|
|
|
104
|
|
|
return sprintf('{%s}', join( |
105
|
|
|
',', |
106
|
|
|
array_map(function ($val) use ($converter, $type, $session) { |
107
|
|
|
if ($val === null) { |
108
|
|
|
return 'NULL'; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
$val = $converter->toPgStandardFormat($val, $type, $session); |
112
|
|
|
|
113
|
|
|
if ($val !== '') { |
114
|
|
|
if (preg_match('/[,\\"\s]/', $val)) { |
115
|
|
|
$val = sprintf('"%s"', addcslashes($val, '"\\')); |
116
|
|
|
} |
117
|
|
|
} else { |
118
|
|
|
$val = '""'; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
return $val; |
122
|
|
|
}, $data) |
123
|
|
|
)); |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
|