id = $id; $this->result = $result; } /** * Returns the ID of the response. * * @return string The ID of the response. */ public function getId(): string { return $this->id; } /** * Returns the result of the response. * * @return mixed|null The result of the response. */ public function getResult(): mixed { return $this->result; } /** * Converts the given data to an array. * * @param mixed $data The data to be converted. This can be an instance of SerializableInterface, an array, or a scalar value. * @return mixed The converted data as an array if applicable, or the original data. */ private function convertToArray(mixed $data): mixed { // If the data is an instance of SerializableInterface, call toArray on it if ($data instanceof SerializableInterface) { return $data->toArray(); } // If the data is an array, recursively call this function on each element if(is_array($data)) { foreach($data as $key => $value) { if(is_array($value)) { $data[$key] = $this->convertToArray($value); } elseif($value instanceof SerializableInterface) { $data[$key] = $value->toArray(); } else { $data[$key] = $value; } } } return $data; } /** * Returns an array representation of the object. * * @return array The array representation of the object. */ public function toArray(): array { return [ 'id' => $this->id, 'result' => $this->convertToArray($this->result) ]; } /** * Returns the response object from an array of data. * * @param array $data The data to construct the response from. * @return RpcResponse The response object. */ public static function fromArray(array $data): RpcResponse { return new RpcResponse($data['id'], $data['result']); } }