Passed
Branch master (e86b58)
by Martin
02:18
created

transformPostgresTextArrayToPHPArray()   B

Complexity

Conditions 9
Paths 1

Size

Total Lines 38
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 21
c 1
b 0
f 0
dl 0
loc 38
rs 8.0555
cc 9
nc 1
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace MartinGeorgiev\Utils;
6
7
/**
8
 * Util class with helpers for working with PostgreSql data structures.
9
 *
10
 * @since 0.9
11
 *
12
 * @author Martin Georgiev <[email protected]>
13
 */
14
class DataStructure
15
{
16
    /**
17
     * This method supports only single-dimensioned text arrays and
18
     * relays on the default escaping strategy in PostgreSql (double quotes).
19
     */
20
    public static function transformPostgresTextArrayToPHPArray(string $postgresArray): array
21
    {
22
        $transform = static function (string $textArrayToTransform): array {
23
            $indicatesMultipleDimensions = \mb_strpos($textArrayToTransform, '},{') !== false
24
                || \mb_strpos($textArrayToTransform, '{{') === 0;
25
            if ($indicatesMultipleDimensions) {
26
                throw new \InvalidArgumentException('Only single-dimensioned arrays are supported');
27
            }
28
29
            $phpArray = \str_getcsv(\trim($textArrayToTransform, '{}'));
30
            foreach ($phpArray as $i => $text) {
31
                if ($text === null) {
32
                    unset($phpArray[$i]);
33
34
                    break;
35
                }
36
37
                $isInteger = \is_numeric($text) && ''.(int) $text === $text;
38
                if ($isInteger) {
39
                    $phpArray[$i] = (int) $text;
40
41
                    continue;
42
                }
43
44
                $isFloat = \is_numeric($text) && ''.(float) $text === $text;
45
                if ($isFloat) {
46
                    $phpArray[$i] = (float) $text;
47
48
                    continue;
49
                }
50
51
                $phpArray[$i] = \stripslashes(\str_replace('\"', '"', $text));
52
            }
53
54
            return $phpArray;
55
        };
56
57
        return $transform($postgresArray);
58
    }
59
60
    /**
61
     * This method supports only single-dimensioned PHP arrays.
62
     * This method relays on the default escaping strategy in PostgreSql (double quotes).
63
     *
64
     * @see https://stackoverflow.com/a/5632171/3425372 Kudos to jmz for the inspiration
65
     */
66
    public static function transformPHPArrayToPostgresTextArray(array $phpArray): string
67
    {
68
        $transform = static function (array $phpArrayToTransform): string {
69
            $result = [];
70
            foreach ($phpArrayToTransform as $text) {
71
                if (\is_array($text)) {
72
                    throw new \InvalidArgumentException('Only single-dimensioned arrays are supported');
73
                }
74
75
                if (\is_numeric($text) || \ctype_digit($text)) {
76
                    $escapedText = $text;
77
                } else {
78
                    $escapedText = \sprintf('"%s"', \addcslashes($text, '"\\'));
79
                }
80
                $result[] = $escapedText;
81
            }
82
83
            return '{'.\implode(',', $result).'}';
84
        };
85
86
        return $transform($phpArray);
87
    }
88
}
89