1
0
mirror of https://github.com/danog/psalm.git synced 2024-12-02 17:52:45 +01:00
psalm/tests/LanguageServer/Message.php
2022-02-18 17:29:09 +00:00

46 lines
1.4 KiB
PHP

<?php
namespace Psalm\Tests\LanguageServer;
use AdvancedJsonRpc\Message as AdvancedJsonRpcMessage;
use AdvancedJsonRpc\Notification;
use AdvancedJsonRpc\Request;
use AdvancedJsonRpc\SuccessResponse;
use AdvancedJsonRpc\ErrorResponse;
/**
* Base message
*/
abstract class Message extends AdvancedJsonRpcMessage
{
/**
* A String specifying the version of the JSON-RPC protocol. MUST be exactly "2.0".
*
* @var string
*/
public $jsonrpc = '2.0';
/**
* Returns the appropriate Message subclass
*
* @param array $msg
* @return AdvancedJsonRpcMessage
*/
public static function parseArray(array $msg): AdvancedJsonRpcMessage
{
$decoded = (object) $msg;
if (Notification::isNotification($decoded)) {
$obj = new Notification($decoded->method, $decoded->params ?? null);
} else if (Request::isRequest($decoded)) {
$obj = new Request($decoded->id, $decoded->method, $decoded->params ?? null);
} else if (SuccessResponse::isSuccessResponse($decoded)) {
$obj = new SuccessResponse($decoded->id, $decoded->result);
} else if (ErrorResponse::isErrorResponse($decoded)) {
$obj = new ErrorResponse($decoded->id, new Error($decoded->error->message, $decoded->error->code, $decoded->error->data ?? null));
} else {
throw new Error('Invalid message', ErrorCode::INVALID_REQUEST);
}
return $obj;
}
}