Skip to content

Commit

Permalink
Migrated StaticValidator to PluginBroker
Browse files Browse the repository at this point in the history
- Created ValidatorLoader and ValidatorBroker; will re-use with
  Zend\Filter\InputFilter and Zend\Form
  • Loading branch information
weierophinney committed Oct 28, 2010
1 parent 41dfc8d commit 21201c6
Show file tree
Hide file tree
Showing 4 changed files with 348 additions and 74 deletions.
71 changes: 19 additions & 52 deletions library/Zend/Validator/StaticValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,9 @@
*/
namespace Zend\Validator;

use Zend\Loader;
use Zend\Loader\Broker;

/**
* @uses \Zend\Loader
* @uses \Zend\Validator\AbstractValidator
* @uses \Zend\Validator\Exception
* @uses \Zend\Validator\Validator
* @category Zend
* @package Zend_Validate
* @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
Expand All @@ -39,34 +35,32 @@
class StaticValidator
{
/**
* @var Zend\Loader\ShortNameLocater
* @var Zend\Loader\Broker
*/
protected static $_pluginLoader;
protected static $broker;

/**
* Set plugin loader to use for locating validators
* Set plugin broker to use for locating validators
*
* @param Loader\ShortNameLocater|null $loader
* @param Broker|null $broke
* @return void
*/
public static function setPluginLoader(Loader\ShortNameLocater $loader = null)
public static function setBroker(Broker $broker = null)
{
self::$_pluginLoader = $loader;
self::$broker = $broker;
}

/**
* Get plugin loader for locating validators
* Get plugin broker for locating validators
*
* @return Loader\ShortNameLocater
* @return Broker
*/
public static function getPluginLoader()
public static function getBroker()
{
if (null === self::$_pluginLoader) {
static::setPluginLoader(new Loader\PluginLoader(array(
'Zend\Validator' => 'Zend/Validator',
)));
if (null === self::$broker) {
static::setBroker(new ValidatorBroker());
}
return self::$_pluginLoader;
return self::$broker;
}

/**
Expand All @@ -78,41 +72,14 @@ public static function getPluginLoader()
*/
public static function execute($value, $classBaseName, array $args = array())
{
$loader = static::getPluginLoader();
if (!class_exists($classBaseName)) {
try {
$className = $loader->load($classBaseName);
} catch (Loader\Exception $e) {
throw new Exception("Validator class not found from basename '$classBaseName'", null, $e);
}
} else {
$className = $classBaseName;
}

$class = new \ReflectionClass($className);
if (!$class->implementsInterface('Zend\Validator\Validator')) {
throw new Exception("Validator class not found from basename '$classBaseName'");
}
$broker = static::getBroker();

if ((0 < count($args)) && $class->hasMethod('__construct')) {
$keys = array_keys($args);
$numeric = false;
foreach($keys as $key) {
if (is_numeric($key)) {
$numeric = true;
break;
}
}
$validator = $broker->load($classBaseName, $args);
$result = $validator->isValid($value);

if ($numeric) {
$object = $class->newInstanceArgs($args);
} else {
$object = $class->newInstance($args);
}
} else {
$object = $class->newInstance();
}
// Unregister validator in case different args are used on later invocation
$broker->unregister($classBaseName);

return $object->isValid($value);
return $result;
}
}
54 changes: 54 additions & 0 deletions library/Zend/Validator/ValidatorBroker.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php
/**
* Zend Framework
*
* LICENSE
*
* This source file is subject to the new BSD license that is bundled
* with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://framework.zend.com/license/new-bsd
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to [email protected] so we can send you a copy immediately.
*
* @category Zend
* @package Zend_Validator
* @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/

namespace Zend\Validator;

use Zend\Loader\PluginBroker;

/**
* Broker for validator instances
*
* @category Zend
* @package Zend_Validator
* @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
class ValidatorBroker extends PluginBroker
{
/**
* @var string Default plugin loading strategy
*/
protected $defaultClassLoader = 'Zend\Validator\ValidatorLoader';

/**
* Determine if we have a valid validator
*
* @param mixed $plugin
* @return true
* @throws Exception
*/
protected function validatePlugin($plugin)
{
if (!$plugin instanceof Validator) {
throw new Exception('Validators must implement Zend\Validator\Validator');
}
return true;
}
}
Loading

0 comments on commit 21201c6

Please sign in to comment.