Thanks to visit codestin.com
Credit goes to github.com

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

Commit 9e8f20f

Browse files
committed
Merge branch 'hotfix/2'
Close #2
2 parents 7e80065 + 919c9c4 commit 9e8f20f

File tree

3 files changed

+71
-11
lines changed

3 files changed

+71
-11
lines changed

CHANGELOG.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Changelog
2+
3+
All notable changes to this project will be documented in this file, in reverse chronological order by release.
4+
5+
## 2.5.2 - TBD
6+
7+
### Added
8+
9+
- [#2](https://github.com/zendframework/zend-inputfilter/pull/2) adds support
10+
in `Zend\InputFilter\Factory` for using the composed `InputFilterManager` to
11+
retrieve an input of a given `type` based on configuration; only if the type
12+
is not available in the factory will it attempt to directly instantiate it.
13+
14+
### Deprecated
15+
16+
- Nothing.
17+
18+
### Removed
19+
20+
- Nothing.
21+
22+
### Fixed
23+
24+
- Nothing.

src/Factory.php

Lines changed: 12 additions & 11 deletions
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

Lines changed: 35 additions & 0 deletions
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)