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

Commit c793ff6

Browse files
committed
BaseInputFilter::add() does not allow all input types defined in the interface contract
InputFlterInterface::add() specify array (input specification format) as an allowed type for $input. BaseInputFilter::add() throws exception for this case. This commit promote the logic present in InputFilter (now deprecated) to BaseInputFilter so this one become compatible with Interface contract. Since InputFilter allow too Traversable object and Factory (underlying logic) allows InputProviderInterface objects then I decided promote all this types to Interface. With this change add too many allowed types is the only one way for to do this change preserving the backward compatibility.
1 parent b75c2a7 commit c793ff6

7 files changed

+146
-179
lines changed

src/BaseInputFilter.php

Lines changed: 44 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,11 @@ class BaseInputFilter implements
2525
*/
2626
protected $data;
2727

28+
/**
29+
* @var Factory
30+
*/
31+
protected $factory;
32+
2833
/**
2934
* @var InputInterface[]|InputFilterInterface[]
3035
*/
@@ -45,6 +50,33 @@ class BaseInputFilter implements
4550
*/
4651
protected $validInputs;
4752

53+
/**
54+
* Set factory to use when adding inputs and filters by spec
55+
*
56+
* @param Factory $factory
57+
* @return InputFilter
58+
*/
59+
public function setFactory(Factory $factory)
60+
{
61+
$this->factory = $factory;
62+
return $this;
63+
}
64+
65+
/**
66+
* Get factory to use when adding inputs and filters by spec
67+
*
68+
* Lazy-loads a Factory instance if none attached.
69+
*
70+
* @return Factory
71+
*/
72+
public function getFactory()
73+
{
74+
if (null === $this->factory) {
75+
$this->setFactory(new Factory());
76+
}
77+
return $this->factory;
78+
}
79+
4880
/**
4981
* This function is automatically called when creating element with factory. It
5082
* allows to perform various operations (add elements...)
@@ -70,19 +102,27 @@ public function count()
70102
/**
71103
* Add an input to the input filter
72104
*
73-
* @param InputInterface|InputFilterInterface $input
105+
* @param InputInterface|InputFilterInterface|array|Traversable|InputProviderInterface $input
74106
* @param null|string $name Name used to retrieve this input
75107
* @throws Exception\InvalidArgumentException
76108
* @return InputFilterInterface
77109
*/
78110
public function add($input, $name = null)
79111
{
112+
if (is_array($input) || $input instanceof Traversable || $input instanceof InputProviderInterface) {
113+
$factory = $this->getFactory();
114+
$input = $factory->createInput($input);
115+
}
116+
80117
if (!$input instanceof InputInterface && !$input instanceof InputFilterInterface) {
81118
throw new Exception\InvalidArgumentException(sprintf(
82-
'%s expects an instance of %s or %s as its first argument; received "%s"',
119+
'%s expects an instance of %s, %s, %s, %s or %s as its first argument; received "%s"',
83120
__METHOD__,
84-
'Zend\InputFilter\InputInterface',
85-
'Zend\InputFilter\InputFilterInterface',
121+
InputInterface::class,
122+
InputFilterInterface::class,
123+
'array',
124+
Traversable::class,
125+
InputProviderInterface::class,
86126
(is_object($input) ? get_class($input) : gettype($input))
87127
));
88128
}

src/CollectionInputFilter.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
use Traversable;
1313

14-
class CollectionInputFilter extends InputFilter
14+
class CollectionInputFilter extends BaseInputFilter
1515
{
1616
/*
1717
* @var bool

src/InputFilter.php

Lines changed: 3 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -9,57 +9,9 @@
99

1010
namespace Zend\InputFilter;
1111

12-
use Traversable;
13-
12+
/**
13+
* @deprecated 2.5.5 Logic of this class has been promoted to his parent. Use Zend\InputFilter\BaseInputFilter instead.
14+
*/
1415
class InputFilter extends BaseInputFilter
1516
{
16-
/**
17-
* @var Factory
18-
*/
19-
protected $factory;
20-
21-
/**
22-
* Set factory to use when adding inputs and filters by spec
23-
*
24-
* @param Factory $factory
25-
* @return InputFilter
26-
*/
27-
public function setFactory(Factory $factory)
28-
{
29-
$this->factory = $factory;
30-
return $this;
31-
}
32-
33-
/**
34-
* Get factory to use when adding inputs and filters by spec
35-
*
36-
* Lazy-loads a Factory instance if none attached.
37-
*
38-
* @return Factory
39-
*/
40-
public function getFactory()
41-
{
42-
if (null === $this->factory) {
43-
$this->setFactory(new Factory());
44-
}
45-
return $this->factory;
46-
}
47-
48-
/**
49-
* Add an input to the input filter
50-
*
51-
* @param array|Traversable|InputInterface|InputFilterInterface $input
52-
* @param null|string $name
53-
* @return InputFilter
54-
*/
55-
public function add($input, $name = null)
56-
{
57-
if (is_array($input)
58-
|| ($input instanceof Traversable && !$input instanceof InputFilterInterface)
59-
) {
60-
$factory = $this->getFactory();
61-
$input = $factory->createInput($input);
62-
}
63-
return parent::add($input, $name);
64-
}
6517
}

src/InputFilterInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ interface InputFilterInterface extends Countable
1919
/**
2020
* Add an input to the input filter
2121
*
22-
* @param InputInterface|InputFilterInterface|array $input
22+
* @param InputInterface|InputFilterInterface|array|Traversable|InputProviderInterface $input
2323
* @param null|string $name Name used to retrieve this input
2424
* @return InputFilterInterface
2525
*/

test/BaseInputFilterTest.php

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,13 @@
1010
namespace ZendTest\InputFilter;
1111

1212
use ArrayObject;
13+
use PHPUnit_Framework_MockObject_MockObject as MockObject;
1314
use PHPUnit_Framework_TestCase as TestCase;
1415
use stdClass;
16+
use Zend\InputFilter\CollectionInputFilter;
17+
use Zend\InputFilter\Factory;
1518
use Zend\InputFilter\Input;
19+
use Zend\InputFilter\InputInterface;
1620
use Zend\InputFilter\FileInput;
1721
use Zend\InputFilter\BaseInputFilter as InputFilter;
1822
use Zend\Filter;
@@ -1114,4 +1118,94 @@ public function testAllowsValidatingArrayAccessData()
11141118
$filter->setData($data);
11151119
$this->assertTrue($filter->isValid());
11161120
}
1121+
1122+
public function testLazilyComposesAFactoryByDefault()
1123+
{
1124+
$filter = new InputFilter();
1125+
1126+
$factory = $filter->getFactory();
1127+
$this->assertInstanceOf('Zend\InputFilter\Factory', $factory);
1128+
}
1129+
1130+
public function testCanComposeAFactory()
1131+
{
1132+
$filter = new InputFilter();
1133+
1134+
/** @var Factory|MockObject $factory */
1135+
$factory = $this->getMock(Factory::class);
1136+
$filter->setFactory($factory);
1137+
$this->assertSame($factory, $filter->getFactory());
1138+
}
1139+
1140+
/**
1141+
* @covers \Zend\InputFilter\BaseInputFilter::getValue
1142+
*
1143+
* @group 6028
1144+
*/
1145+
public function testGetValueReturnsArrayIfNestedInputFilters()
1146+
{
1147+
$filter = new InputFilter();
1148+
1149+
$inputFilter = new InputFilter();
1150+
$inputFilter->add(new Input(), 'name');
1151+
1152+
$filter->add($inputFilter, 'people');
1153+
1154+
$data = [
1155+
'people' => [
1156+
'name' => 'Wanderson'
1157+
]
1158+
];
1159+
1160+
$filter->setData($data);
1161+
$this->assertTrue($filter->isValid());
1162+
1163+
$this->assertInternalType('array', $filter->getValue('people'));
1164+
}
1165+
1166+
/**
1167+
* @group ZF2-5648
1168+
*/
1169+
public function testCountZeroValidateInternalInputWithCollectionInputFilter()
1170+
{
1171+
$filter = new InputFilter();
1172+
1173+
$inputFilter = new InputFilter();
1174+
$inputFilter->add(new Input(), 'name');
1175+
1176+
$collection = new CollectionInputFilter();
1177+
$collection->setInputFilter($inputFilter);
1178+
$collection->setCount(0);
1179+
1180+
$filter->add($collection, 'people');
1181+
1182+
$data = [
1183+
'people' => [
1184+
[
1185+
'name' => 'Wanderson',
1186+
],
1187+
],
1188+
];
1189+
$filter->setData($data);
1190+
1191+
$this->assertTrue($filter->isvalid());
1192+
$this->assertSame($data, $filter->getValues());
1193+
}
1194+
1195+
public function testCanUseContextPassedToInputFilter()
1196+
{
1197+
$filter = new InputFilter();
1198+
1199+
$context = new \stdClass();
1200+
1201+
/** @var InputInterface|MockObject $input */
1202+
$input = $this->getMock(InputInterface::class);
1203+
$input->expects($this->once())->method('isValid')->with($context)->will($this->returnValue(true));
1204+
$input->method('getRawValue')->will($this->returnValue('Mwop'));
1205+
1206+
$filter->add($input, 'username');
1207+
$filter->setData(['username' => 'Mwop']);
1208+
1209+
$filter->isValid($context);
1210+
}
11171211
}

