Skip to content
This repository has been archived by the owner on Jan 30, 2020. It is now read-only.

Commit

Permalink
Show file tree
Hide file tree
Showing 6 changed files with 142 additions and 5 deletions.
3 changes: 3 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
"zendframework/zend-validator": "self.version",
"zendframework/zend-stdlib": "self.version"
},
"suggest": {
"zendframework/zend-servicemanager": "To support plugin manager support"
},
"extra": {
"branch-alias": {
"dev-master": "2.1-dev",
Expand Down
14 changes: 13 additions & 1 deletion src/BaseInputFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,30 @@
use ArrayAccess;
use Traversable;
use Zend\Stdlib\ArrayUtils;
use Zend\Stdlib\InitializableInterface;

/**
* @todo How should we deal with required input when data is missing?
* should a message be returned? if so, what message?
*/
class BaseInputFilter implements InputFilterInterface, UnknownInputsCapableInterface
class BaseInputFilter implements InputFilterInterface, UnknownInputsCapableInterface, InitializableInterface
{
protected $data;
protected $inputs = array();
protected $invalidInputs;
protected $validationGroup;
protected $validInputs;

/**
* This function is automatically called when creating element with factory. It
* allows to perform various operations (add elements...)
*
* @return void
*/
public function init()
{
}

/**
* Countable: number of inputs in this input filter
*
Expand Down Expand Up @@ -469,6 +480,7 @@ public function getMessages()
foreach ($this->getInvalidInput() as $name => $input) {
$messages[$name] = $input->getMessages();
}

return $messages;
}

Expand Down
6 changes: 6 additions & 0 deletions src/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ class Factory
protected $defaultFilterChain;
protected $defaultValidatorChain;

public function __construct()
{
$this->defaultFilterChain = new FilterChain();
$this->defaultValidatorChain = new ValidatorChain();
}

/**
* Set default filter chain to use
*
Expand Down
74 changes: 74 additions & 0 deletions src/InputFilterPluginManager.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/

namespace Zend\InputFilter;

use Zend\InputFilter\Exception;
use Zend\ServiceManager\AbstractPluginManager;
use Zend\ServiceManager\ConfigInterface;
use Zend\ServiceManager\ServiceLocatorInterface;
use Zend\Stdlib\InitializableInterface;

/**
* Plugin manager implementation for input filters.
*/
class InputFilterPluginManager extends AbstractPluginManager
{
/**
* Whether or not to share by default
*
* @var bool
*/
protected $shareByDefault = false;

/**
* @param ConfigInterface $configuration
*/
public function __construct(ConfigInterface $configuration = null)
{
parent::__construct($configuration);

$this->addInitializer(array($this, 'populateFactory'));
}

/**
* Populate the factory with filter chain and validator chain
*
* @param $element
*/
public function populateFactory($element)
{
if ($element instanceof InputFilter && $this->serviceLocator instanceof ServiceLocatorInterface) {
$factory = $element->getFactory();
$factory->getDefaultFilterChain()->setPluginManager($this->serviceLocator->get('FilterManager'));
$factory->getDefaultValidatorChain()->setPluginManager($this->serviceLocator->get('ValidatorManager'));
}
}

/**
* {@inheritDoc}
*/
public function validatePlugin($plugin)
{
if ($plugin instanceof InputFilterInterface) {
// Hook to perform various initialization, when the element is not created through the factory
if ($plugin instanceof InitializableInterface) {
$plugin->init();
}

// we're okay
return;
}

throw new Exception\RuntimeException(sprintf(
'Plugin of type %s is invalid; must implement Zend\InputFilter\InputFilterInterface',
(is_object($plugin) ? get_class($plugin) : gettype($plugin))
));
}
}
8 changes: 4 additions & 4 deletions test/FactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,16 @@

class FactoryTest extends TestCase
{
public function testFactoryDoesNotComposeFilterChainByDefault()
public function testFactoryComposesFilterChainByDefault()
{
$factory = new Factory();
$this->assertNull($factory->getDefaultFilterChain());
$this->assertInstanceOf('Zend\Filter\FilterChain', $factory->getDefaultFilterChain());
}

public function testFactoryDoesNotComposeValidatorChainByDefault()
public function testFactoryComposesValidatorChainByDefault()
{
$factory = new Factory();
$this->assertNull($factory->getDefaultValidatorChain());
$this->assertInstanceOf('Zend\Validator\ValidatorChain', $factory->getDefaultValidatorChain());
}

public function testFactoryAllowsInjectingFilterChain()
Expand Down
42 changes: 42 additions & 0 deletions test/InputFilterManagerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/

namespace ZendTest\InputFilter;

use Zend\ServiceManager\ServiceManager;
use Zend\InputFilter\InputFilterPluginManager;

/**
* @group Zend_Stdlib
*/
class InputFilterManagerTest extends \PHPUnit_Framework_TestCase
{
/**
* @var InputFilterPluginManager
*/
protected $manager;

public function setUp()
{
$this->manager = new InputFilterPluginManager();
}

public function testRegisteringInvalidElementRaisesException()
{
$this->setExpectedException('Zend\InputFilter\Exception\RuntimeException');
$this->manager->setService('test', $this);
}

public function testLoadingInvalidElementRaisesException()
{
$this->manager->setInvokableClass('test', get_class($this));
$this->setExpectedException('Zend\InputFilter\Exception\RuntimeException');
$this->manager->get('test');
}
}

0 comments on commit 4eaa835

Please sign in to comment.