Completed
Push — master ( 5d80f5...f36035 )
by Marcel
08:17
created

Input   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 79
Duplicated Lines 0 %

Test Coverage

Coverage 96.15%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 29
c 3
b 0
f 0
dl 0
loc 79
ccs 25
cts 26
cp 0.9615
rs 10
wmc 9

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 26 2
A isRpcRequest() 0 3 1
A fromString() 0 3 1
A isArray() 0 3 2
A parsable() 0 3 1
A data() 0 3 1
A fromSafeData() 0 5 1
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
                        'enum' => ['2.0']
43
                    ],
44
                    'method' => (object)[
45 1
                        'type' => 'string'
46
                    ],
47
                    'params' => (object)[
48 1
                        'type' => ['array', 'object']
49
                    ],
50
                    'id' => (object)[
51 1
                        'type' => ['integer', 'string']
52
                    ],
53
                ]
54
            ];
55
        }
56 49
    }
57
58 48
    public static function fromString(string $raw): Input
59
    {
60 48
        return new self(\json_decode($raw), \json_last_error());
61
    }
62
63 5
    public static function fromSafeData($data): Input
64
    {
65 5
        \assert(false !== \json_encode($data));
66
67 5
        return new self($data, JSON_ERROR_NONE);
68
    }
69
70 41
    public function data()
71
    {
72 41
        return $this->data;
73
    }
74
75 48
    public function parsable(): bool
76
    {
77 48
        return JSON_ERROR_NONE === $this->error;
78
    }
79
80 44
    public function isArray(): bool
81
    {
82 44
        return \is_array($this->data) && !empty($this->data);
83
    }
84
85 27
    public function isRpcRequest(): bool
86
    {
87 27
        return Validator::validate(self::$schema, $this->data);
88
    }
89
}
90