/**
  * @param array $json
  * @return \MToolkit\Network\RPC\Json\MRPCJsonError
  */
 public static function fromArray(array $json)
 {
     $error = new MRPCJsonError();
     $error->setCode($json["error"]);
     $error->setMessage($json["message"]);
     return $error;
 }
 /**
  * Run the instance of the web service.
  */
 public static function run()
 {
     /* @var $classes string[] */
     $classes = array_reverse(get_declared_classes());
     foreach ($classes as $class) {
         $type = new \ReflectionClass($class);
         $abstract = $type->isAbstract();
         if (is_subclass_of($class, 'MToolkit\\Network\\RPC\\Json\\Server\\MRPCJsonWebService') === false || $abstract === true) {
             continue;
         }
         /* @var $webService MRPCJsonWebService */
         $webService = new $class();
         // If the definitions are reuqested
         if ($_SERVER['QUERY_STRING'] == "definition") {
             $webService->definition();
             echo "<html><body>";
             echo "<h1>Methods definitions</h1>";
             foreach ($webService->methodsDefinitions as $methodDefinition) {
                 echo "<h2>" . $methodDefinition["name"] . "</h2>";
                 echo "<pre>" . $methodDefinition["definition"] . "</pre>";
                 echo "<h3>Request example</h3>";
                 echo "<pre>" . '{"jsonrpc": "2.0", "method": "' . $methodDefinition["name"] . '", "params": {"name_1": value_1, "name_2": value_2, ...}, "id": 3}' . "</pre>";
             }
             echo "</body></html>";
         } else {
             header(ContentType::APPLICATION_JSON);
             try {
                 $webService->execute($class);
                 $webService->getResponse()->setId($webService->getRequest()->getId());
             } catch (MRPCJsonServerException $ex) {
                 $error = new MRPCJsonError();
                 $error->setCode(-1);
                 $error->setMessage($ex->getMessage());
                 $webService->response = new MRPCJsonResponse();
                 $webService->response->setError($error);
             }
             echo $webService->getResponse()->toJSON();
         }
         // Clean the $_SESSION from signals.
         $webService->disconnectSignals();
         return;
     }
 }
 /**
  * @param array $json
  */
 public static function fromArray(array $json)
 {
     $response = new MRPCJsonResponse();
     $response->setError(MRPCJsonError::fromArray($json["error"]));
     $response->setId($json["id"]);
     $response->setResult($json["result"]);
 }
 /**
  * Run the instance of the web service.
  */
 public static function autorun()
 {
     /* @var $classes string[] */
     $classes = array_reverse(get_declared_classes());
     foreach ($classes as $class) {
         $type = new \ReflectionClass($class);
         $abstract = $type->isAbstract();
         if (is_subclass_of($class, MRPCJsonWebService::class) === false || $abstract === true) {
             continue;
         }
         /* @var $webService MRPCJsonWebService */
         $webService = new $class();
         // If the definitions are requested
         if ($_SERVER['QUERY_STRING'] == 'definition') {
             $definition = new MRPCJsonWebServiceDefinition($class);
             $webService->getHttpResponse()->setContentType(ContentType::TEXT_HTML);
             $webService->getHttpResponse()->setOutput((string) $definition);
         } else {
             $webService->getHttpResponse()->setContentType(ContentType::APPLICATION_JSON);
             try {
                 $webService->execute($class);
                 $webService->getResponse()->setId($webService->getRequest()->getId());
             } catch (MRPCJsonServerException $ex) {
                 $error = new MRPCJsonError();
                 $error->setCode(-1);
                 $error->setMessage($ex->getMessage());
                 $webService->response = new MRPCJsonResponse();
                 $webService->response->setError($error);
             }
             $webService->getHttpResponse()->setOutput($webService->getResponse()->toJSON());
         }
         return $webService;
     }
     return null;
 }