Пример #1
0
 public function testValidators()
 {
     $goods = array("int" => 42, "double" => 42.42, "boolean" => true, "base64" => "dGhpc2lzYXRlc3QK", "dateTime.iso8601" => date('c'), "string" => "test", "array" => array(0, 1, 2), "struct" => array("this" => "is", "a" => "test"), "null" => null);
     $bads = array("int" => "42", "double" => 42, "boolean" => 2, "base64" => false, "dateTime.iso8601" => date('r'), "string" => 1, "array" => array("test" => 0, 1, 2), "struct" => array(0, 1, 2), "null" => "null");
     foreach ($goods as $kind => $value) {
         $result = DataValidator::validate($kind, $value);
         $this->assertTrue($result);
         $result = DataValidator::validate("undefined", $value);
         $this->assertTrue($result);
     }
     foreach ($bads as $kind => $value) {
         $result = DataValidator::validate($kind, $value);
         $this->assertFalse($result);
     }
 }
Пример #2
0
 /**
  * Check if call match a signature
  *
  * @param array  $provided
  * @param array  $requested
  *
  * @return bool
  */
 private static function checkSignatureMatch($provided, $requested)
 {
     if (is_object($provided)) {
         foreach ($provided as $parameter => $value) {
             if (!isset($requested[$parameter]) || !DataValidator::validate($requested[$parameter], $value)) {
                 return false;
             }
         }
     } else {
         $provided_parameters_count = count($provided);
         $requested_parameters_count = count($requested);
         if ($provided_parameters_count != $requested_parameters_count) {
             return false;
         }
         $index = 0;
         foreach ($requested as $parameter => $type) {
             if (!DataValidator::validate($type, $provided[$index])) {
                 return false;
             }
             $index += 1;
         }
     }
     return true;
 }
Пример #3
0
 /**
  * Check if a request is consistent (i.e. if it matches one of method's signatures)
  *
  * @param array  $provided_parameters
  *
  * @return int
  * @throws \Comodojo\Exception\RpcException
  */
 private function checkRequestConsistence($provided_parameters)
 {
     $signatures = $this->registered_method->getSignatures(false);
     $provided_parameters_count = count($provided_parameters);
     foreach ($signatures as $num => $signature) {
         $requested_parameters_count = count($signature["PARAMETERS"]);
         if ($provided_parameters_count != $requested_parameters_count) {
             continue;
         }
         $index = 0;
         foreach ($signature["PARAMETERS"] as $parameter => $type) {
             if (!DataValidator::validate($type, $provided_parameters[$index])) {
                 continue 2;
             }
             $index += 1;
         }
         return $num;
     }
     throw new RpcException("Invalid params", -32602);
 }