10000 Merge pull request #2 from halaxa/input_from_manager · zendframework/zend-inputfilter@3fa2943 · GitHub
[go: up one dir, main page]

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

Commit 3fa2943

Browse files
committed
Merge pull request #2 from halaxa/input_from_manager
'type' in Input config may be plugin name
2 parents 7e80065 + de361b0 commit 3fa2943

File tree

2 files changed

+47
-11
lines changed

2 files changed

+47
-11
lines changed

src/Factory.php

+12-11
Original file line numberDiff line numberDiff line change
@@ -171,19 +171,20 @@ public function createInput($inputSpecification)
171171

172172
if (isset($inputSpecification['type'])) {
173173
$class = $inputSpecification['type'];
174+
}
174175

175-
if ($this->getInputFilterManager()->has($class)) {
176-
return $this->createInputFilter($inputSpecification);
177-
}
178-
179-
if (!class_exists($class)) {
180-
throw new Exception\RuntimeException(sprintf(
181-
'Input factory expects the "type" to be a valid class; received "%s"',
182-
$class
183-
));
184-
}
176+
$managerInstance = null;
177+
if ($this->getInputFilterManager()->has($class)) {
178+
$managerInstance = $this->getInputFilterManager()->get($class);
185179
}
186-
$input = new $class();
180+
if (!$managerInstance && !class_exists($class)) {
181+
throw new Exception\RuntimeException(sprintf(
182+
'Input factory expects the "type" to be a valid class or a plugin name; received "%s"',
183+
$class
184+
));
185+
}
186+
187+
$input = $managerInstance ?: new $class();
187188

188189
if ($input instanceof InputFilterInterface) {
189190
return $this->createInputFilter($inputSpecification);

test/FactoryTest.php

+35
Original file line numberDiff line numberDiff line change
@@ -647,4 +647,39 @@ public function testCanCreateInputFilterFromProvider()
647647

648648
$this->assertInstanceOf('Zend\InputFilter\InputFilterInterface', $inputFilter);
649649
}
650+
651+
public function testSuggestedTypeMayBePluginNameInInputFilterPluginManager()
652+
{
653+
$factory = new Factory();
654+
$pluginManager = new InputFilterPluginManager();
655+
$pluginManager->setService('bar', new Input('bar'));
656+
$factory->setInputFilterManager($pluginManager);
657+
658+
$input = $factory->createInput(array(
659+
'type' => 'bar'
660+
));
661+
$this->assertSame('bar', $input->getName());
662+
663+
$this->setExpectedException('Zend\Filter\Exception\RuntimeException');
664+
$factory->createInput(array(
665+
'type' => 'foo'
666+
));
667+
}
668+
669+
public function testInputFromPluginManagerMayBeFurtherConfiguredWithSpec()
670+
{
671+
$factory = new Factory();
672+
$pluginManager = new InputFilterPluginManager();
673+
$pluginManager->setService('bar', $barInput = new Input('bar'));
674+
$this->assertTrue($barInput->isRequired());
675+
$factory->setInputFilterManager($pluginManager);
676+
677+
$input = $factory->createInput(array(
678+
'type' => 'bar',
679+
'required' => false
680+
));
681+
682+
$this->assertFalse($input->isRequired());
683+
$this->assertSame('bar', $input->getName());
684+
}
650685
}

0 commit comments

Comments
 (0)
0