Introdução ao Zend Framework

 Conhecendo o Zend Framework


 Referência do Zend Framework

  • Zend_Gdata
  • Zend_Http
  • Zend_InfoCard
  • Zend_Json
  • Zend_Layout
  • Zend_Ldap
  • Zend_Loader
  • Zend_Locale
  • Zend_Log
  • Zend_Mail
  • Zend_Markup
  • Zend_Measure
  • Zend_Memory
  • Zend_Mime
  • Zend_Navigation
  • Zend_Oauth
  • Zend_OpenId
  • Zend_Paginator
  • Zend_Pdf
  • Zend_ProgressBar
  • Zend_Queue
  • Zend_Reflection
  • Zend_Registry
  • Zend_Rest

  • Zend_Search_Lucene
  • Zend_Serializer
  • Zend_Server
  • Zend_Service
  • Zend_Session
  • Zend_Soap
  • Zend_Tag
  • Zend_Test
  • Zend_Text
  • Zend_TimeSync
  • Zend_Tool
  • Zend_Tool_Framework
  • Zend_Tool_Project
  • Zend_Translate
  • Zend_Uri
  • Zend_Validate
  • Zend_Version
  • Zend_View
  • Zend_Wildfire
  • Zend_XmlRpc
  • ZendX_Console_Process_Unix
  • ZendX_JQuery
  • Translation 12.2% Update 2011-11-16 - Revision 24447 - Version ZF 1.11.x

    Capítulo 34. Zend_Filter


    34.1. Introduction
    34.1.1. What is a filter?
    34.1.2. Basic usage of filters
    34.1.3. Using the static staticFilter() method Namespaces
    34.1.4. Double filtering
    34.2. Standard Filter Classes
    34.2.1. Alnum Supported options for Zend_Filter_Alnum Basic usage Allow whitespaces
    34.2.2. Alpha Supported options for Zend_Filter_Alpha Basic usage Allow whitespace characters
    34.2.3. BaseName Supported options for Zend_Filter_BaseName Basic usage
    34.2.4. Boolean Supported options for Zend_Filter_Boolean Default behaviour for Zend_Filter_Boolean Changing behaviour for Zend_Filter_Boolean Localized booleans Disable casting
    34.2.5. Callback Supported options for Zend_Filter_Callback Basic usage Default parameters within a callback
    34.2.6. Compress and Decompress Supported options for Zend_Filter_Compress and Zend_Filter_Decompress Supported compression adapters Generic handling Creating an archive Decompressing an archive Bz2 Adapter Gz Adapter Lzf Adapter Rar Adapter Tar Adapter Zip Adapter
    34.2.7. Digits Supported options for Zend_Filter_Digits Basic usage
    34.2.8. Dir Supported options for Zend_Filter_Dir Basic usage
    34.2.9. Encrypt and Decrypt Supported options for Zend_Filter_Encrypt and Zend_Filter_Decrypt Adapter usage Encryption with Mcrypt Decryption with Mcrypt Encryption with OpenSSL Simplified usage with Openssl Compressing the content Decryption with OpenSSL
    34.2.10. HtmlEntities Supported options for Zend_Filter_HtmlEntities Basic usage Quote Style Helper Methods
    34.2.11. Int Supported options for Zend_Filter_Int Basic usage
    34.2.12. LocalizedToNormalized and NormalizedToLocalized Supported options for Zend_Filter_LocalizedToNormalized and Zend_Filter_NormalizedToLocalized Workflow Normalization for numbers Normalization for date and time Localization for numbers Localization for date and time
    34.2.13. Null Supported options for Zend_Filter_Null Default behaviour for Zend_Filter_Null Changing behaviour for Zend_Filter_Null
    34.2.14. PregReplace Supported options for Zend_Filter_PregReplace Basic usage
    34.2.15. RealPath Supported options for Zend_Filter_RealPath Basic usage Non existing paths
    34.2.16. StringToLower Supported options for Zend_Filter_StringToLower Basic usage Different encoded strings
    34.2.17. StringToUpper Supported options for Zend_Filter_StringToUpper Basic usage Different encoded strings
    34.2.18. StringTrim Supported options for Zend_Filter_StringTrim Basic usage Default behaviour for Zend_Filter_StringTrim
    34.2.19. StripNewLines Supported options for Zend_Filter_StripNewLines Basic usage
    34.2.20. StripTags Supported options for Zend_Filter_StripTags Basic usage Allowing defined tags Allowing defined attributes
    34.3. Filter Chains
    34.3.1. Changing filter chain order
    34.4. Writing Filters
    34.5. Zend_Filter_Input
    34.5.1. Declaring Filter and Validator Rules
    34.5.2. Creating the Filter and Validator Processor
    34.5.3. Retrieving Validated Fields and other Reports Querying if the input is valid Getting Invalid, Missing, or Unknown Fields Getting Valid Fields
    34.5.4. Using Metacommands to Control Filter or Validator Rules The FIELDS metacommand The PRESENCE metacommand The DEFAULT_VALUE metacommand The ALLOW_EMPTY metacommand The BREAK_CHAIN metacommand The MESSAGES metacommand Using options to set metacommands for all rules
    34.5.5. Adding Filter Class Namespaces
    34.6. Zend_Filter_Inflector
    34.6.1. Operation
    34.6.2. Setting Paths To Alternate Filters
    34.6.3. Setting the Inflector Target
    34.6.4. Inflection Rules Static Rules Filter Inflector Rules Setting Many Rules At Once
    34.6.5. Utility Methods
    34.6.6. Using Zend_Config with Zend_Filter_Inflector

    34.1. Introduction

    The Zend_Filter 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.

    34.1.1. What is a filter?

    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.

    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 HTML 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 HTML entities or contain only escaped HTML entities, in order to prevent undesired behavior and security vulnerabilities. To meet this requirement, HTML 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 HTML 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 HTML entities, however, transforms the input (e.g., "&" is transformed to "&"). Supporting such use cases for web developers is important, and "to filter," in the context of using Zend_Filter, means to perform some transformations upon input data.

    34.1.2. Basic usage of filters

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

    Following is a basic example of using a filter upon two input data, the ampersand (&) and double quote (") characters:

    $htmlEntities = new Zend_Filter_HtmlEntities();

    $htmlEntities->filter('&'); // &
    echo $htmlEntities->filter('"'); // "

    34.1.3. Using the static staticFilter() method

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

    echo Zend_Filter::filterStatic('&''HtmlEntities');

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

    echo Zend_Filter::filterStatic('"',
    'quotestyle' => ENT_QUOTES));

    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 filter() method.

    Also, the Zend_Filter_Input class allows you to instantiate and run multiple filter and validator classes on demand to process sets of input data. See Zend_Filter_Input. Namespaces

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

    echo Zend_Filter::filterStatic(

    Zend_Filter 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 Zend_Filter::filterStatic(). The following code snippet is identical to the above one.

    Zend_Filter::filterStatic('"''MyFilter', array($parameters));
    Zend_Filter::filterStatic('"''OtherFilter', array($parameters));

    For your convenience there are following methods which allow the handling of namespaces:

    • Zend_Filter::getDefaultNamespaces(): Returns all set default namespaces as array.

    • Zend_Filter::setDefaultNamespaces(): Sets new default namespaces and overrides any previous set. It accepts either a string for a single namespace of an array for multiple namespaces.

    • Zend_Filter::addDefaultNamespaces(): Adds additional namespaces to already set ones. It accepts either a string for a single namespace of an array for multiple namespaces.

    • Zend_Filter::hasDefaultNamespaces(): Returns TRUE when one or more default namespaces are set, and FALSE when no default namespaces are set.

    34.1.4. Double filtering

    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:

    $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)

    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 my_original_content will be changed to MyOriginalContent. But after applying the second filter the result is My_Original_Content.

    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.

    digg delicious meneame google twitter technorati facebook