Passed
Push — master ( 2ccfa2...09325e )
by Marcel
02:12
created

Input::parsable()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
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 49
    private function __construct($data, int $error)
27
    {
28 49
        $this->data = $data;
29 49
        $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 49
        if (!self::$schema instanceof stdClass) {
0 ignored issues
show
introduced by
self::schema is always a sub-type of stdClass.
Loading history...
34
            self::$schema = (object)[
35 1
                '$schema' => 'https://json-schema.org/draft-07/schema#',
36 1
                'description' => 'JSON-RPC 2.0 single request schema',
37 1
                'type' => 'object',
38
                'required' => ['jsonrpc', 'method'],
39
                'additionalProperties' => false,
40
                'properties' => (object)[
41
                    'jsonrpc' => (object)[
42 1
                        'type' => 'string',
43
                        'enum' => ['2.0']
44
                    ],
45
                    'method' => (object)[
46 1
                        'type' => 'string'
47
                    ],
48
                    'params' => (object)[
49 1
                        'type' => ['array', 'object']
50
                    ],
51
                    'id' => (object)[
52 1
                        'type' => ['integer', 'string']
53
                    ],
54
                ]
55
            ];
56
        }
57 49
    }
58
59 48
    public static function fromString(string $raw): Input
60
    {
61 48
        if (!\extension_loaded('simdjson')) {
62 48
            return new self(\json_decode($raw), \json_last_error());
63
        }
64
65
        if (!\simdjson_isvalid($raw)) {
0 ignored issues
show
Bug introduced by
The function simdjson_isvalid was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

65
        if (!/** @scrutinizer ignore-call */ \simdjson_isvalid($raw)) {
Loading history...
66
            return new self(null, JSON_ERROR_SYNTAX);
67
        }
68
69
        return new self(\simdjson_decode($raw), JSON_ERROR_NONE);
0 ignored issues
show
Bug introduced by
The function simdjson_decode was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

69
        return new self(/** @scrutinizer ignore-call */ \simdjson_decode($raw), JSON_ERROR_NONE);
Loading history...
70
    }
71
72 5
    public static function fromSafeData($data): Input
73
    {
74 5
        \assert(false !== \json_encode($data));
75
76 5
        return new self($data, JSON_ERROR_NONE);
77
    }
78
79 41
    public function data()
80
    {
81 41
        return $this->data;
82
    }
83
84 48
    public function parsable(): bool
85
    {
86 48
        return JSON_ERROR_NONE === $this->error;
87
    }
88
89 44
    public function isArray(): bool
90
    {
91 44
        return \is_array($this->data) && !empty($this->data);
92
    }
93
94 27
    public function isRpcRequest(): bool
95
    {
96 27
        return Validator::validate(self::$schema, $this->data);
97
    }
98
}
99