php类...验证 [英] php classes... validation
问题描述
我使用扩展它的子类(例如mobile,suburb,credit_card,ect
)创建一个php验证类
,您可以致电
$ validation = new Validation('mobile');
$ valid = $ validation-> validate($ number);
$ validation-> type('suburb');
$ valid2 = $ validation-> validate($ suburb);
现在我的想法是
class Validation(){
private $ v_type = null;
function __construct($ type){
$ this-> type($ type);
}
public function type($ type){
$ this-> v_type = new $ type();
}
public function validate($ info){
return $ this-> v_type-> validate($ info);
}
}
作为一个非常基本的例子
但是有更好的方法吗?
,但可以改进。具有实际的验证器胶囊它们自己的验证逻辑是好的。从基类扩展它们不是。让我们实现一个接口。这样,任何类都可以是Validator。
接口IValidate
{
public function validate值);
}
您的验证器将如下所示:
class IsNumeric implements IValidate
{
public function validate($ value)
{
return is_numeric );
}
}
和
class GreaterThan implements IValidate
{
protected $ _value;
public function __construct($ value)
{
$ this-> _value = $ value;
}
public function validate($ value)
{
return $ value> $ this-> _value;
}
}
您仍然有一个主Validator类。与您的示例不同,Validator下面接受多个Validator,这将允许您创建一个过滤器链。
class Validator implements IValidate
{
protected $ _validators;
public function addValidator(IValidate $ validator)
{
$ this-> _validators [] = $ validator;
return $ this;
}
public function validate($ value)
{
foreach($ this-> _validators as $ validator){
if($ validator-> validate ($ value)=== FALSE){
return FALSE;
}
}
return TRUE;
}
}
$ validator = new Validator;
$ validator-> addValidator(new IsNumeric)
- > addValidator(new GreaterThan(5));
var_dump($ validator-> validate('ten')); // FALSE
var_dump($ validator-> validate('10')); // TRUE
var_dump($ validator-> validate('1')); // FALSE
上面几乎是一个命令模式。由于Validator也实施了IValidate,它也是一个组合。您可以从上面获取Validator链并将其堆叠到另一个Validator Chain,例如
$ numericGreaterThanFive = new Validator;
$ numericGreaterThanFive-> addValidator(new IsNumeric)
- > addValidator(new GreaterThan(5));
$ otherValidator = new Validator;
$ otherValidator-> addValidator(new Foo)
- > addValidator(new Bar)
- > addValidator($ numericGreatherThanFive);为了方便起见,您可以添加一个静态工厂方法,用于使用实际的验证命令对象创建验证器(如下所示):
<
在旁注: Zend Framework已经有大量的验证器可以在上构建。由于ZF是组件库,因此您可以使用它们,而无需将整个应用程序迁移到ZF。
im am making a php validation class with sub classes that extend it, eg, mobile, suburb, credit_card, ect
so, the idea is you can call
$validation = new Validation('mobile');
$valid = $validation->validate($number);
$validation->type('suburb');
$valid2 = $validation->validate($suburb);
now my idea for doing this is having
class Validation() {
private $v_type = null;
function __construct($type) {
$this->type($type);
}
public function type($type) {
$this->v_type = new $type();
}
public function validate($info) {
return $this->v_type->validate($info);
}
}
as a very basic example
but is there a better way of doing this?
You could do it this way, but it could be improved. Having the actual validators capsule their own validation logic is good. Extending them from a base class isn't. Let's implement an interface instead. This way, any class can be a Validator.
interface IValidate
{
public function validate($value);
}
Your validators would look like this then:
class IsNumeric implements IValidate
{
public function validate($value)
{
return is_numeric($value);
}
}
and
class GreaterThan implements IValidate
{
protected $_value;
public function __construct($value)
{
$this->_value = $value;
}
public function validate($value)
{
return $value > $this->_value;
}
}
You'd still have a main Validator class. Unlike in your example, the Validator below accepts multiple Validators, which will allow you to create a Filter Chain.
class Validator implements IValidate
{
protected $_validators;
public function addValidator(IValidate $validator)
{
$this->_validators[] = $validator;
return $this;
}
public function validate($value)
{
foreach($this->_validators as $validator) {
if ($validator->validate($value) === FALSE) {
return FALSE;
}
}
return TRUE;
}
}
And this could be used like:
$validator = new Validator;
$validator->addValidator(new IsNumeric)
->addValidator(new GreaterThan(5));
var_dump( $validator->validate('ten') ); // FALSE
var_dump( $validator->validate('10') ); // TRUE
var_dump( $validator->validate('1') ); // FALSE
The above is pretty much a Command pattern. And due to the Validator implementing IValidate as well, it is also a Composite. You could take the Validator chain from above and stack it into another Validator Chain, e.g.
$numericGreaterThanFive = new Validator;
$numericGreaterThanFive->addValidator(new IsNumeric)
->addValidator(new GreaterThan(5));
$otherValidator = new Validator;
$otherValidator->addValidator(new Foo)
->addValidator(new Bar)
->addValidator($numericGreatherThanFive);
For convenience, you could add a static factory method for creating Validators with the actual Validation Command objects (as shown elsewhere).
On a sidenote: the Zend Framework already has an extensive number of Validators you can build on. Since ZF is a component library, you can use them without having to migrate your entire application to ZF.
这篇关于php类...验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!