<?xml version="1.0" encoding="UTF-8"?>
<!-- Reviewed: no -->
<sect1 id="zend.filter.introduction">
    <title>Introduction</title>

    <para>
        The <classname>Zend_Filter</classname> component provides a set of commonly needed data
        filters. It also provides a simple filter chaining mechanism by which
        multiple filters may be applied to a single datum in a user-defined
        order.
    </para>

    <sect2 id="zend.filter.introduction.definition">
        <title>What is a filter?</title>

        <para>
            In the physical world, a filter is typically used for removing
            unwanted portions of input, and the desired portion of the input
            passes through as filter output (e.g., coffee). In such scenarios,
            a filter is an operator that produces a subset of the input. This
            type of filtering is useful for web applications - removing illegal
            input, trimming unnecessary white space, etc.
        </para>

        <para>
            This basic definition of a filter may be extended to include
            generalized transformations upon input. A common transformation
            applied in web applications is the escaping of <acronym>HTML</acronym> entities. For
            example, if a form field is automatically populated with untrusted
            input (e.g., from a web browser), this value should either be free
            of <acronym>HTML</acronym> entities or contain only escaped <acronym>HTML</acronym>
            entities, in order to prevent undesired behavior and security vulnerabilities. To meet
            this requirement, <acronym>HTML</acronym> entities that appear in the input must
            either be removed or escaped. Of course, which approach is more
            appropriate depends on the situation. A filter that removes the
            <acronym>HTML</acronym> entities operates within the scope of the first definition of
            filter - an operator that produces a subset of the input. A filter
            that escapes the <acronym>HTML</acronym> entities, however, transforms the input
            (e.g., "&amp;" is transformed to
            "&amp;amp;"). Supporting such use cases for web
            developers is important, and "to filter," in the context of using
            <classname>Zend_Filter</classname>, means to perform some transformations upon input
            data.
        </para>
    </sect2>

    <sect2 id="zend.filter.introduction.using">
        <title>Basic usage of filters</title>

        <para>
            Having this filter definition established provides the foundation
            for <classname>Zend_Filter_Interface</classname>, which requires a single
            method named <methodname>filter()</methodname> to be implemented by a filter
            class.
        </para>

        <para>
            Following is a basic example of using a filter upon two input data,
            the ampersand (<emphasis>&amp;</emphasis>) and double quote
            (<emphasis>&quot;</emphasis>) characters:
        </para>

        <programlisting language="php"><![CDATA[
$htmlEntities = new Zend_Filter_HtmlEntities();

echo $htmlEntities->filter('&'); // &amp;
echo $htmlEntities->filter('"'); // &quot;
]]></programlisting>
    </sect2>

    <sect2 id="zend.filter.introduction.static">
        <title>Using the static staticFilter() method</title>

        <para>
            If it is inconvenient to load a given filter class and create an
            instance of the filter, you can use the static method
            <methodname>Zend_Filter::filterStatic()</methodname> as an alternative invocation style.
            The first argument of this method is a data input value, that you
            would pass to the <methodname>filter()</methodname> method. The second
            argument is a string, which corresponds to the basename of the
            filter class, relative to the <classname>Zend_Filter</classname> namespace. The
            <methodname>staticFilter()</methodname> method automatically loads the class, creates
            an instance, and applies the <methodname>filter()</methodname> method to the data
            input.
        </para>

        <programlisting language="php"><![CDATA[
echo Zend_Filter::filterStatic('&', 'HtmlEntities');
]]></programlisting>

        <para>
            You can also pass an array of constructor arguments, if they
            are needed for the filter class.
        </para>

        <programlisting language="php"><![CDATA[
echo Zend_Filter::filterStatic('"',
                               'HtmlEntities',
                               array('quotestyle' => ENT_QUOTES));
]]></programlisting>

        <para>
            The static usage can be convenient for invoking a filter ad hoc,
            but if you have the need to run a filter for multiple inputs,
            it's more efficient to follow the first example above,
            creating an instance of the filter object and calling its
            <methodname>filter()</methodname> method.
        </para>

        <para>
            Also, the <classname>Zend_Filter_Input</classname> class allows you to instantiate and
            run multiple filter and validator classes on demand to process
            sets of input data. See <link linkend="zend.filter.input">Zend_Filter_Input</link>.
        </para>

        <sect3 id="zend.filter.introduction.static.namespaces">
            <title>Namespaces</title>

            <para>
                When working with self defined filters you can give a fourth parameter
                to <methodname>Zend_Filter::filterStatic()</methodname> which is the namespace
                where your filter can be found.
            </para>

            <programlisting language="php"><![CDATA[
echo Zend_Filter::filterStatic(
    '"',
    'MyFilter',
    array($parameters),
    array('FirstNamespace', 'SecondNamespace')
);
]]></programlisting>

            <para>
                <classname>Zend_Filter</classname> allows also to set namespaces as default.
                This means that you can set them once in your bootstrap and have not to give
                them again for each call of <methodname>Zend_Filter::filterStatic()</methodname>.
                The following code snippet is identical to the above one.
            </para>

            <programlisting language="php"><![CDATA[
Zend_Filter::setDefaultNamespaces(array('FirstNamespace', 'SecondNamespace'));
echo Zend_Filter::filterStatic('"', 'MyFilter', array($parameters));
echo Zend_Filter::filterStatic('"', 'OtherFilter', array($parameters));
]]></programlisting>

            <para>
                For your convenience there are following methods which allow the handling of
                namespaces:
            </para>

            <itemizedlist>
                <listitem>
                    <para>
                        <emphasis><methodname>Zend_Filter::getDefaultNamespaces()</methodname></emphasis>:
                        Returns all set default namespaces as array.
                    </para>
                </listitem>

                <listitem>
                    <para>
                        <emphasis><methodname>Zend_Filter::setDefaultNamespaces()</methodname></emphasis>:
                        Sets new default namespaces and overrides any previous set. It accepts
                        either a string for a single namespace of an array for multiple namespaces.
                    </para>
                </listitem>

                <listitem>
                    <para>
                        <emphasis><methodname>Zend_Filter::addDefaultNamespaces()</methodname></emphasis>:
                        Adds additional namespaces to already set ones. It accepts either a string
                        for a single namespace of an array for multiple namespaces.
                    </para>
                </listitem>

                <listitem>
                    <para>
                        <emphasis><methodname>Zend_Filter::hasDefaultNamespaces()</methodname></emphasis>:
                        Returns <constant>TRUE</constant> when one or more default namespaces are
                        set, and <constant>FALSE</constant> when no default namespaces are set.
                    </para>
                </listitem>
            </itemizedlist>
        </sect3>
    </sect2>

    <sect2 id="zend.filter.introduction.caveats">
        <title>Double filtering</title>

        <para>
            When using two filters after each other you have to keep in mind that it is often not
            possible to get the original output by using the opposite filter. Take the following
            example:
        </para>

        <programlisting language="php"><![CDATA[
$original = "my_original_content";

// Attach a filter
$filter   = new Zend_Filter_Word_UnderscoreToCamelCase();
$filtered = $filter->filter($original);

// Use it's opposite
$filter2  = new Zend_Filter_Word_CamelCaseToUnderscore();
$filtered = $filter2->filter($filtered)
]]></programlisting>

        <para>
            The above code example could lead to the impression that you will get the original
            output after the second filter has been applied. But thinking logically this is not the
            case. After applying the first filter <emphasis>my_original_content</emphasis> will be
            changed to <emphasis>MyOriginalContent</emphasis>. But after applying the second filter
            the result is <emphasis>My_Original_Content</emphasis>.
        </para>

        <para>
            As you can see it is not always possible to get the original output by using a filter
            which seems to be the opposite. It depends on the filter and also on the given input.
        </para>
    </sect2>
</sect1>
<!--
vim:se ts=4 sw=4 et:
-->
