PgHstore::buildArray()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 18
rs 9.6666
c 0
b 0
f 0
cc 3
nc 3
nop 1
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\Exception\ConverterException;
13
use PommProject\Foundation\Session\Session;
14
15
/**
16
 * PgHStore
17
 *
18
 * HStore converter
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       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)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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
     * @param  array $data
77
     * @return array
78
     */
79
    protected function buildArray(array $data)
80
    {
81
        $insert_values = [];
82
83
        foreach ($data as $key => $value) {
84
            if ($value === null) {
85
                $insert_values[] = sprintf('%s => NULL', $this->escape($key));
86
            } else {
87
                $insert_values[] = sprintf(
88
                    '%s => %s',
89
                    $this->escape($key),
90
                    $this->escape($value)
91
                );
92
            }
93
        }
94
95
        return $insert_values;
96
    }
97
98
    /**
99
     * escape
100
     *
101
     * Escape a string.
102
     *
103
     * @param  string $string
104
     * @return string
105
     */
106
    protected function escape($string)
107
    {
108
        if (preg_match('/["\s,]/', $string) === false) {
109
            return addslashes($string);
110
        } else {
111
            return sprintf('"%s"', addcslashes($string, '"\\'));
112
        }
113
    }
114
}
115