|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace UMA\JsonRpc\Internal; |
|
6
|
|
|
|
|
7
|
|
|
use stdClass; |
|
8
|
|
|
|
|
9
|
|
|
final class Input |
|
10
|
|
|
{ |
|
11
|
|
|
/** |
|
12
|
|
|
* @var stdClass |
|
13
|
|
|
*/ |
|
14
|
|
|
private static $schema; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* @var mixed |
|
18
|
|
|
*/ |
|
19
|
|
|
private $data; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @var int |
|
23
|
|
|
*/ |
|
24
|
|
|
private $error; |
|
25
|
|
|
|
|
26
|
|
|
private function __construct($data, int $error) |
|
27
|
|
|
{ |
|
28
|
|
|
$this->data = $data; |
|
29
|
|
|
$this->error = $error; |
|
30
|
|
|
|
|
31
|
|
|
// This is the minimal schema that all incoming payloads must |
|
32
|
|
|
// conform to in order to be considered well-formed JSON-RPC requests. |
|
33
|
|
|
if (!self::$schema instanceof stdClass) { |
|
|
|
|
|
|
34
|
|
|
self::$schema = (object)[ |
|
35
|
|
|
'$schema' => 'https://json-schema.org/draft-07/schema#', |
|
36
|
|
|
'description' => 'JSON-RPC 2.0 single request schema', |
|
37
|
|
|
'type' => 'object', |
|
38
|
|
|
'required' => ['jsonrpc', 'method'], |
|
39
|
|
|
'additionalProperties' => false, |
|
40
|
|
|
'properties' => (object)[ |
|
41
|
|
|
'jsonrpc' => (object)[ |
|
42
|
|
|
'enum' => ['2.0'] |
|
43
|
|
|
], |
|
44
|
|
|
'method' => (object)[ |
|
45
|
|
|
'type' => 'string' |
|
46
|
|
|
], |
|
47
|
|
|
'params' => (object)[ |
|
48
|
|
|
'type' => ['array', 'object'] |
|
49
|
|
|
], |
|
50
|
|
|
'id' => (object)[ |
|
51
|
|
|
'type' => ['integer', 'string'] |
|
52
|
|
|
], |
|
53
|
|
|
] |
|
54
|
|
|
]; |
|
55
|
|
|
} |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
public static function fromString(string $raw, bool $simdJson): Input |
|
59
|
|
|
{ |
|
60
|
|
|
if (!$simdJson || !\extension_loaded('simdjson')) { |
|
61
|
|
|
return new self(\json_decode($raw), \json_last_error()); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
if (!\simdjson_isvalid($raw)) { |
|
|
|
|
|
|
65
|
|
|
return new self(null, JSON_ERROR_SYNTAX); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
return new self(\simdjson_decode($raw), JSON_ERROR_NONE); |
|
|
|
|
|
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
public static function fromSafeData($data): Input |
|
72
|
|
|
{ |
|
73
|
|
|
\assert(false !== \json_encode($data)); |
|
74
|
|
|
|
|
75
|
|
|
return new self($data, JSON_ERROR_NONE); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
public function data() |
|
79
|
|
|
{ |
|
80
|
|
|
return $this->data; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
public function parsable(): bool |
|
84
|
|
|
{ |
|
85
|
|
|
return JSON_ERROR_NONE === $this->error; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
public function isArray(): bool |
|
89
|
|
|
{ |
|
90
|
|
|
return \is_array($this->data) && !empty($this->data); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
public function isRpcRequest(): bool |
|
94
|
|
|
{ |
|
95
|
|
|
return Validator::validate(self::$schema, $this->data); |
|
96
|
|
|
} |
|
97
|
|
|
} |
|
98
|
|
|
|