-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInputStreamCollection.php
More file actions
40 lines (32 loc) · 1.03 KB
/
InputStreamCollection.php
File metadata and controls
40 lines (32 loc) · 1.03 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
<?php declare(strict_types=1);
namespace Parable\GetSet;
use Parable\GetSet\Resource\LocalResourceInterface;
use Throwable;
class InputStreamCollection extends BaseCollection implements LocalResourceInterface
{
protected const INPUT_SOURCE = 'php://input';
public function __construct()
{
$body_content = @file_get_contents(static::INPUT_SOURCE);
if ($body_content === false) {
throw new GetSetException(sprintf(
"Could not read from input source '%s'.",
static::INPUT_SOURCE
));
}
if (!empty($body_content)) {
$this->extractAndSetData($body_content);
}
}
protected function extractAndSetData(string $data)
{
try {
$data_parsed = json_decode($data, true, 512, JSON_THROW_ON_ERROR);
} catch (Throwable $t) {
parse_str(trim($data), $data_parsed);
}
if (is_array($data_parsed) && !empty($data_parsed)) {
$this->setAll($data_parsed);
}
}
}