ZF-213: Trim on construction

Description

Me again. I'm making a lot of use of Zend_Filter_Input atm so noticing a lot of stuff. It would be really nice if you could optionally trim all the elements of the source array.

I extended Zend_Filter_Input and actually did this


class SomeClass extend Zend_Filter_Input 
{
    const TRIM = 1;
    const WHITESPACE = 2;
    /**
     * @param array $source the data you want to filter
     * @param int $trimAll data cleaning method, use class constant
     */
    public function __construct(array &$source, $cleaning) 
    {
        switch ($cleaning) {
            case self::TRIM: 
                foreach ($source as $key => $value) {
                    if (is_string($value)) {    // preserve non-string types
                        $source[$key] = trim($value);
                    }
                }
                break;
            case self::WHITESPACE:
                foreach ($source as $key => $value) {
                    if (is_string($value)) {    // preserve non-string types
                        $source[$key] = preg_replace('/\s{2,}/', ' ', $value);
                    }
                }
                break;
            default:
                // no action
                break;
        }
        $this->_source = $source;
        $source = null;
    }

Comments

Extending the class for your specific purposes is exactly the right thing to do here. Since we will have a major refactoring of the filtering and validation technologies for 0.9.0, I do not want to mislead by placing this new and specific functionality into the existing design. Thus, instead of adding this to the current implementation, I have made a note to include such commonly-needed functionality into the new design.