Skip to content
This repository was archived by the owner on Jan 8, 2020. It is now read-only.
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
117 changes: 117 additions & 0 deletions library/Zend/InputFilter/InputFilterAbstractServiceFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

change with :

/**
 * Zend Framework (http://framework.zend.com/)
 *
 * @link      http://github.com/zendframework/zf2 for the canonical source repository
 * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
 * @license   http://framework.zend.com/license/new-bsd New BSD License
 */

like other files.


namespace Zend\InputFilter;

use Zend\Filter\FilterPluginManager;
use Zend\ServiceManager\AbstractFactoryInterface;
use Zend\ServiceManager\ServiceLocatorInterface;
use Zend\Validator\ValidatorPluginManager;

class InputFilterAbstractServiceFactory implements AbstractFactoryInterface
{
/**
* @var Factory
*/
protected $factory;

/**
* @param ServiceLocatorInterface $inputFilters
* @param string $cName
* @param string $rName
*
* @return bool
*/
public function canCreateServiceWithName(ServiceLocatorInterface $inputFilters, $cName, $rName)
{
$services = $inputFilters->getServiceLocator();
if (! $services instanceof ServiceLocatorInterface
|| ! $services->has('Config')
) {
return false;
}

$config = $services->get('Config');
if (!isset($config['input_filter_specs'][$rName])
|| !is_array($config['input_filter_specs'][$rName])
) {
return false;
}

return true;
}

/**
* @param ServiceLocatorInterface $inputFilters
* @param string $cName
* @param string $rName
*
* @return \Zend\InputFilter\InputFilterInterface
*/
public function createServiceWithName(ServiceLocatorInterface $inputFilters, $cName, $rName)
{
$services = $inputFilters->getServiceLocator();
$allConfig = $services->get('Config');
$config = $allConfig['input_filter_specs'][$rName];

$factory = $this->getInputFilterFactory($services);

return $factory->createInputFilter($config);
}

/**
* @param ServiceLocatorInterface $services
*
* @return Factory
*/
protected function getInputFilterFactory(ServiceLocatorInterface $services)
{
if ($this->factory instanceof Factory) {
return $this->factory;
}

$this->factory = new Factory();
$this->factory
->getDefaultFilterChain()
->setPluginManager($this->getFilterPluginManager($services));
$this->factory
->getDefaultValidatorChain()
->setPluginManager($this->getValidatorPluginManager($services));

return $this->factory;
}

/**
* @param ServiceLocatorInterface $services
*
* @return \Zend\ServiceManager\AbstractPluginManager
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's returning a FilterPluginManager. I can change this at merge.

*/
protected function getFilterPluginManager(ServiceLocatorInterface $services)
{
if ($services->has('FilterManager')) {
return $services->get('FilterManager');
}

return new FilterPluginManager();
}

/**
* @param ServiceLocatorInterface $services
*
* @return \Zend\ServiceManager\AbstractPluginManager
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above comment, only ValidatorPluginManager. I'll also update at merge.

*/
protected function getValidatorPluginManager(ServiceLocatorInterface $services)
{
if ($services->has('ValidatorManager')) {
return $services->get('ValidatorManager');
}

return new ValidatorPluginManager();
}
}
156 changes: 156 additions & 0 deletions tests/ZendTest/InputFilter/InputFilterAbstractServiceFactoryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

change it too with :

/**
 * Zend Framework (http://framework.zend.com/)
 *
 * @link      http://github.com/zendframework/zf2 for the canonical source repository
 * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
 * @license   http://framework.zend.com/license/new-bsd New BSD License
 */

namespace ZendTest\InputFilter;

use PHPUnit_Framework_TestCase as TestCase;
use Zend\Filter\FilterPluginManager;
use Zend\InputFilter\InputFilterPluginManager;
use Zend\ServiceManager\ServiceManager;
use Zend\Validator\ValidatorPluginManager;
use Zend\InputFilter\InputFilterAbstractServiceFactory;

