A free and open-source book on ZF3 for beginners

8.2. Standard Filters Overview

Standard filters implementing the FilterInterface interface belong to Zend\Filter namespace 26. A filter class inheritance diagram is shown in figure 8.1. From that figure, you can see that base concrete class for most standard filters is the AbstractFilter class, which implements the FilterInterface interface 27.

Figure 8.1. Filter class inheritance Figure 8.1. Filter class inheritance

26) In this section, we only consider the standard filters belonging to the Zend\Filter namespace, although there are other filters that can also be considered standard. For example, the Zend\Filter\File namespace contains several filters applicable to processing file uploads (those filters will be covered in the next chapters). Additionally, the Zend\I18n component defines several filter classes that are aware of the user's locale.

27) From figure 8.1, you may also notice that there are several more base filters: AbstractUnicode filter is the base class for the StringToUpper and StringToLower filters, because it provides the string conversion functionality common to both of them. And, the Decompress filter inherits from the Compress filter, because these filters are in fact very similar. By analogy, the Decrypt filter inherits from the Encrypt filter, because they are the "mirror reflection" of each other as well.

You may notice that there is a strange filter called StaticFilter which does not inherit from AbstractFilter base class. This is because the StaticFilter class is actually a "wrapper" (it is designed to be a proxy to another filter without explicit instantiation of that filter).

Standard filters provided by the Zend\Filter component, along with a brief description of each, are listed in table 8.1.

As you can see from the table, the standard filters can be roughly divided into the following groups:

Table 8.1. Standard filters
Class name Description
Boolean Returns a boolean representation of $value.
ToInt Casts the input $value to int.
Digits Returns the string $value, removing all but digit characters.
ToNull Returns null if the input value can be treated as null; otherwise returns the $value itself.
DateTimeFormatter Takes a date & time string in an arbitrary format and produces a date & time string in a given format.
BaseName Given a string containing the path to a file or directory, this filter will return the trailing name component.
Dir Given a string containing the path of a file or directory, this filter will return the parent directory's path.
RealPath Returns canonicalized absolute pathname.
Compress Compresses the input data with the specified algorithm (GZ by default).
Decompress Decompresses the input data with the specified algorithm (the effect is inverse to the Compress filter).
Encrypt Encrypts the input data with the specified cryptographic algorithm.
Decrypt Decrypts the input data previously encrypted with the specified cryptographic algorithm.
Inflector Performs the modification of a word to express different grammatical categories such as tense, mood, voice, aspect, person, number, gender, and case.
PregReplace Performs a regular expression search and replace.
StringToLower Converts the string to lowercase letters.
StringToUpper Converts the string to uppercase letters.
StringTrim Removes white spaces (space, tabs, etc.) from the beginning and the end of the string.
StripNewlines Removes new line characters from string (ASCII codes #13, #10).
HtmlEntities Returns the string, converting characters to their corresponding HTML entity equivalents where they exist.
StripTags Removes tags (e.g., <a></a>) and comments (e.g., <!-- -->).
UriNormalize Converts a URL string to the "normalized" form and prepends the schema part (e.g., converts www.example.com to http://www.example.com).
Callback Allows to use a callback function as a filter.
FilterChain Allows to organize several filters in a chain.
StaticFilter Returns a value filtered through a specified filter class without requiring separate instantiation of the filter object.