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

Commit ef6c009

Browse files
committed
Merge pull request #68 from Blinden/patch_inputFilter_naming
Patch for integer indexed nested InputFilters defined in input_filter_specs
2 parents 6dbe9d6 + 9382925 commit ef6c009

File tree

3 files changed

+88
-1
lines changed

3 files changed

+88
-1
lines changed

src/Factory.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,23 @@ public function createInputFilter($inputFilterSpecification)
331331
) {
332332
$input = $value;
333333
} else {
334+
335+
// Patch to enable nested, integer indexed input_filter_specs
336+
// Check type and name are in spec
337+
if (isset($value['type']) && is_string($value['type'])
338+
&& isset($value['name']) && is_string($value['name'])) {
339+
// Make sure type is an InputFilter
340+
$checkType = $this->getInputFilterManager()->get($value['type']);
341+
if ($checkType instanceof InputFilter) {
342+
// Only apply when key is an integer
343+
if (is_integer($key)) {
344+
$key = $value['name'];
345+
}
346+
// Remove name from spec. InputFilter doesn't have an name property!
347+
unset($value['name']);
348+
}
349+
}
350+
334351
$input = $this->createInput($value);
335352
}
336353

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
*
@@ -26,7 +27,7 @@ class InputFilterAbstractServiceFactoryTest extends TestCase
2627
{
2728
/**
2829
* @var ServiceManager
29-
*/
30+
*/
3031
protected $services;
3132

3233
/**

0 commit comments

Comments
 (0)