forked from reactphp/http
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPostBufferedSink.php
More file actions
62 lines (52 loc) · 1.59 KB
/
Copy pathPostBufferedSink.php
File metadata and controls
62 lines (52 loc) · 1.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
<?php
namespace React\Http\StreamingBodyParser;
use React\Promise;
class PostBufferedSink
{
/**
* @param ParserInterface $parser
* @return Promise\PromiseInterface
*/
public static function createPromise(ParserInterface $parser)
{
$deferred = new Promise\Deferred();
$postFields = [];
$parser->on('post', function ($key, $value) use (&$postFields) {
self::extractPost($postFields, $key, $value);
});
$parser->on('end', function () use ($deferred, &$postFields) {
$deferred->resolve($postFields);
});
return $deferred->promise();
}
public static function extractPost(&$postFields, $key, $value)
{
$chunks = explode('[', $key);
if (count($chunks) == 1) {
$postFields[$key] = $value;
return;
}
$chunkKey = $chunks[0];
if (!isset($postFields[$chunkKey])) {
$postFields[$chunkKey] = [];
}
$parent = &$postFields;
for ($i = 1; $i < count($chunks); $i++) {
$previousChunkKey = $chunkKey;
if (!isset($parent[$previousChunkKey])) {
$parent[$previousChunkKey] = [];
}
$parent = &$parent[$previousChunkKey];
$chunkKey = $chunks[$i];
if ($chunkKey == ']') {
$parent[] = $value;
return;
}
$chunkKey = rtrim($chunkKey, ']');
if ($i == count($chunks) - 1) {
$parent[$chunkKey] = $value;
return;
}
}
}
}