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

PgHstore::toPg()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 8
Ratio 100 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 8
loc 8
rs 9.4285
cc 2
eloc 4
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\Exception\ConverterException;
13
use PommProject\Foundation\Session\Session;
14
15
/**
16
 * PgHStore
17
 *
18
 * HStore converter
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       ArrayTypeConverter
25
 */
26
class PgHstore extends ArrayTypeConverter
27
{
28
    /**
29
     * @see \Pomm\Converter\ConverterInterface
30
     */
31
    public function fromPg($data, $type, Session $session)
32
    {
33
        if ($data ===  null) {
34
            return null;
35
        }
36
37
        $hstore = null;
38
        $code = @eval(sprintf("\$hstore = [%s];", $data));
39
40
        if ($code !== null || !is_array($hstore)) {
41
            throw new ConverterException(sprintf("Could not parse hstore string '%s' to array.", $data));
42
        }
43
44
        return $hstore;
45
    }
46
47
    /**
48
     * @see \Pomm\Converter\ConverterInterface
49
     */
50 View Code Duplication
    public function toPg($data, $type, Session $session)
51
    {
52
        if ($data === null) {
53
            return sprintf("NULL::%s", $type);
54
        }
55
56
        return sprintf("%s(\$hs\$%s\$hs\$)", $type, join(', ', $this->buildArray($this->checkArray($data))));
57
    }
58
59
    /**
60
     * @see \Pomm\Converter\ConverterInterface
61
     */
62 View Code Duplication
    public function toPgStandardFormat($data, $type, Session $session)
63
    {
64
        if ($data === null) {
65
            return null;
66
        }
67
68
        return sprintf('%s', join(', ', $this->buildArray($this->checkArray($data))));
69
    }
70
71
    /**
72
     * buildArray
73
     *
74
     * Return an array of HStore elements.
75
     *
76
     * @access   protected
77
     * @param  array $data
78
     * @return array
79
     */
80
    protected function buildArray(array $data)
81
    {
82
        $insert_values = [];
83
84
        foreach ($data as $key => $value) {
85
            if ($value === null) {
86
                $insert_values[] = sprintf('%s => NULL', $this->escape($key));
87
            } else {
88
                $insert_values[] = sprintf(
89
                    '%s => %s',
90
                    $this->escape($key),
91
                    $this->escape($value)
92
                );
93
            }
94
        }
95
96
        return $insert_values;
97
    }
98
99
    /**
100
     * escape
101
     *
102
     * Escape a string.
103
     *
104
     * @access protected
105
     * @param  string $string
106
     * @return string
107
     */
108
    protected function escape($string)
109
    {
110
        if (preg_match('/["\s,]/', $string) === false) {
111
            return addslashes($string);
112
        } else {
113
            return sprintf('"%s"', addcslashes($string, '"\\'));
114
        }
115
    }
116
}
117