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
|
7 |
|
public static function transformPostgresTextArrayToPHPArray(string $postgresArray): array |
21
|
|
|
{ |
22
|
7 |
|
$transform = static function (string $textArrayToTransform): array { |
23
|
7 |
|
$indicatesMultipleDimensions = \mb_strpos($textArrayToTransform, '},{') !== false |
24
|
7 |
|
|| \mb_strpos($textArrayToTransform, '{{') === 0; |
25
|
7 |
|
if ($indicatesMultipleDimensions) { |
26
|
1 |
|
throw new \InvalidArgumentException('Only single-dimensioned arrays are supported'); |
27
|
|
|
} |
28
|
|
|
|
29
|
6 |
|
$phpArray = \str_getcsv(\trim($textArrayToTransform, '{}'), escape: '\\'); |
30
|
6 |
|
foreach ($phpArray as $i => $text) { |
31
|
6 |
|
if ($text === null) { |
32
|
1 |
|
unset($phpArray[$i]); |
33
|
|
|
|
34
|
1 |
|
break; |
35
|
|
|
} |
36
|
|
|
|
37
|
5 |
|
$isInteger = \is_numeric($text) && ''.(int) $text === $text; |
38
|
5 |
|
if ($isInteger) { |
39
|
3 |
|
$phpArray[$i] = (int) $text; |
40
|
|
|
|
41
|
3 |
|
continue; |
42
|
|
|
} |
43
|
|
|
|
44
|
4 |
|
$isFloat = \is_numeric($text) && ''.(float) $text === $text; |
45
|
4 |
|
if ($isFloat) { |
46
|
1 |
|
$phpArray[$i] = (float) $text; |
47
|
|
|
|
48
|
1 |
|
continue; |
49
|
|
|
} |
50
|
|
|
|
51
|
3 |
|
$phpArray[$i] = \stripslashes(\str_replace('\"', '"', $text)); |
52
|
|
|
} |
53
|
|
|
|
54
|
6 |
|
return $phpArray; |
55
|
7 |
|
}; |
56
|
|
|
|
57
|
7 |
|
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
|
7 |
|
public static function transformPHPArrayToPostgresTextArray(array $phpArray): string |
67
|
|
|
{ |
68
|
7 |
|
$transform = static function (array $phpArrayToTransform): string { |
69
|
7 |
|
$result = []; |
70
|
7 |
|
foreach ($phpArrayToTransform as $text) { |
71
|
6 |
|
if (\is_array($text)) { |
72
|
1 |
|
throw new \InvalidArgumentException('Only single-dimensioned arrays are supported'); |
73
|
|
|
} |
74
|
|
|
|
75
|
5 |
|
if (\is_numeric($text) || \ctype_digit($text)) { |
76
|
4 |
|
$escapedText = $text; |
77
|
|
|
} else { |
78
|
3 |
|
\assert(\is_string($text)); |
79
|
3 |
|
$escapedText = \sprintf('"%s"', \addcslashes($text, '"\\')); |
80
|
|
|
} |
81
|
|
|
|
82
|
5 |
|
$result[] = $escapedText; |
83
|
|
|
} |
84
|
|
|
|
85
|
6 |
|
return '{'.\implode(',', $result).'}'; |
86
|
7 |
|
}; |
87
|
|
|
|
88
|
7 |
|
return $transform($phpArray); |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|