test/CollectionInputFilterTest.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
use Zend\InputFilter\BaseInputFilter;
1414
use Zend\InputFilter\CollectionInputFilter;
1515
use Zend\InputFilter\Input;
16-
use Zend\InputFilter\InputFilter;
1716
use Zend\Validator;
1817

1918
class CollectionInputFilterTest extends TestCase
@@ -726,13 +725,13 @@ public function dataNestingCollection()
726725
*/
727726
public function testNestingCollectionCountCached($count, $expectedIsValid)
728727
{
729-
$firstInputFilter = new InputFilter();
728+
$firstInputFilter = new BaseInputFilter();
730729

731730
$firstCollection = new CollectionInputFilter();
732731
$firstCollection->setInputFilter($firstInputFilter);
733732

734733
$someInput = new Input('input');
735-
$secondInputFilter = new InputFilter();
734+
$secondInputFilter = new BaseInputFilter();
736735
$secondInputFilter->add($someInput, 'input');
737736

738737
$secondCollection = new CollectionInputFilter();
@@ -743,7 +742,7 @@ public function testNestingCollectionCountCached($count, $expectedIsValid)
743742

744743
$firstInputFilter->add($secondCollection, 'second_collection');
745744

746-
$mainInputFilter = new InputFilter();
745+
$mainInputFilter = new BaseInputFilter();
747746
$mainInputFilter->add($firstCollection, 'first_collection');
748747

749748
$data = [

0 commit comments

Comments
 (0)