ailixter/gears-filter

该项目实现了 gears Filter 功能。

0.1.2 2019-08-16 12:26 UTC

This package is auto-updated.

Last update: 2024-09-20 02:08:28 UTC


README

该项目实现了 gears Filter 功能。

类型转换

  • 变量
  • 输入 GET, POST 等
  • 数组

将 $var 转换为 $type。

$filter = new Filter;
$int1 = $filter->cast(FILTER_VALIDATE_INT, '123');
$int2 = $filter->cast(['filter'=>FILTER_VALIDATE_INT, 'flags'=>...], '123');
$int3 = $filter->cast('int', '123');

将输入 $key 转换为 $type。

 $filter = new Filter;
 $int1 = $filter->castInput(FILTER_VALIDATE_INT, INPUT_POST, 'p');
 $int2 = $filter->castInput(['filter'=>FILTER_VALIDATE_INT, 'flags'=>...], INPUT_POST, 'p');
 $int3 = $filter->castInput('int', INPUT_POST, 'p');

基于 Filter 的实用类 ArrayFilter

$array = new ArrayFilter(['a' => 123, 'b' => 'false'], new Filter);
$bool = $array->get('bool', 'b');
$float = $array->getFloat('a', 0.0);
$custom = $array->getCustomType('a');
$all = $array->castAll(['a' => 'float', 'b' => 'bool', 'undefined' => 'str']);

基于 Filter 的实用类 InputFilter

$post = new InputFilter(INPUT_POST, new Filter);
$bool = $post->get('bool', 'b');
$float = $post->getFloat('a', 0.0);
$custom = $post->getCustomType('a');
$all = $post->castAll(['a' => 'float', 'b' => 'bool', 'undefined' => 'str']);