承接 grithin/phpinput 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

grithin/phpinput

最新稳定版本:3.0.0

Composer 安装命令:

composer require grithin/phpinput

包简介

PHP Tools for handling input.

关键字:

README 文档

README

Purpose

Provide a succinct way for common filtering and validating of input

See the Input class inline documentation

Brief

Rules is an array of rules whos keys map the keys of the input

Rules are parsed in the order provided

A rule consists of steps

Each rule can be an array of steps or a string of steps separated with ','

Every step consists of at most three parts

  • prefix
  • handler
  • parameters

The rule looks like one of the following

  • handler|param1;param2;param3, handler, handler
  • ['handler|param1;param2;param3', handler, handler]
  • [[handler, param1, param2, param2], handler, handler]
  • [[[prefix, callback], param1, param2, param2], handler, handler] Notice, as a string, parameters are separated with ";"

The prefix can be some combination of the following

  • "!" to break on error with no more rules for that field
  • "!!" to break on error with no more rules for any field
  • "?" to indicate the validation is optional, and not to throw an error (useful when combined with '!' => '?!v.filled,email')
  • "~" to indicate if the validation does not fail, then there was an error
  • "&" to indicate code should break if there were any previous errors on that field. This moves around the responsibility of ending on error, which is sometimes desirable with prefixed validations : prefixed_validation.concat ['&validation2',...]
  • "&&" to indicate code should break if there were any previous errors on any field in the validate run

The handler can be one of the following

  • "f.name" or "filter.name" where 'name' is a method of \Grithin\Filter
  • "v.name" or "validate.name" where 'name' is a method of \Grithin\Validate
  • "g.name" or "global.name" where name is a global user function
  • "l.instance.name" or "local.instance.name" where "instance" is an instance name of $this->localInstances and "name" is the method
  • "class.method" where "class" is the name of a class and "method" is a name of a static method on that class
  • An actual callback-able value. For instance ['\Grithin\Filter','trim']

Example

use \Grithin\Input;


$in = ['bob'=>'sucks'];
$input = new Input(['in'=>$in]);

#validate that the bob value is a float
$rules = ['bob'=>'v.isFloat'];

if($input->handle($rules)){
	die("Yay!");
}else{
	echo 'uh oh...';
	\Grithin\Debug::quit($input->errors);
}
/*
uh oh...

[base:index.php:19] 1: [
	0 : [
		'type' : 'isFloat'
		'fields' : [
			0 : 'bob'
		]
	]
]
*/
use \Grithin\Input;

$passingInput = [];

$passingInput[1] = [
		'phone'=>'my phone number is (555)   555-5555.',
		'appointment_time' => 'August 8, 2020',
		'comment' => '    bob is a crazy    '
	];
$passingInput[2] = [
		'phone'=>'my phone number is (555)   555-5555.',
		'comment' => '    bob is a crazy    '
	];

#validate that the bob value is a float
$rules = [
		'phone'=>'v.phone',
		'appointment_time'=>'?!v.filled,v.date,f.datetime', #< '?' means optional, '!' means stop of not validated
		'comment' => 'f.trim'
	];

$input = new Input(['in'=>$passingInput[1]]);
$input->handle($rules);
\Grithin\Debug::out($input->in);
/*
[base:index.php:34] 1: [
	'phone' : '5555555555'
	'appointment_time' : '2020-08-08 00:00:00'
	'comment' : 'bob is a crazy'
]*/

$input = new Input(['in'=>$passingInput[2]]);
$input->handle($rules);
\Grithin\Debug::out($input->in);
/*
[base:index.php:45] 2: [
	'phone' : '5555555555'
	'comment' : 'bob is a crazy'
]
*/

统计信息

  • 总下载量: 26
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: Apache-2.0
  • 更新时间: 2015-10-09