예제 #1
0
 /**
  * 根据规则获取参数
  *
  * 根据提供的参数规则,进行参数创建工作,并返回错误信息
  *
  * @param $rule array('name' => '', 'type' => '', 'defalt' => ...) 参数规则
  * @return mixed
  */
 public function getByRule($rule)
 {
     $rs = NULL;
     if (!isset($rule['name'])) {
         throw new PhalApi_Exception_InternalServerError(T('miss name for rule'));
     }
     $rs = PhalApi_Request_Var::format($rule['name'], $rule, $this->data);
     if ($rs === NULL && (isset($rule['require']) && $rule['require'])) {
         throw new PhalApi_Exception_BadRequest(T('{name} require, but miss', array('name' => $rule['name'])));
     }
     return $rs;
 }
예제 #2
0
 /**
  * @expectedException PhalApi_Exception_InternalServerError
  */
 public function testFormatCallableButWroing()
 {
     $rs = PhalApi_Request_Var::format('testKey', array('name' => 'testKey', 'type' => 'callable', 'callback' => 'xxx'), array('testKey' => 1));
 }
 public function testFormatAllTypes()
 {
     $params = array('floatVal' => '1.0', 'booleanVal' => '1', 'dateVal' => '2015-02-05 00:00:00', 'arrayVal' => 'a,b,c', 'enumVal' => 'male');
     $rule = array('name' => 'floatVal', 'type' => 'float');
     $rs = PhalApi_Request_Var::format('floatVal', $rule, $params);
     $this->assertSame(1.0, $rs);
     $rule = array('name' => 'booleanVal', 'type' => 'boolean');
     $rs = PhalApi_Request_Var::format('booleanVal', $rule, $params);
     $this->assertSame(true, $rs);
     $rule = array('name' => 'dateVal', 'type' => 'date', 'format' => 'timestamp');
     $rs = PhalApi_Request_Var::format('dateVal', $rule, $params);
     $this->assertSame(1423065600, $rs);
     $rule = array('name' => 'arrayVal', 'type' => 'array', 'format' => 'explode');
     $rs = PhalApi_Request_Var::format('arrayVal', $rule, $params);
     $this->assertSame(array('a', 'b', 'c'), $rs);
     $rule = array('name' => 'enumVal', 'type' => 'enum', 'range' => array('female', 'male'));
     $rs = PhalApi_Request_Var::format('enumVal', $rule, $params);
     $this->assertSame('male', $rs);
     $rule = array('name' => 'noThisKey');
     $rs = PhalApi_Request_Var::format('noThisKey', $rule, $params);
     $this->assertSame(null, $rs);
     $rule = array('name' => 'noThisKey', 'type' => 'noThisType');
     $rs = PhalApi_Request_Var::format('noThisKey', $rule, $params);
     $this->assertSame(null, $rs);
     $_FILES['aFile'] = array('name' => 'aHa~', 'type' => 'image/jpeg', 'size' => 100, 'tmp_name' => '/tmp/123456', 'error' => 0);
     $rule = array('name' => 'aFile', 'range' => array('image/jpeg'), 'min' => 50, 'max' => 1024, 'require' => true, 'default' => array(), 'type' => 'file');
     $rs = PhalApi_Request_Var::format('aFile', $rule, $params);
     $this->assertNotEmpty($rs);
 }