|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
|
|
3
|
|
|
namespace Personnage\Tinkoff\SDK; |
|
4
|
|
|
|
|
5
|
|
|
use Psr\Http\Message\MessageInterface; |
|
6
|
|
|
use Psr\Http\Message\StreamInterface; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Parse a stream to array. |
|
10
|
|
|
* |
|
11
|
|
|
* @param StreamInterface $stream |
|
12
|
|
|
* @return array |
|
13
|
|
|
*/ |
|
14
|
|
|
function stream_decode(StreamInterface $stream): array |
|
15
|
|
|
{ |
|
16
|
63 |
|
return json_decode((string) $stream, true); |
|
17
|
|
|
} |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Get all contents from message instance. |
|
21
|
|
|
* |
|
22
|
|
|
* @param MessageInterface $message |
|
23
|
|
|
* @return array |
|
24
|
|
|
*/ |
|
25
|
|
|
function message_get_body(MessageInterface $message): array |
|
26
|
|
|
{ |
|
27
|
63 |
|
return stream_decode($message->getBody()); |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Interpolates context values into the message placeholders. |
|
32
|
|
|
* |
|
33
|
|
|
* @param string $message |
|
34
|
|
|
* @param array $context |
|
35
|
|
|
* @return string |
|
36
|
|
|
*/ |
|
37
|
|
|
function interpolate(string $message, array $context = []): string |
|
38
|
|
|
{ |
|
39
|
|
|
$replace = []; |
|
40
|
|
|
foreach ($context as $key => $val) { |
|
41
|
|
|
$replace['%' . $key . '%'] = $val; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
return strtr($message, $replace); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Encode values for transfer by http. |
|
49
|
|
|
* |
|
50
|
|
|
* @param string|array $values |
|
51
|
|
|
* @param string $separator |
|
52
|
|
|
* @return string |
|
53
|
|
|
*/ |
|
54
|
|
|
function pack($values, string $separator = '|'): string |
|
55
|
|
|
{ |
|
56
|
|
|
if (is_array($values)) { |
|
57
|
|
|
return http_build_query($values, '', $separator); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
return urlencode(urldecode($values)); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Decode data after receive. |
|
65
|
|
|
* |
|
66
|
|
|
* @param string|null $data |
|
67
|
|
|
* @param string $separator |
|
68
|
|
|
* @return array |
|
69
|
|
|
*/ |
|
70
|
|
|
function unpack($data, string $separator = '|'): array |
|
71
|
|
|
{ |
|
72
|
|
|
if (empty($data)) { |
|
73
|
|
|
return []; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
$coercion = function ($value) { |
|
77
|
|
|
switch (strtolower($value)) { |
|
78
|
|
|
case 'y': |
|
79
|
|
|
case 'true': |
|
80
|
|
|
return true; |
|
81
|
|
|
case 'n': |
|
82
|
|
|
case 'false': |
|
83
|
|
|
return false; |
|
84
|
|
|
case 'null': |
|
85
|
|
|
return null; |
|
86
|
|
|
default: |
|
87
|
|
|
return $value; |
|
88
|
|
|
} |
|
89
|
|
|
}; |
|
90
|
|
|
|
|
91
|
|
|
$values = []; |
|
92
|
|
|
|
|
93
|
|
|
foreach (explode($separator, urldecode($data)) as $value) { |
|
94
|
|
|
list($k, $v) = explode('=', $value); |
|
95
|
|
|
|
|
96
|
|
|
$values[$k] = $coercion($v); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
return $values; |
|
100
|
|
|
} |
|
101
|
|
|
|