1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* This file is part of the 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; |
11
|
|
|
|
12
|
|
|
use PommProject\Foundation\Session\ResultHandler; |
13
|
|
|
use PommProject\Foundation\Session\Session as BaseSession; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* ConvertedResultIterator |
17
|
|
|
* |
18
|
|
|
* Extends result iterator to add type conversion. |
19
|
|
|
* |
20
|
|
|
* @package Foundation |
21
|
|
|
* @copyright 2014 - 2017 Grégoire HUBERT |
22
|
|
|
* @author Grégoire HUBERT |
23
|
|
|
* @license X11 {@link http://opensource.org/licenses/mit-license.php} |
24
|
|
|
* @see ResultIterator |
25
|
|
|
*/ |
26
|
|
|
class ConvertedResultIterator extends ResultIterator |
27
|
|
|
{ |
28
|
|
|
protected $types = []; |
29
|
|
|
protected $session; |
30
|
|
|
protected $converters = []; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @param ResultHandler $result |
34
|
|
|
* @param BaseSession $session |
35
|
|
|
*/ |
36
|
|
|
public function __construct(ResultHandler $result, BaseSession $session) |
37
|
|
|
{ |
38
|
|
|
parent::__construct($result); |
39
|
|
|
$this->session = $session; |
40
|
|
|
$this->initTypes(); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* get |
45
|
|
|
* |
46
|
|
|
* Return a particular result. An array with converted values is returned. |
47
|
|
|
* pg_fetch_array is muted because it produces untrappable warnings on |
48
|
|
|
* errors. |
49
|
|
|
* |
50
|
|
|
* @param integer $index |
51
|
|
|
* @return array |
52
|
|
|
*/ |
53
|
|
|
public function get($index) |
54
|
|
|
{ |
55
|
|
|
return $this->parseRow(parent::get($index)); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* initTypes |
60
|
|
|
* |
61
|
|
|
* Get the result types from the result handler. |
62
|
|
|
* |
63
|
|
|
* @return ResultIterator $this |
64
|
|
|
*/ |
65
|
|
|
protected function initTypes() |
66
|
|
|
{ |
67
|
|
|
foreach ($this->result->getFieldNames() as $index => $name) { |
68
|
|
|
$type = $this->result->getFieldType($name); |
69
|
|
|
|
70
|
|
|
if ($type === null) { |
71
|
|
|
$type = 'text'; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
$this->types[$name] = $type; |
75
|
|
|
$this->converters[$name] = $this |
76
|
|
|
->session |
77
|
|
|
->getClientUsingPooler('converter', $type) |
78
|
|
|
; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
return $this; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* parseRow |
86
|
|
|
* |
87
|
|
|
* Convert values from Pg. |
88
|
|
|
* |
89
|
|
|
* @param array $values |
90
|
|
|
* @return mixed |
91
|
|
|
*/ |
92
|
|
|
protected function parseRow(array $values) |
93
|
|
|
{ |
94
|
|
|
$output_values = []; |
95
|
|
|
|
96
|
|
|
foreach ($values as $name => $value) { |
97
|
|
|
$output_values[$name] = |
98
|
|
|
$this->convertField($name, $value) ; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
return $output_values; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* convertField |
106
|
|
|
* |
107
|
|
|
* Return converted value for a result field. |
108
|
|
|
* |
109
|
|
|
* @param string $name |
110
|
|
|
* @param string $value |
111
|
|
|
* @return mixed |
112
|
|
|
*/ |
113
|
|
|
protected function convertField($name, $value) |
114
|
|
|
{ |
115
|
|
|
return $this |
116
|
|
|
->converters[$name] |
117
|
|
|
->fromPg($value, $this->types[$name]) |
118
|
|
|
; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* slice |
123
|
|
|
* |
124
|
|
|
* see @ResultIterator |
125
|
|
|
*/ |
126
|
|
|
public function slice($name) |
127
|
|
|
{ |
128
|
|
|
$values = []; |
129
|
|
|
foreach (parent::slice($name) as $value) { |
130
|
|
|
$values[] = $this->convertField($name, $value); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
return $values; |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
|