1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* This file is part of the Pomm 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
|
|
|
* PgComposite |
16
|
|
|
* |
17
|
|
|
* Composite type converter. |
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 PgComposite extends ArrayTypeConverter |
26
|
|
|
{ |
27
|
|
|
protected $structure; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* __construct |
31
|
|
|
* |
32
|
|
|
* Takes the composite type structure as parameter. |
33
|
|
|
* The structure is $name => $type. |
34
|
|
|
* |
35
|
|
|
* @param array $structure structure definition. |
36
|
|
|
*/ |
37
|
|
|
public function __construct(array $structure) |
38
|
|
|
{ |
39
|
|
|
$this->structure = $structure; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* fromPg |
44
|
|
|
* |
45
|
|
|
* @see ConverterInterface |
46
|
|
|
*/ |
47
|
|
|
public function fromPg($data, $type, Session $session) |
48
|
|
|
{ |
49
|
|
|
if ($data === null) { |
50
|
|
|
return null; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
$values = str_getcsv(stripcslashes(trim($data, '()'))); |
54
|
|
|
|
55
|
|
|
return $this->convertArray(array_combine(array_keys($this->structure), $values), $session, 'fromPg'); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* toPg |
60
|
|
|
* |
61
|
|
|
* @see ConverterInterface |
62
|
|
|
*/ |
63
|
|
|
public function toPg($data, $type, Session $session) |
64
|
|
|
{ |
65
|
|
|
if ($data === null) { |
66
|
|
|
return sprintf("NULL::%s", $type); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
$this->checkArray($data); |
70
|
|
|
|
71
|
|
|
return sprintf( |
72
|
|
|
"ROW(%s)::%s", |
73
|
|
|
join(',', $this->convertArray($data, $session, 'toPg')), |
74
|
|
|
$type |
75
|
|
|
); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* toPgStandardFormat |
80
|
|
|
* |
81
|
|
|
* @see ConverterInterface |
82
|
|
|
*/ |
83
|
|
|
public function toPgStandardFormat($data, $type, Session $session) |
84
|
|
|
{ |
85
|
|
|
if ($data === null) { |
86
|
|
|
return null; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
$this->checkArray($data); |
90
|
|
|
|
91
|
|
|
return |
92
|
|
|
sprintf( |
93
|
|
|
"(%s)", |
94
|
|
|
join(',', array_map(function ($val) { |
95
|
|
|
if ($val === null) { |
96
|
|
|
return ''; |
97
|
|
|
} elseif ($val === '') { |
98
|
|
|
return '""'; |
99
|
|
|
} elseif (preg_match('/[,\s]/', $val)) { |
100
|
|
|
return sprintf('"%s"', str_replace('"', '""', $val)); |
101
|
|
|
} else { |
102
|
|
|
return $val; |
103
|
|
|
} |
104
|
|
|
}, $this->convertArray($data, $session, 'toPgStandardFormat'))) |
105
|
|
|
); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* convertArray |
110
|
|
|
* |
111
|
|
|
* Convert the given array of values. |
112
|
|
|
* |
113
|
|
|
* @param array $data |
114
|
|
|
* @param Session $session |
115
|
|
|
* @param string $method |
116
|
|
|
* @return array |
117
|
|
|
*/ |
118
|
|
|
private function convertArray(array $data, Session $session, $method) |
119
|
|
|
{ |
120
|
|
|
$values = []; |
121
|
|
|
|
122
|
|
|
foreach ($this->structure as $name => $subtype) { |
123
|
|
|
$values[$name] = isset($data[$name]) |
124
|
|
|
? $this->getSubtypeConverter($subtype, $session) |
125
|
|
|
->$method($data[$name], $subtype, $session) |
126
|
|
|
: $this->getSubtypeConverter($subtype, $session) |
127
|
|
|
->$method(null, $subtype, $session) |
128
|
|
|
; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
return $values; |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
|