topolis / filter
一个适用于不受信任值的灵活过滤系统
Requires (Dev)
- phpunit/phpunit: 5.*
README
一个安全的过滤类,用于封装从不受信任的来源(如 $_REQUEST
或 $_COOKIE
)访问变量。
过滤值
The Filter::filter() method filters the given value and returns the sanitized result. If value is an array
, the filter function is applied to all elements of the array, even recursive.
// Filter variable $text with Strip filter.
$text = Filter::filter($text, "strip");
验证值
The Filter::validate() method validates the given value and returns false if invalid. If value is an array
, the validation check is applied to all elements of the array, the function will return false if any of the validated values inside the array is invalid.
// Check if variable $text does not contain any tags
$valid = Filter::validate($text, "strip");
使用过滤选项
You can set special options for the used filter as the fourth method parameter. Available options can be seen below. Most filters use an array of $key => $value pairs to configure their behaviour.
$value = Filter::filter($value, $filter, ["Parameter" => "Value"]);
使用多个过滤器
You can use multiple filters and options in a queue and pass your value through them.
$value = Filter::filter($value, ["plain","strip","number], [ ["options" => "params"], ["options" => "params"], ["options" => "params"]]);
使用多个值
You can use an array or multi-dimensional array of values with your filters and options. The test will be appied to the non-array values inside the array. Depending on the type
option, the value has to be of this nature
tree
a multidimensional array of at least one level of depth of valuesarray
an array of valuessingle
A single value
If no type is specified, anything is valid.
$value = Filter::filter($value, $filter, ["type" => single]);
可用过滤器
You can specify any of the following filters for Filter. The name of the filter allways is identical to the first part of it's class name lowercase. (Example: "plain" means class "PlainFilter" in file "PlainFilter.php".
布尔值
Filters a value and returns it as a boolean value of true or false
Options
- true an array of values that are treated as
true
. Default:[ 1, "true", true ]
- strict Only allow exactly the allowed values for the true option above and
false
if not. Otherwise a simple cast to boolean is also enough.
日期时间
Validates or returns a formatted date-time string.
Options
- format the format to return datetime values in. Default:
Y-m-d H:i:s
- timezone The timezone to use or
false
if none. Default:false
电子邮件
Validates or filters a email value. (No options)
枚举
Allow only values from a fixed set of options.
Options
- values An array of allowed values. Default:
[]
- strict Allowed values must also have the matching type. default:
false
- insensitive Allow case insensitive checks for strings. Default
false
- autocorrect Return the matching value from the allowed array on a successfull match (possibly correcting wrong types and cases). Default:
true
JSON
Validate and optionally unserialize a inpout string with a JSON value.
Options
- format return the input in one of the following formats
- 1 - JSON: Return untouched as the validated json string
- 2 - 解码:返回解码结果作为多维数组(而不是stdClass对象)
- 3 - 序列化:返回输入值作为PHP序列化字符串
货币
此过滤器是数字过滤器的简写版本。它已预配置为默认值,适用于货币。(见 @Number 以获取选项)
数字
使用或不用小数点过滤或验证数字。注意:结果将是 double
,无论小数位数如何。
Options
- min 值必须至少为 X。默认:
false
- max 值必须最大为 X。默认:
false
- adjust 如果指定了 min/max,允许在需要时将数字清理到这个值。默认:
false
- decimals 将结果四舍五入到指定的小数位数。默认:
false
- round 使用此方法进行四舍五入。可以是 round、floor 或 ceil。此方法将使用 bc 库进行四舍五入以避免浮点错误。默认:
round
- validate 如果值不是根据选项定义的有效数字,则失败。默认:
false
透传
此过滤器允许任何输入而不进行修改。请小心!
Options
- append 将此固定字符串附加到输入值。(这将导致验证失败,因为输入与输出不同。)
路径
PlainExt 过滤器的简写版本,预配置为允许文件路径中可用的任何字符。
Options
- characters 除所选字符集之外的额外有效字符。默认:无
- charactersets 要使用的字符集。可以通过添加数字来使用组合
- 1 - 基本型:
abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890._-
- 2 - Windows:
\
- 4 - 单位:
/
- 7 - 所有:
以上全部
- 1 - 基本型:
普通扩展
一个文本过滤器,允许某些字符,并具有预配置的可选字符集。
Options
- characters 除所选字符集之外的额外有效字符。默认:无
- charactersets 要使用的字符集。可以通过添加数字来使用组合
- 1 - 基本型:
abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890
- 2 - 简单型:
,.:-_()?!
- 4 - 德语:
äöüÄÖÜß
- 8 - 法语:
áéíóúàèìòùâêîôûÁÉÍÓÚÀÈÌÒÙÂÊÎÔÛ
- 13 - 国际型:Basic + German + French
- 15 - 所有:
以上全部
- 1 - 基本型:
纯文本
一个简单的文本过滤器,只允许基本的字母和数字(A-Z a-z 0-9)。
正则表达式
一个根据正则表达式进行验证的过滤器。
Options
- pattern 要由
preg_match
使用的正则表达式。示例:/(dog|horse)shit$/i
删除
一个移除HTML标签的文本过滤器。
Options
- allowable_tags 允许的标签。只需指定打开标签。(见:https://php.ac.cn/manual/en/function.strip-tags.php)
- preserve_null 如果输入字符串是
null
,则返回null
而不是空字符串。默认:false
测试
一个简单的过滤器,可用于单元测试。它测试输入与一个或多个预期值是否匹配。
Options
- expected 一个值或一个值的数组。默认:
null
- error 如果输入无效,则返回此值。默认:过滤器的正常行为
- strict 使用类型严格比较检查输入。
URL
检查输入是否根据 parse_url 方法配置的检查为有效URL。
Options
- require 必要的 URL 元素数组。可以是(方案、主机、端口、用户、密码、根、路径、查询、片段)之一或多个。默认:
[]
- disallow 被禁止的 URL 元素数组。可以是(方案、主机、端口、用户、密码、根、路径、查询、片段)之一或多个。默认:
[]
- schemes 如果找到方案,则允许的方案数组。默认:
["http","https"]
- 类型 以上选项的简写定义。可以是(绝对、相对、根)之一。默认:
false