Completed
Push — 2.0 ( 143803...4e64fa )
by grégoire
08:42 queued 04:22
created

PgComposite::toPgStandardFormat()   B

Complexity

Conditions 5
Paths 2

Size

Total Lines 24
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 24
rs 8.5125
cc 5
eloc 16
nc 2
nop 3
1
<?php
2
/*
3
 * This file is part of the Pomm 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
 * PgComposite
16
 *
17
 *  Composite type converter.
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 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
     * @access public
36
     * @param array $structure structure definition.
37
     */
38
    public function __construct(array $structure)
39
    {
40
        $this->structure = $structure;
41
    }
42
43
    /**
44
     * fromPg
45
     *
46
     * @see ConverterInterface
47
     */
48
    public function fromPg($data, $type, Session $session)
49
    {
50
        if ($data === null) {
51
            return null;
52
        }
53
54
        $values = str_getcsv(stripcslashes(trim($data, '()')));
55
56
        return $this->convertArray(array_combine(array_keys($this->structure), $values), $session, 'fromPg');
57
    }
58
59
    /**
60
     * toPg
61
     *
62
     * @see ConverterInterface
63
     */
64
    public function toPg($data, $type, Session $session)
65
    {
66
        if ($data === null) {
67
            return sprintf("NULL::%s", $type);
68
        }
69
70
        $this->checkArray($data);
71
72
        return sprintf(
73
            "ROW(%s)::%s",
74
            join(',', $this->convertArray($data, $session, 'toPg')),
75
            $type
76
        );
77
    }
78
79
    /**
80
     * toPgStandardFormat
81
     *
82
     * @see ConverterInterface
83
     */
84
    public function toPgStandardFormat($data, $type, Session $session)
85
    {
86
        if ($data === null) {
87
            return null;
88
        }
89
90
        $this->checkArray($data);
91
92
        return
93
            sprintf("(%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
    /**
110
     * convertArray
111
     *
112
     * Convert the given array of values.
113
     *
114
     * @access private
115
     * @param  array $data
116
     * @param  Session $session
117
     * @param  string $method
118
     * @return array
119
     */
120
    private function convertArray(array $data, Session $session, $method)
121
    {
122
        $values = [];
123
124
        foreach ($this->structure as $name => $subtype) {
125
            $values[$name] = isset($data[$name])
126
                ? $this->getSubtypeConverter($subtype, $session)
127
                    ->$method($data[$name], $subtype, $session)
128
                : $this->getSubtypeConverter($subtype, $session)
129
                    ->$method(null, $subtype, $session)
130
                ;
131
        }
132
133
        return $values;
134
    }
135
}
136