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

Commit

Permalink
Merge branch 'hotfix/68'
Browse files Browse the repository at this point in the history
Close #68
  • Loading branch information
weierophinney committed Apr 7, 2016
2 parents 6dbe9d6 + 61749d0 commit c907c3a
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 6 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ All notable changes to this project will be documented in this file, in reverse

### Added

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

### Deprecated

Expand Down
24 changes: 20 additions & 4 deletions src/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -329,12 +329,28 @@ public function createInputFilter($inputFilterSpecification)
if (($value instanceof InputInterface)
|| ($value instanceof InputFilterInterface)
) {
$input = $value;
} else {
$input = $this->createInput($value);
$inputFilter->add($value, $key);
continue;
}

// Patch to enable nested, integer indexed input_filter_specs.
// Check type and name are in spec, and that composed type is
// an input filter...
if ((isset($value['type']) && is_string($value['type']))
&& (isset($value['name']) && is_string($value['name']))
&& $this->getInputFilterManager()->get($value['type']) instanceof InputFilter
) {
// If $key is an integer, reset it to the specified name.
if (is_integer($key)) {
$key = $value['name'];
}

// Remove name from specification. InputFilter doesn't have a
// name property!
unset($value['name']);
}

$inputFilter->add($input, $key);
$inputFilter->add($this->createInput($value), $key);
}

return $inputFilter;
Expand Down
69 changes: 69 additions & 0 deletions test/FactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -934,6 +934,75 @@ public function testInputFromPluginManagerMayBeFurtherConfiguredWithSpec()
$this->assertSame('bar', $input->getName());
}

public function testCreateInputFilterConfiguredNameWhenSpecIsIntegerIndexed()
{
$factory = $this->createDefaultFactory();
$inputFilter = $factory->createInputFilter([
1 => [
'type' => InputFilter::class,
'name' => 'foo',
],
]);

$this->assertTrue($inputFilter->has('foo'));
}

public function testCreateInputFilterUsesAssociatedNameMappingOverConfiguredName()
{
$factory = $this->createDefaultFactory();
$inputFilter = $factory->createInputFilter([
'foo' => [
'type' => InputFilter::class,
'name' => 'bar',
],
]);

$this->assertTrue($inputFilter->has('foo'));
$this->assertFalse($inputFilter->has('bar'));
}

public function testCreateInputFilterUsesConfiguredNameForNestedInputFilters()
{
$factory = $this->createDefaultFactory();
$inputFilter = $factory->createInputFilter([
0 => [
'type' => InputFilter::class,
'name' => 'bar',
'0' => [
'name' => 'bat',
],
'1' => [
'name' => 'baz',
],
],
1 => [
'type' => CollectionInputFilter::class,
'name' => 'foo',
'input_filter' => [
'0' => [
'name' => 'bat',
],
],
],
]);

$this->assertInstanceOf(InputFilter::class, $inputFilter);
$this->assertEquals(2, count($inputFilter));

$nestedInputFilter = $inputFilter->get('bar');
$this->assertInstanceOf(InputFilter::class, $nestedInputFilter);
$this->assertEquals(2, count($nestedInputFilter));
$this->assertTrue($nestedInputFilter->has('bat'));
$this->assertTrue($nestedInputFilter->has('baz'));

$collection = $inputFilter->get('foo');
$this->assertInstanceOf(CollectionInputFilter::class, $collection);
$collectionInputFilter = $collection->getInputFilter();
$this->assertInstanceOf(InputFilter::class, $collectionInputFilter);
$this->assertEquals(1, count($collectionInputFilter));
$this->assertTrue($collectionInputFilter->has('bat'));
}

/**
* @return Factory
*/
Expand Down
3 changes: 2 additions & 1 deletion test/InputFilterAbstractServiceFactoryTest.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/**
* Zend Framework (http://framework.zend.com/)
*
Expand Down Expand Up @@ -26,7 +27,7 @@ class InputFilterAbstractServiceFactoryTest extends TestCase
{
/**
* @var ServiceManager
*/
*/
protected $services;

/**
Expand Down

0 comments on commit c907c3a

Please sign in to comment.