View Source

<ac:macro ac:name="unmigrated-inline-wiki-markup"><ac:plain-text-body><![CDATA[{zone-template-instance:ZFDEV:Zend Proposal Zone Template v2}

{zone-data:component-name}
Zend_Filter static usage
{zone-data}

{zone-data:proposer-list}
[Bill Karwin|mailto:bill.k@zend.com]
[Darby Felton|mailto:darby@zend.com]
{zone-data}

{zone-data:revision}
1.1 - 12 April 2007: initial writeup.
{zone-data}

{zone-data:overview}
This is a proposed solution for a convenient static method to allow Zend_Filter and Zend_Validate to work in an ad-hoc fashion.

This proposal is intended to *supplement*, not to replace the current object-oriented usage of Zend_Filter and Zend_Validate. You would still create instances of classes that implement Zend_Filter_Interface and Zend_Validate_Interface and use them as objects, call their {{isValid()}} and {{filter()}} methods, etc.
{zone-data}

{zone-data:references}
* tbd
{zone-data}

{zone-data:requirements}
* This component *will* create an static interface to filtering and validating.
* This component *will* load, instantiate, and invoke the Filter or Validate objects.
* This component *will* support user-defined Filter and Validate classes in namespaces other than Zend_Filter and Zend_Validate.
{zone-data}

{zone-data:dependencies}
* Zend_Filter_Interface
* Zend_Filter_Exception
* Zend_Validate_Interface
* Zend_Validate_Exception
* other concrete classes that implement Zend_Filter_Interface and Zend_Validate_Interface
{zone-data}

{zone-data:operation}
The {{call()}} method in classes Zend_Filter and Zend_Validate provide a static interface to invoke a single class that implement Zend_Filter_Interface or Zend_Validate_Interface respectively in their default usage. The desired filter or validator is specified by the class basename.

{code}
$newValue = Zend_Filter::call($oldValue, 'filtername');
{code}

In the example above, 'filtername' is transformed to 'Zend_Filter_Filtername' and an instance is created. Then its {{filter()}} method is called on the {{$oldValue}} provided and the value returned.

If no such filter class can be found, an exception is thrown.

Some filters take arguments in their constructor. Pass these arguments as an array in the third argument to the {{call()}} method.

{code}
$newValue = Zend_Filter::call($oldValue, 'filtername', array('foo', 'bar'));
{code}

You can make the {{call()}} method search for classes in other namespaces than Zend_Filter and Zend_Validate, by passing a string or array as the fourth argument. User-specified namespaces are searched before Zend_Filter or Zend_Validate namespaces.

{code}
$newValue = Zend_Filter::call($oldValue, 'filtername', array(), array('My_Filters', 'Third_Party_Filters'));
{code}

If the filter or validator takes no arguments, you must pass an empty array. A {{null}} or {{false}} is interpreted as an argument with that value to pass to the constructors.

{zone-data}

{zone-data:milestones}
* Milestone 1: Post prototype design, gather community feedback
* Milestone 2: Working prototype checked into the incubator supporting use cases
* Milestone 3: Unit tests exist, work, and are checked into SVN.
* Milestone 4: Write documentation.

If a milestone is already done, begin the description with "\[DONE\]", like this:
* Milestone #: \[DONE\] Unit tests ...
{zone-data}

{zone-data:class-list}
* Zend_Filter
* Zend_Validate
{zone-data}

{zone-data:use-cases}

||UC-01||

Apply a single Filter to a single value.

{code}
<?php
error_reporting( E_ALL | E_STRICT );
require_once 'Zend/Filter';

$string =

$string = Zend_Filter::call($string, 'htmlEntities');

?>
{code}

||UC-02||

Apply a single Validator to a single value.

{code}
<?php
error_reporting( E_ALL | E_STRICT );
require_once 'Zend/Validate.php';

echo '6 is between 1 and 12? ';

$isBetween = Zend_Validate::call(6, 'between', array(1, 12));

echo ($isBetween ? 'yes' : 'no') . "\n";

?>
{code}

||UC-03||

Use a user-defined validate class in a different namespace.

{code}
<?php
error_reporting( E_ALL | E_STRICT );
require_once 'Zend/Validate.php';

// Must pass empty array for validator options.
$bool = Zend_Validate::call($value, 'myValidator', array(), 'My_Validators');

{code}

{zone-data}

{zone-data:skeletons}

||Zend_Filter||

The following method is added to the Zend_Filter class. The existing methods of that class are not changed.

{code}
/**
* @param mixed $value
* @param string $classBaseName
* @param array $args OPTIONAL
* @param mixed $userPackages OPTIONAL
* @return boolean
* @throws Zend_Filter_Exception
*/
public static function call($value, $classBaseName, array $args = array(), $userPackages = array())
{
$packages = array_merge((array) $userPackages, array('Zend_Filter'));
foreach ($packages as $package) {
$className = $package . '_' . ucfirst($classBaseName);
try {
require_once 'Zend/Loader.php';
Zend_Loader::loadClass($className);
$class = new ReflectionClass($className);
$filter = $class->newInstanceArgs((array) $args);
if ($filter instanceof Zend_Filter_Interface) {
return $filter->filter($value);
}
} catch (Zend_Exception $ze) {
// fallthrough and continue
}
}
throw new Zend_Filter_Exception("Filter class not found from basename '$classBaseName'");
}
{code}

||Zend_Validate||

The following method is added to the Zend_Validate class. The existing methods of that class are not changed.

{code}
/**
* @param mixed $value
* @param string $classBaseName
* @param array $args OPTIONAL
* @param mixed $userPackages OPTIONAL
* @return boolean
* @throws Zend_Validate_Exception
*/
public static function call($value, $classBaseName, array $args = array(), $userPackages = array())
{
$packages = array_merge((array) $userPackages, array('Zend_Validate'));
foreach ($packages as $package) {
$className = $package . '_' . ucfirst($classBaseName);
try {
require_once 'Zend/Loader.php';
Zend_Loader::loadClass($className);
$class = new ReflectionClass($className);
$validator = $class->newInstanceArgs((array) $args);
if ($validator instanceof Zend_Validate_Interface) {
return $validator->isValid($value);
}
} catch (Zend_Exception $ze) {
// fallthrough and continue
}
}
throw new Zend_Validate_Exception("Validate class not found from basename '$classBaseName'");
}
{code}

{zone-data}

{zone-data:testing}
To be written.
{zone-data}

{zone-template-instance}]]></ac:plain-text-body></ac:macro>