Completed
Pull Request — 2.0 (#75)
by Julien
02:32
created

ConvertedResultIterator::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
c 0
b 0
f 0
rs 9.4285
cc 1
eloc 4
nc 1
nop 2
1
<?php
2
/*
3
 * This file is part of the 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;
11
12
use PommProject\Foundation\Session\ResultHandler;
13
use PommProject\Foundation\Session\Session as BaseSession;
14
15
/**
16
 * ConvertedResultIterator
17
 *
18
 * Iterator on converted results.
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       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
     * @access protected
64
     * @return ResultIterator $this
65
     */
66
    protected function initTypes()
67
    {
68
        foreach ($this->result->getFieldNames() as $index => $name) {
69
            $this->types[$index] = $this->result->getFieldType($name);
70
        }
71
72
        return $this;
73
    }
74
75
    /**
76
     * parseRow
77
     *
78
     * Convert values from Pg.
79
     *
80
     * @access protected
81
     * @param  array $values
82
     * @return mixed
83
     */
84
    protected function parseRow(array $values)
85
    {
86
        $output_values = [];
87
88
        foreach ($values as $name => $value) {
89
            $output_values[$name] =
90
                $this->convertField($name, $value) ;
91
        }
92
93
        return $output_values;
94
    }
95
96
    /**
97
     * convertField
98
     *
99
     * Return converted value for a result field.
100
     *
101
     * @access protected
102
     * @param  string $name
103
     * @param  string $value
104
     * @return mixed
105
     */
106
    protected function convertField($name, $value)
107
    {
108
        $type = $this->result->getFieldType($name);
109
110
        if ($type === null) {
111
            $type = 'text';
112
        }
113
114 View Code Duplication
        if (!isset($this->converters[$type])) {
115
            $this->converters[$type] = $this
116
            ->session
117
            ->getClientUsingPooler('converter', $type)
118
            ;
119
        }
120
121
        return $this
122
            ->converters[$type]
123
            ->fromPg($value, $type)
124
            ;
125
    }
126
127
    /**
128
     * slice
129
     *
130
     * see @ResultIterator
131
     */
132
    public function slice($name)
133
    {
134
        $values = [];
135
        foreach (parent::slice($name) as $value) {
136
            $values[] = $this->convertField($name, $value);
137
        }
138
139
        return $values;
140
    }
141
}
142