Skip to content

Commit 81fcf4e

Browse files
committed
If the ID is explicitly set to NULL when creating a Request then we accept that, however if no value for ID was passed to the constructor we generate a random ID
1 parent d8394b4 commit 81fcf4e

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

src/Request.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,12 @@ class Request extends Notification implements JsonSerializable {
3232
public function __construct(string $method, array $params = [], $id = null)
3333
{
3434
parent::__construct($method, $params);
35-
$this->setId($id);
35+
/** If the ID is explicitly set to NULL then we accept that, however if no value for ID was passed to the constructor we generate an ID */
36+
if (3 === func_num_args()) {
37+
$this->setId($id);
38+
} else {
39+
$this->setId(bin2hex(random_bytes(5)));
40+
}
3641
}
3742

3843
/**
@@ -49,7 +54,7 @@ public function setId($id)
4954
if (is_string($id) || is_int($id) || is_null($id)) {
5055
$this->id = $id;
5156
} else {
52-
throw new RuntimeException('Invalid Id format. Must be string, number or null');
57+
throw new RuntimeException('Invalid Id format. Must be a string, number, or null');
5358
}
5459
}
5560

0 commit comments

Comments
 (0)