Completed
Push — 2.1 ( b44a46...4c2160 )
by
unknown
12:30
created

ArrayParser   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 93
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 20
lcom 1
cbo 0
dl 0
loc 93
ccs 39
cts 39
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A parse() 0 12 3
C parseArray() 0 26 7
C parseString() 0 24 10
1
<?php
2
/**
3
 * @link http://www.yiiframework.com/
4
 * @copyright Copyright (c) 2008 Yii Software LLC
5
 * @license http://www.yiiframework.com/license/
6
 */
7
8
namespace yii\db\pgsql;
9
10
/**
11
 * The class converts PostgreSQL array representation to PHP array
12
 *
13
 * @author Sergei Tigrov <[email protected]>
14
 * @author Dmytro Naumenko <[email protected]>
15
 * @since 2.0.14
16
 */
17
class ArrayParser
18
{
19
    /**
20
     * @var string Character used in array
21
     */
22
    private $delimiter = ',';
23
24
25
    /**
26
     * Convert array from PostgreSQL to PHP
27
     *
28
     * @param string $value string to be converted
29
     * @return array|null
30
     */
31 21
    public function parse($value)
32
    {
33 21
        if ($value === null) {
34 9
            return null;
35
        }
36
37 12
        if ($value === '{}') {
38 1
            return [];
39
        }
40
41 11
        return $this->parseArray($value);
42
    }
43
44
    /**
45
     * Pares PgSQL array encoded in string
46
     *
47
     * @param string $value
48
     * @param int $i parse starting position
49
     * @return array
50
     */
51 11
    private function parseArray($value, &$i = 0)
52
    {
53 11
        $result = [];
54 11
        $len = strlen($value);
55 11
        for (++$i; $i < $len; ++$i) {
56 11
            switch ($value[$i]) {
57 11
                case '{':
58 4
                    $result[] = $this->parseArray($value, $i);
59 4
                    break;
60 11
                case '}':
61 10
                    break 2;
62 11
                case $this->delimiter:
63 11
                    if (empty($result)) { // `{}` case
64 2
                        $result[] = null;
65
                    }
66 11
                    if (in_array($value[$i + 1], [$this->delimiter, '}'], true)) { // `{,}` case
67 5
                        $result[] = null;
68
                    }
69 11
                    break;
70
                default:
71 9
                    $result[] = $this->parseString($value, $i);
72
            }
73
        }
74
75 11
        return $result;
76
    }
77
78
    /**
79
     * Parses PgSQL encoded string
80
     *
81
     * @param string $value
82
     * @param int $i parse starting position
83
     * @return null|string
84
     */
85 9
    private function parseString($value, &$i)
86
    {
87 9
        $isQuoted = $value[$i] === '"';
88 9
        $stringEndChars = $isQuoted ? ['"'] : [$this->delimiter, '}'];
89 9
        $result = '';
90 9
        $len = strlen($value);
91 9
        for ($i += $isQuoted ? 1 : 0; $i < $len; ++$i) {
92 9
            if (in_array($value[$i], ['\\', '"'], true) && in_array($value[$i + 1], [$value[$i], '"'], true)) {
93 4
                ++$i;
94 9
            } elseif (in_array($value[$i], $stringEndChars, true)) {
95 9
                break;
96
            }
97
98 9
            $result .= $value[$i];
99
        }
100
101 9
        $i -= $isQuoted ? 0 : 1;
102
103 9
        if (!$isQuoted && $result === 'NULL') {
104 3
            $result = null;
105
        }
106
107 9
        return $result;
108
    }
109
}
110