thant / input-filter
本软件包最新版本(1.0.4)没有提供许可信息。
1.0.4
2022-02-13 01:42 UTC
Requires (Dev)
- phpunit/phpunit: ^5.7
This package is not auto-updated.
Last update: 2024-09-22 13:55:39 UTC
README
这是一个小巧的PHP库,用于输入过滤和验证,可用于任何框架或应用。它使用PHP原生过滤器进行过滤和验证。有关PHP原生过滤器的更多信息,请参阅以下列表
https://php.ac.cn/manual/en/filter.filters.php
如何使用它?
<?php use Thant\Helpers\InputFilter; $inputs = array( 'field1' => ' 100 ', 'field2' => 'kevin@gmail.com', 'field3' => '4400-129-2210' ); $sanitizer = new InputFilter(); $sanitizer->setInputs($inputs) ->setRequired('field1') ->filter('field1', array( FILTER_VALIDATE_INT )) ->setRequired('field2') ->filter('field2', array( FILTER_VALIDATE_EMAIL ), 'Valid email required') ->setRequired('field3', 'Field3 is required') //you can also filter a field by multiple filters in sequence ->filter('field3', array( FILTER_SANITIZE_STRING, FILTER_VALIDATE_REGEXP => [ 'options' => array( 'regexp' => '=^([0-9]{3})\-?([0-9]{3})\-?([0-9]{4})$=' ) ] ), 'Must be a valid US phone number') ->sanitize(); if($sanitizer->hasErrors()) { $errors = $sanitizer->getErrors(); //TODO do anything you want with error list failed for validation and filtering } else { $data = $santizer->getClean(); //get the cleaned data $field1 = $data['field1']; $field2 = $data['field2']; $field3 = $data['field3']; }
请查看test/InputFilterTest.php文件中的更多示例
如何编写您自己的客户过滤器?
要使用您自己的客户过滤器,您将使用PHP过滤器常量“FILTER_CALLBACK”,然后您可以提供一个回调函数数组。目前有一个名为DateTimeFilterCallback的客户过滤器,它实现了IFilterCallback接口。请参考该类,了解如何实现您自己的自定义过滤器。
以下是自定义过滤器回调类的简单实现。
<?php use Thant\Helpers\InputFilter\IFilterCallback; class MyCustomFilterCallback implements IFilterCallback { public function filter($val) { //return false if validation or filtering fails. //Otherwise you can return a sanitized value back. } }
使用自定义过滤器示例代码
<?php use Thant\Helpers\InputFilter; use Thant\Helpers\InputFilter\DateTimeFilterCallback; $inputs = array( 'dateOfBirth' => '01/01/1990', 'appointmentDate' => '06/01/2017' ); $sanitizer = new InputFilter(); $sanitizer->setInputs($inputs) ->setRequired('dateOfBirth', 'Date of birth is required') ->filter('dateOfBirth', array( FILTER_CALLBACK => new DateTimeFilterCallback([ 'maxDate' => date('m/d/Y') //today date because birth date should be later than today, 'format' => 'Y-m-d' //the final date format you want after filtering and validation pass ]) ), 'Must be a valid date not later than today date') ->setRequired('appointmentDate') ->filter('appointmentDate', array( FILTER_CALLBACK => new DateTimeFilterCallback([ 'minDate' => date('m/d/Y'), //appointment date should be today or in the future 'format' => 'Y-m-d' ]) ), 'Appointment date must be a valid date starting from today') ->sanitize(); if($santizer->hasErrors()) { $errors = $santizer->getErrors(); // do input validation error handling } else { $data = $sanitizer->getClean(); $dateOfBirth = $data['dateOfBirth']; //should be '1990-01-01' $appointmentDate = $data['appointmentDate']; //should be '2017-06-01' }
使用匿名函数的自定义过滤器
$sanitizer = new InputFilter(); $sanitizer->filter('field1', array( FILTER_CALLBACK => [ 'options' => function($val) { //TODO check the $val, return false if filtering/validation fails. Otherwise, return the sanitized value } ] ));
您也可以在tests/InputFilterTest.php文件中找到使用匿名函数的测试用例。