class InputFilterAbstractServiceFactoryTest extends TestCase
{
public function setUp()
{
$this->services = new ServiceManager();
$this->filters = new InputFilterPluginManager();
$this->filters->setServiceLocator($this->services);
$this->services->setService('InputFilterManager', $this->filters);

$this->factory = new InputFilterAbstractServiceFactory();
}

public function testCannotCreateServiceIfNoConfigServicePresent()
{
$this->assertFalse($this->factory->canCreateServiceWithName($this->filters, 'filter', 'filter'));
}

public function testCannotCreateServiceIfConfigServiceDoesNotHaveInputFiltersConfiguration()
{
$this->services->setService('Config', array());
$this->assertFalse($this->factory->canCreateServiceWithName($this->filters, 'filter', 'filter'));
}

public function testCannotCreateServiceIfConfigInputFiltersDoesNotContainMatchingServiceName()
{
$this->services->setService('Config', array(
'input_filter_specs' => array(),
));
$this->assertFalse($this->factory->canCreateServiceWithName($this->filters, 'filter', 'filter'));
}

public function testCanCreateServiceIfConfigInputFiltersContainsMatchingServiceName()
{
$this->services->setService('Config', array(
'input_filter_specs' => array(
'filter' => array(),
),
));
$this->assertTrue($this->factory->canCreateServiceWithName($this->filters, 'filter', 'filter'));
}

public function testCreatesInputFilterInstance()
{
$this->services->setService('Config', array(
'input_filter_specs' => array(
'filter' => array(),
),
));
$filter = $this->factory->createServiceWithName($this->filters, 'filter', 'filter');
$this->assertInstanceOf('Zend\InputFilter\InputFilterInterface', $filter);
}

/**
* @depends testCreatesInputFilterInstance
*/
public function testUsesConfiguredValidationAndFilterManagerServicesWhenCreatingInputFilter()
{
$filters = new FilterPluginManager();
$filter = function ($value) {
};
$filters->setService('foo', $filter);

$validators = new ValidatorPluginManager();
$validator = $this->getMock('Zend\Validator\ValidatorInterface');
$validators->setService('foo', $validator);

$this->services->setService('FilterManager', $filters);
$this->services->setService('ValidatorManager', $validators);
$this->services->setService('Config', array(
'input_filter_specs' => array(
'filter' => array(
'input' => array(
'name' => 'input',
'required' => true,
'filters' => array(
array( 'name' => 'foo' ),
),
'validators' => array(
array( 'name' => 'foo' ),
),
),
),
),
));

$inputFilter = $this->factory->createServiceWithName($this->filters, 'filter', 'filter');
$this->assertTrue($inputFilter->has('input'));

$input = $inputFilter->get('input');

$filterChain = $input->getFilterChain();
$this->assertSame($filters, $filterChain->getPluginManager());
$this->assertEquals(1, count($filterChain));
$this->assertSame($filter, $filterChain->plugin('foo'));
$this->assertEquals(1, count($filterChain));

$validatorChain = $input->getvalidatorChain();
$this->assertSame($validators, $validatorChain->getPluginManager());
$this->assertEquals(1, count($validatorChain));
$this->assertSame($validator, $validatorChain->plugin('foo'));
$this->assertEquals(1, count($validatorChain));
}

public function testRetrieveInputFilterFromInputFilterPluginManager()
{
$filters = new FilterPluginManager();
$filter = function ($value) {
};
$filters->setService('foo', $filter);

$validators = new ValidatorPluginManager();
$validator = $this->getMock('Zend\Validator\ValidatorInterface');
$validators->setService('foo', $validator);

$this->services->setService('FilterManager', $filters);
$this->services->setService('ValidatorManager', $validators);
$this->services->setService('Config', array(
'input_filter_specs' => array(
'foobar' => array(
'input' => array(
'name' => 'input',
'required' => true,
'filters' => array(
array( 'name' => 'foo' ),
),
'validators' => array(
array( 'name' => 'foo' ),
),
),
),
),
));
$this->services->get('InputFilterManager')->addAbstractFactory('Zend\InputFilter\InputFilterAbstractServiceFactory');

$inputFilter = $this->services->get('InputFilterManager')->get('foobar');
$this->assertInstanceOf('Zend\InputFilter\InputFilterInterface', $inputFilter);
}
}