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

Commit 239b1d0

Browse files
committed
Merge branch 'hotfix/68' into develop
Forward port #68
2 parents 3b9916f + 61749d0 commit 239b1d0

File tree

4 files changed

+94
-6
lines changed

4 files changed

+94
-6
lines changed

CHANGELOG.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,9 @@ All notable changes to this project will be documented in this file, in reverse
2727

2828
### Added
2929

30-
- Nothing.
30+
- [#68](https://github.com/zendframework/zend-inputfilter/pull/68) adds support
31+
for using *either* named keys *or* a `name` element in input filter specs
32+
parsed by the `InputFilterAbstractServiceFactory`.
3133

3234
### Deprecated
3335

src/Factory.php

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -329,12 +329,28 @@ public function createInputFilter($inputFilterSpecification)
329329
if (($value instanceof InputInterface)
330330
|| ($value instanceof InputFilterInterface)
331331
) {
332-
$input = $value;
333-
} else {
334-
$input = $this->createInput($value);
332+
$inputFilter->add($value, $key);
333+
continue;
334+
}
335+
336+
// Patch to enable nested, integer indexed input_filter_specs.
337+
// Check type and name are in spec, and that composed type is
338+
// an input filter...
339+
if ((isset($value['type']) && is_string($value['type']))
340+
&& (isset($value['name']) && is_string($value['name']))
341+
&& $this->getInputFilterManager()->get($value['type']) instanceof InputFilter
342+
) {
343+
// If $key is an integer, reset it to the specified name.
344+
if (is_integer($key)) {
345+
$key = $value['name'];
346+
}
347+
348+
// Remove name from specification. InputFilter doesn't have a
349+
// name property!
350+
unset($value['name']);
335351
}
336352

337-
$inputFilter->add($input, $key);
353+
$inputFilter->add($this->createInput($value), $key);
338354
}
339355

340356
return $inputFilter;

test/FactoryTest.php

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -934,6 +934,75 @@ public function testInputFromPluginManagerMayBeFurtherConfiguredWithSpec()
934934
$this->assertSame('bar', $input->getName());
935935
}
936936

937+
public function testCreateInputFilterConfiguredNameWhenSpecIsIntegerIndexed()
938+
{
939+
$factory = $this->createDefaultFactory();
940+
$inputFilter = $factory->createInputFilter([
941+
1 => [
942+
'type' => InputFilter::class,
943+
'name' => 'foo',
944+
],
945+
]);
946+
947+
$this->assertTrue($inputFilter->has('foo'));
948+
}
949+
950+
public function testCreateInputFilterUsesAssociatedNameMappingOverConfiguredName()
951+
{
952+
$factory = $this->createDefaultFactory();
953+
$inputFilter = $factory->createInputFilter([
954+
'foo' => [
955+
'type' => InputFilter::class,
956+
'name' => 'bar',
957+
],
958+
]);
959+
960+
$this->assertTrue($inputFilter->has('foo'));
961+
$this->assertFalse($inputFilter->has('bar'));
962+
}
963+
964+
public function testCreateInputFilterUsesConfiguredNameForNestedInputFilters()
965+
{
966+
$factory = $this->createDefaultFactory();
967+
$inputFilter = $factory->createInputFilter([
968+
0 => [
969+
'type' => InputFilter::class,
970+
'name' => 'bar',
971+
'0' => [
972+
'name' => 'bat',
973+
],
974+
'1' => [
975+
'name' => 'baz',
976+
],
977+
],
978+
1 => [
979+
'type' => CollectionInputFilter::class,
980+
'name' => 'foo',
981+
'input_filter' => [
982+
'0' => [
983+
'name' => 'bat',
984+
],
985+
],
986+
],
987+
]);
988+
989+
$this->assertInstanceOf(InputFilter::class, $inputFilter);
990+
$this->assertEquals(2, count($inputFilter));
991+
992+
$nestedInputFilter = $inputFilter->get('bar');
993+
$this->assertInstanceOf(InputFilter::class, $nestedInputFilter);
994+
$this->assertEquals(2, count($nestedInputFilter));
995+
$this->assertTrue($nestedInputFilter->has('bat'));
996+
$this->assertTrue($nestedInputFilter->has('baz'));
997+
998+
$collection = $inputFilter->get('foo');
999+
$this->assertInstanceOf(CollectionInputFilter::class, $collection);
1000+
$collectionInputFilter = $collection->getInputFilter();
1001+
$this->assertInstanceOf(InputFilter::class, $collectionInputFilter);
1002+
$this->assertEquals(1, count($collectionInputFilter));
1003+
$this->assertTrue($collectionInputFilter->has('bat'));
1004+
}
1005+
9371006
/**
9381007
* @return Factory
9391008
*/

test/InputFilterAbstractServiceFactoryTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
2+
23
/**
34
* Zend Framework (http://framework.zend.com/)
45
*
@@ -27,7 +28,7 @@ class InputFilterAbstractServiceFactoryTest extends TestCase
2728
{
2829
/**
2930
* @var ServiceManager
30-
*/
31+
*/
3132
protected $services;
3233

3334
/**

0 commit comments

Comments
 (0)