本文介绍了ZF2中InputFilterAwareInterface和InputFilterProviderInterface之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以解释一下InputFilterAwareInterface和InputFilterProviderInterface这两个接口之间的区别吗?两者似乎都达到了相同的目的,即获得InputFilter,但我知道它们不可能是相同的……而且何时调用它们?

Can someone explain me the difference between both interfaces InputFilterAwareInterface and InputFilterProviderInterface? Both seem to serve to the same purpose, to get an InputFilter, but I know they cannot be the same... And when do they get called?

谢谢

推荐答案

两个接口都存在不同的用途. InputFilterAwareInterface 保证已实现的类将具有接受并返回setInputFilter()getInputFilter()方法必要时使用InputFilter 实例.另一方面, InputFilterProviderInterface 仅保证该实现的类将具有一个getInputFilterSpecification()方法,该方法返回一个过滤器规范(配置数组),该规范可以在各种输入工厂中用作参数.

Both interfaces exist for different purposes. The InputFilterAwareInterface guarantees that implemented classes will have a setInputFilter() and getInputFilter() methods which accept and return an InputFilter instance when necessary. On the other hand, the InputFilterProviderInterface guarantees only that implemented classes will have a getInputFilterSpecification() method which returns a filter specification (configuration array) which is ready to use as argument in various input factories.

例如;下面的摘录来自Zend\Form\Form.php类:

For example; the snippet below came from Zend\Form\Form.php class:

if ($fieldset === $this && $fieldset instanceof InputFilterProviderInterface) {
    foreach ($fieldset->getInputFilterSpecification() as $name => $spec) {
        $input = $inputFactory->createInput($spec);
        $inputFilter->add($input, $name);
    }
}

如您所见,Form类使用给定的规范创建输入并将其绑定到相关的过滤器,该规范由实现类的getInputFilterSpecification()方法返回(在这种情况下为$ fieldset).

As you can see, the Form class creates inputs and binds them to related filter using given specification which is returned by getInputFilterSpecification() method of the implementing class ($fieldset int this case).

使用特征

Zend Framework 2还为常用接口提供了许多特征.例如,针对InputFilterInterface的 InputFilterAwareTrait .这意味着,如果您的PHP> = 5.4

Zend Framework 2 also provides lot of traits for commonly used interfaces. For example InputFilterAwareTrait for InputFilterInterface. This means, you can easily implement that interface if you have PHP >= 5.4

namespace MyNamespace;

use Zend\InputFilter\InputFilterInterface;

MyClass implements InputFilterInterface {

    // Here is the trait which provides set and getInputFilter methods
    // with a protected $inputFilter attribute to all MyClass instances.

    use \Zend\InputFilter\InputFilterAwareTrait;

    // Your other methods.
    ...
}

现在在代码中的任何地方,您都可以执行以下操作:

Now anywhere in your code, you can do this:

$myClass->setInputFilter($AnInputFilterInstance);
$myClass->getInputFilter(); // Returns an inputfilter instance.

您可以想象,InputFilterProviderInterface不存在任何特征,因为它的职责只是返回有效的配置规范.它不处理任何实例或类属性,就像InputFilterInterface中强制执行的那样.

As you can imagine, no trait exists for InputFilterProviderInterface because its responsibility is only returning a valid config spec. It doesn't deal with any instance or class attribute like is forced in InputFilterInterface.

这篇关于ZF2中InputFilterAwareInterface和InputFilterProviderInterface之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-11 01:21