diff --git a/src/Factory.php b/src/Factory.php index b1f74b0a..65400ade 100644 --- a/src/Factory.php +++ b/src/Factory.php @@ -14,6 +14,7 @@ use Zend\Stdlib\ArrayUtils; use Zend\Validator\ValidatorInterface; use Zend\Validator\ValidatorChain; +use Zend\ServiceManager\ServiceLocatorInterface; class Factory { @@ -116,7 +117,15 @@ public function clearDefaultValidatorChain() public function setInputFilterManager(InputFilterPluginManager $inputFilterManager) { $this->inputFilterManager = $inputFilterManager; - + $serviceLocator = $this->inputFilterManager->getServiceLocator(); + if ($serviceLocator && $serviceLocator instanceof ServiceLocatorInterface) { + if ($serviceLocator->has('ValidatorManager')) { + $this->getDefaultValidatorChain()->setPluginManager($serviceLocator->get('ValidatorManager')); + } + if ($serviceLocator->has('FilterManager')) { + $this->getDefaultFilterChain()->setPluginManager($serviceLocator->get('FilterManager')); + } + } return $this; } diff --git a/test/FactoryTest.php b/test/FactoryTest.php index d5c78f4e..b0e7534a 100644 --- a/test/FactoryTest.php +++ b/test/FactoryTest.php @@ -16,7 +16,8 @@ use Zend\InputFilter\Input; use Zend\InputFilter\InputFilter; use Zend\Validator; -use ZendTest\InputFilter\TestAsset\CustomFactory; +use Zend\InputFilter\InputFilterPluginManager; +use Zend\ServiceManager; class FactoryTest extends TestCase { @@ -519,7 +520,7 @@ public function testConflictNameWithInputFilterType() public function testCustomFactoryInCollection() { - $factory = new CustomFactory(); + $factory = new TestAsset\CustomFactory(); $inputFilter = $factory->createInputFilter(array( 'type' => 'collection', 'input_filter' => new InputFilter(), @@ -540,4 +541,31 @@ public function testCanSetInputErrorMessage() )); $this->assertEquals('Custom error message', $input->getErrorMessage()); } + + public function testSetInputFilterManagerWithServiceManager() + { + $inputFilterManager = new InputFilterPluginManager; + $serviceManager = new ServiceManager\ServiceManager; + $serviceManager->setService('ValidatorManager', new Validator\ValidatorPluginManager); + $serviceManager->setService('FilterManager', new Filter\FilterPluginManager); + $inputFilterManager->setServiceLocator($serviceManager); + $factory = new Factory(); + $factory->setInputFilterManager($inputFilterManager); + $this->assertInstanceOf( + 'Zend\Validator\ValidatorPluginManager', + $factory->getDefaultValidatorChain()->getPluginManager() + ); + $this->assertInstanceOf( + 'Zend\Filter\FilterPluginManager', + $factory->getDefaultFilterChain()->getPluginManager() + ); + } + + public function testSetInputFilterManagerWithoutServiceManager() + { + $inputFilterManager = new InputFilterPluginManager(); + $factory = new Factory(); + $factory->setInputFilterManager($inputFilterManager); + $this->assertSame($inputFilterManager, $factory->getInputFilterManager()); + } }