Skip to content

Commit

Permalink
参数检查,增加空数组检查
Browse files Browse the repository at this point in the history
  • Loading branch information
yeaha committed Mar 13, 2015
1 parent 38241e6 commit bc69ea8
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 2 deletions.
22 changes: 20 additions & 2 deletions src/Parameter/Checker.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@
* 'bar' => [], // 不指定任何配置就用默认配置
* 'foobar' => [
* 'type' => 'hash', // 字典类型
* 'allow_empty' => true, // 是否允许空hash数组
* 'keys' => [ // 声明字典值配置
* 'foo' => [ // 字典和数组都可以任意嵌套
* 'type' => 'array', // 数组类型
* 'allow_empty' => true, // 是否允许空数组
* 'element' => [ // 数组每个元素的值配置
* 'bar' => [],
* ],
Expand Down Expand Up @@ -156,10 +158,18 @@ protected function checkLiteral($key, $value, array $option) {
* @return void
*/
protected function checkHash($key, $value, array $option) {
if (!is_array($value) || array_values($value) === $value) {
if (!is_array($value) || ($value && array_values($value) === $value)) {
throw $this->exception($key, 'value is not hash type');
}

if (!$value) {
if (!$option['allow_empty']) {
throw $this->exception($key, 'value not allow empty hash');
}

return;
}

if (isset($option['keys']) && $option['keys']) {
$this->path[] = $key;
$this->execute($value, $option['keys'], $this->path);
Expand All @@ -175,10 +185,18 @@ protected function checkHash($key, $value, array $option) {
* @return void
*/
protected function checkArray($key, $value, array $option) {
if (!is_array($value) || array_values($value) !== $value) {
if (!is_array($value) || ($value && array_values($value) !== $value)) {
throw $this->exception($key, 'value is not array type');
}

if (!$value) {
if (!$option['allow_empty']) {
throw $this->exception($key, 'value not allow empty array');
}

return;
}

if (isset($option['element']) && $option['element']) {
$this->path[] = $key;
foreach ($value as $element) {
Expand Down
48 changes: 48 additions & 0 deletions tests/Parameter/CheckerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,30 @@ public function testHashTypeException() {
);
}

public function testEmptyHashException() {
$checker = new \Owl\Parameter\Checker;

$checker->execute(
array('foo' => array()),
array(
'foo' => array(
'type' => 'hash',
'allow_empty' => true,
),
)
);

$this->setExpectedExceptionRegExp('\Owl\Parameter\Exception', '/not allow empty hash/');
$checker->execute(
array('foo' => array()),
array(
'foo' => array(
'type' => 'hash',
),
)
);
}

public function testArrayType() {
$checker = new \Owl\Parameter\Checker;

Expand Down Expand Up @@ -155,6 +179,30 @@ public function testArrayType() {
), $options);
}

public function testEmptyArrayException() {
$checker = new \Owl\Parameter\Checker;

$checker->execute(
array('foo' => array()),
array(
'foo' => array(
'type' => 'array',
'allow_empty' => true,
),
)
);

$this->setExpectedExceptionRegExp('\Owl\Parameter\Exception', '/not allow empty array/');
$checker->execute(
array('foo' => array()),
array(
'foo' => array(
'type' => 'array',
),
)
);
}

public function testURLType() {
$checker = new \Owl\Parameter\Checker;

Expand Down

0 comments on commit bc69ea8

Please sign in to comment.