Introducción a Zend Framework

 Aprendiendo Zend Framework

Apéndice

 Referencia de 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
  • Traducción al 26.9% - Actualizado el 2011-11-16 - Revisión 24249 - Versión ZF 1.11.x

    Capítulo 34. Zend_Filter

    Tabla de contenidos

    34.1. Introducción
    34.1.1. ¿Qué es un filtro?
    34.1.2. Uso básico de los filtros
    34.1.3. Usando el método estático staticFilter()
    34.1.3.1. Namespaces
    34.2. Standard Filter Classes
    34.2.1. Alnum
    34.2.1.1. Supported options for Zend_Filter_Alnum
    34.2.1.2. Basic usage
    34.2.1.3. Allow whitespaces
    34.2.2. Alpha
    34.2.2.1. Supported options for Zend_Filter_Alpha
    34.2.2.2. Basic usage
    34.2.2.3. Allow whitespace characters
    34.2.3. BaseName
    34.2.3.1. Supported options for Zend_Filter_BaseName
    34.2.3.2. Basic usage
    34.2.4. Boolean
    34.2.4.1. Supported options for Zend_Filter_Boolean
    34.2.4.2. Default behaviour for Zend_Filter_Boolean
    34.2.4.3. Changing behaviour for Zend_Filter_Boolean
    34.2.4.4. Localized booleans
    34.2.4.5. Disable casting
    34.2.5. Callback
    34.2.5.1. Supported options for Zend_Filter_Callback
    34.2.5.2. Basic usage
    34.2.5.3. Default parameters within a callback
    34.2.6. Compress and Decompress
    34.2.6.1. Supported options for Zend_Filter_Compress and Zend_Filter_Decompress
    34.2.6.2. Supported compression adapters
    34.2.6.3. Generic handling
    34.2.6.4. Creating an archive
    34.2.6.5. Decompressing an archive
    34.2.6.6. Bz2 Adapter
    34.2.6.7. Gz Adapter
    34.2.6.8. Lzf Adapter
    34.2.6.9. Rar Adapter
    34.2.6.10. Tar Adapter
    34.2.6.11. Zip Adapter
    34.2.7. Digits
    34.2.7.1. Supported options for Zend_Filter_Digits
    34.2.7.2. Basic usage
    34.2.8. Dir
    34.2.8.1. Supported options for Zend_Filter_Dir
    34.2.8.2. Basic usage
    34.2.9. Encrypt and Decrypt
    34.2.9.1. Supported options for Zend_Filter_Encrypt and Zend_Filter_Decrypt
    34.2.9.2. Adapter usage
    34.2.9.3. Encryption with Mcrypt
    34.2.9.4. Decryption with Mcrypt
    34.2.9.5. Encryption with OpenSSL
    34.2.9.5.1. Simplified usage with Openssl
    34.2.9.5.2. Compressing the content
    34.2.9.6. Decryption with OpenSSL
    34.2.10. HtmlEntities
    34.2.10.1. Supported options for Zend_Filter_HtmlEntities
    34.2.10.2. Basic usage
    34.2.10.3. Quote Style
    34.2.10.4. Helper Methods
    34.2.11. Int
    34.2.11.1. Supported options for Zend_Filter_Int
    34.2.11.2. Basic usage
    34.2.12. LocalizedToNormalized and NormalizedToLocalized
    34.2.12.1. Supported options for Zend_Filter_LocalizedToNormalized and Zend_Filter_NormalizedToLocalized
    34.2.12.2. Workflow
    34.2.12.3. Normalization for numbers
    34.2.12.4. Normalization for date and time
    34.2.12.5. Localization for numbers
    34.2.12.6. Localization for date and time
    34.2.13. Null
    34.2.13.1. Supported options for Zend_Filter_Null
    34.2.13.2. Default behaviour for Zend_Filter_Null
    34.2.13.3. Changing behaviour for Zend_Filter_Null
    34.2.14. PregReplace
    34.2.14.1. Supported options for Zend_Filter_PregReplace
    34.2.14.2. Basic usage
    34.2.15. RealPath
    34.2.15.1. Supported options for Zend_Filter_RealPath
    34.2.15.2. Basic usage
    34.2.15.3. Non existing paths
    34.2.16. StringToLower
    34.2.16.1. Supported options for Zend_Filter_StringToLower
    34.2.16.2. Basic usage
    34.2.16.3. Different encoded strings
    34.2.17. StringToUpper
    34.2.18. StringTrim
    34.2.18.1. Supported options for Zend_Filter_StringTrim
    34.2.18.2. Basic usage
    34.2.18.3. Default behaviour for Zend_Filter_StringTrim
    34.2.19. StripNewLines
    34.2.19.1. Supported options for Zend_Filter_StripNewLines
    34.2.19.2. Basic usage
    34.2.20. StripTags
    34.2.20.1. Supported options for Zend_Filter_StripTags
    34.2.20.2. Basic usage
    34.2.20.3. Allowing defined tags
    34.2.20.4. 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
    34.5.3.1. Querying if the input is valid
    34.5.3.2. Getting Invalid, Missing, or Unknown Fields
    34.5.3.3. Getting Valid Fields
    34.5.4. Using Metacommands to Control Filter or Validator Rules
    34.5.4.1. The FIELDS metacommand
    34.5.4.2. The PRESENCE metacommand
    34.5.4.3. The DEFAULT_VALUE metacommand
    34.5.4.4. The ALLOW_EMPTY metacommand
    34.5.4.5. The BREAK_CHAIN metacommand
    34.5.4.6. The MESSAGES metacommand
    34.5.4.7. 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
    34.6.4.1. Static Rules
    34.6.4.2. Filter Inflector Rules
    34.6.4.3. Setting Many Rules At Once
    34.6.5. Utility Methods
    34.6.6. Using Zend_Config with Zend_Filter_Inflector

    34.1. Introducción

    El componente Zend_Filter proporciona un conjunto de filtros de datos comúnmente necesarios. También proporciona un sencillo mecanismo de encadenar varios filtros que se puedan aplicar a un solo dato en un orden definido por el usuario.

    34.1.1. ¿Qué es un filtro?

    En el mundo físico, un filtro se suele utilizar para eliminar partes no deseadas de lo ingresado, y la vez lo ingresado pasa a través de un filtro de salida (por ejemplo, el café). En este caso, un filtro es un operador que devuelve una parte de los datos de entrada. Este tipo de filtro es útil para aplicaciones web, para la supresión de entradas ilegales y/o que no se ajustan, eliminación de los espacios en blanco innecesarios, 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.

    Esta definición básica de un filtro puede ser extendido para incluir transformaciones generalizadas sobre la entrada. Una transformación común requerida en las aplicaciones web es la de escapar las entidades HTML . Por ejemplo, si un campo del formulario es completado automáticamente y contiene datos no verificados (por ejemplo, datos ingresados desde un navegador web), este valor debe estar libre de las entidades HTML o sólo contener entidades HTML de forma escapada, a fin de evitar comportamientos no deseados y vulnerabilidades de seguridad. Para cumplir este requerimiento, las entidades HTML que aparecen en los datos introducidos deben ser suprimidos o escapados. Por supuesto, el enfoque más adecuado depende del contexto y de la situción. Un filtro que quita las entidades HTML funciona dentro del ámbito o alcance de la primera definición del filtro - un operador que produce un subconjunto de la entrada. Un filtro que escapa a las entidades HTML , sin embargo, transforma la entrada (por ejemplo, " & " se transforma en " & "). El Apoyo a los casos de uso como para la web los desarrolladores es importante, y "filtrar", en el contexto de la utilización de Zend_Filter , los medios para realizar algunas transformaciones en los datos de entrada.

    34.1.2. Uso básico de los filtros

    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();

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

    34.1.3. Usando el método estático staticFilter()

    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('"''HtmlEntities', array(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 Sección 34.5, “Zend_Filter_Input” .

    34.1.3.1. Namespaces

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

    echo Zend_Filter::filterStatic(
        
    '"',
        
    'MyFilter',
        array(
    $parameters),
        array(
    'FirstNamespace''SecondNamespace')
    );

    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::setDefaultNamespaces(array('FirstNamespace''SecondNamespace'));
    echo 
    Zend_Filter::filterStatic('"''MyFilter', array($parameters));
    echo 
    Zend_Filter::filterStatic('"''OtherFilter', array($parameters));

    For your convinience 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 eighter a string for a single namespace of an array for multiple namespaces.

    • Zend_Filter::addDefaultNamespaces() : Adds additional namespaces to already set ones. It accepts eighter 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.

    digg delicious meneame google twitter technorati facebook

    Comentarios

    Loading...