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?
Thanks
Both interfaces exist for different purposes. The InputFilterAwareInterface guarantees that implemented classes will have a
setInputFilter()
andgetInputFilter()
methods which accept and return an InputFilter instance when necessary. On the other hand, the InputFilterProviderInterface guarantees only that implemented classes will have agetInputFilterSpecification()
method which returns a filter specification (configuration array) which is ready to use as argument in various input factories.For example; the snippet below came from
Zend\Form\Form.php
class: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).Using Traits
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
Now anywhere in your code, you can do this:
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.