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

Commit 4f52b71

Browse files
committed
Merging develop to master in preparation for 2.10.0 release.
2 parents 9715401 + d6b9c62 commit 4f52b71

10 files changed

+231
-11
lines changed

CHANGELOG.md

+13-2
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,22 @@
22

33
All notable changes to this project will be documented in this file, in reverse chronological order by release.
44

5-
## 2.9.2 - TBD
5+
## 2.10.0 - 2019-01-30
66

77
### Added
88

9-
- Nothing.
9+
- [#176](https://github.com/zendframework/zend-inputfilter/pull/176) adds the interface `UnfilteredDataInterface`, with the following methods:
10+
11+
```php
12+
public function getUnfilteredData() : array|object;
13+
public function setUnfilteredData(array|object $data) : $this;
14+
```
15+
16+
By default, the `BaseInputFilter` now implements this interface.
17+
18+
The primary purpose of the interface is to allow the ability to access ALL
19+
original raw data, and not just the data the input filter knows about. This is
20+
particularly useful with collections.
1021

1122
### Changed
1223

composer.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
},
1313
"extra": {
1414
"branch-alias": {
15-
"dev-master": "2.9.x-dev",
16-
"dev-develop": "2.10.x-dev"
15+
"dev-master": "2.10.x-dev",
16+
"dev-develop": "2.11.x-dev"
1717
},
1818
"zf": {
1919
"component": "Zend\\InputFilter",

composer.lock

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/book/unfiltered-data.md

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# Unfiltered Data
2+
3+
> Since 2.10.0
4+
5+
On input filters, there are several methods for retrieving the data:
6+
7+
- `getValues()` will return all known values after filtering them.
8+
- `getRawValues()` will return all known values with no filtering applied.
9+
- `getUnknown()` returns the set of all unknown values (values with no
10+
corresponding input or input filter).
11+
12+
At times, particularly when working with collections, you may want access to the
13+
complete set of original data provided to the input filter. This can be
14+
accomplished by merging the sets returned by `getRawValues()` and
15+
`getUnknown()` when working with normal input filters, but this approach breaks
16+
down when working with collections.
17+
18+
Version 2.10.0 introduces a new interface, `Zend\InputFilter\UnfilteredDataInterface`,
19+
for dealing with this situation. `Zend\InputFilter\BaseInputFilter`, which
20+
forms the parent class for all shipped input filter implementations, implements
21+
the interface, which consists of the following methods:
22+
23+
```php
24+
interface UnfilteredDataInterface
25+
{
26+
/**
27+
* @return array|object
28+
*/
29+
public function getUnfilteredData()
30+
{
31+
return $this->unfilteredData;
32+
}
33+
34+
/**
35+
* @param array|object $data
36+
* @return $this
37+
*/
38+
public function setUnfilteredData($data)
39+
{
40+
$this->unfilteredData = $data;
41+
return $this;
42+
}
43+
}
44+
```
45+
46+
The `setUnfilteredData()` method is called by `setData()` with the full `$data`
47+
provided to that method, ensuring that `getUnfilteredData()` will always provide
48+
the original data with which the input filter was initialized, with no filtering
49+
applied.

mkdocs.yml

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
docs_dir: docs/book
22
site_dir: docs/html
33
pages:
4-
- index.md
5-
- Intro: intro.md
4+
- Home: index.md
5+
- Introduction: intro.md
66
- Reference:
77
- Specifications: specs.md
88
- Files: file-input.md
99
- "Optional Input Filters": optional-input-filters.md
10+
- "Unfiltered Data": unfiltered-data.md
1011
site_name: zend-inputfilter
1112
site_description: zend-inputfilter
1213
repo_url: 'https://github.com/zendframework/zend-inputfilter'
13-
copyright: 'Copyright (c) 2016-2017 <a href="http://www.zend.com/">Zend Technologies USA Inc.</a>'
14+
copyright: 'Copyright (c) 2016-2019 <a href="http://www.zend.com/">Zend Technologies USA Inc.</a>'

src/BaseInputFilter.php

+30-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* Zend Framework (http://framework.zend.com/)
44
*
55
* @link http://github.com/zendframework/zf2 for the canonical source repository
6-
* @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
6+
* @copyright Copyright (c) 2005-2019 Zend Technologies USA Inc. (http://www.zend.com)
77
* @license http://framework.zend.com/license/new-bsd New BSD License
88
*/
99

@@ -18,13 +18,19 @@ class BaseInputFilter implements
1818
InputFilterInterface,
1919
UnknownInputsCapableInterface,
2020
InitializableInterface,
21-
ReplaceableInputInterface
21+
ReplaceableInputInterface,
22+
UnfilteredDataInterface
2223
{
2324
/**
2425
* @var null|array
2526
*/
2627
protected $data;
2728

29+
/**
30+
* @var array|object
31+
*/
32+
protected $unfilteredData = [];
33+
2834
/**
2935
* @var InputInterface[]|InputFilterInterface[]
3036
*/
@@ -194,8 +200,12 @@ public function setData($data)
194200
(is_object($data) ? get_class($data) : gettype($data))
195201
));
196202
}
203+
204+
$this->setUnfilteredData($data);
205+
197206
$this->data = $data;
198207
$this->populate();
208+
199209
return $this;
200210
}
201211

@@ -602,4 +612,22 @@ public function merge(BaseInputFilter $inputFilter)
602612

603613
return $this;
604614
}
615+
616+
/**
617+
* @return array|object
618+
*/
619+
public function getUnfilteredData()
620+
{
621+
return $this->unfilteredData;
622+
}
623+
624+
/**
625+
* @param array|object $data
626+
* @return $this
627+
*/
628+
public function setUnfilteredData($data)
629+
{
630+
$this->unfilteredData = $data;
631+
return $this;
632+
}
605633
}

src/CollectionInputFilter.php

+2
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,8 @@ public function setData($data)
153153
));
154154
}
155155

156+
$this->setUnfilteredData($data);
157+
156158
foreach ($data as $item) {
157159
if (is_array($item) || $item instanceof Traversable) {
158160
continue;

src/UnfilteredDataInterface.php

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
/**
3+
* @see https://github.com/zendframework/zend-inputfilter for the canonical source repository
4+
* @copyright Copyright (c) 2019 Zend Technologies USA Inc. (https://www.zend.com)
5+
* @license https://github.com/zendframework/zend-inputfilter/blob/master/LICENSE.md New BSD License
6+
*/
7+
8+
namespace Zend\InputFilter;
9+
10+
/**
11+
* Ensures Inputs store unfiltered data and are capable of returning it
12+
*/
13+
interface UnfilteredDataInterface
14+
{
15+
/**
16+
* @return array|object
17+
*/
18+
public function getUnfilteredData();
19+
20+
/**
21+
* @param array|object $data
22+
* @return $this
23+
*/
24+
public function setUnfilteredData($data);
25+
}

test/BaseInputFilterTest.php

+74-1
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,16 @@
1212
use ArrayIterator;
1313
use ArrayObject;
1414
use FilterIterator;
15-
use PHPUnit_Framework_MockObject_MockObject as MockObject;
1615
use PHPUnit\Framework\TestCase;
16+
use PHPUnit_Framework_MockObject_MockObject as MockObject;
1717
use stdClass;
1818
use Zend\InputFilter\BaseInputFilter;
1919
use Zend\InputFilter\Exception\InvalidArgumentException;
2020
use Zend\InputFilter\Exception\RuntimeException;
2121
use Zend\InputFilter\Input;
2222
use Zend\InputFilter\InputFilterInterface;
2323
use Zend\InputFilter\InputInterface;
24+
use Zend\InputFilter\UnfilteredDataInterface;
2425

2526
/**
2627
* @covers Zend\InputFilter\BaseInputFilter
@@ -603,6 +604,78 @@ public function testNestedInputFilterShouldAllowNonArrayValueForData()
603604
self::assertNull($filter1->getValues()['nested']['nestedField1']);
604605
}
605606

607+
public function testInstanceOfUnfilteredDataInterface()
608+
{
609+
$baseInputFilter = new BaseInputFilter();
610+
611+
self::assertInstanceOf(
612+
UnfilteredDataInterface::class,
613+
$baseInputFilter,
614+
sprintf('%s should implement %s', BaseInputFilter::class, UnfilteredDataInterface::class)
615+
);
616+
}
617+
618+
public function testGetUnfilteredDataReturnsArray()
619+
{
620+
$baseInputFilter = new BaseInputFilter();
621+
622+
self::assertInternalType('array', $baseInputFilter->getUnfilteredData());
623+
}
624+
625+
public function testSetUnfilteredDataReturnsBaseInputFilter()
626+
{
627+
$baseInputFilter = new BaseInputFilter();
628+
629+
self::assertInstanceOf(BaseInputFilter::class, $baseInputFilter->setUnfilteredData([]));
630+
}
631+
632+
public function testSettingAndReturningDataArrayUnfilteredDataInterface()
633+
{
634+
$testArray = [
635+
'foo' => 'bar',
636+
];
637+
638+
$baseInputFilter = new BaseInputFilter();
639+
$baseInputFilter->setUnfilteredData($testArray);
640+
641+
self::assertSame($testArray, $baseInputFilter->getUnfilteredData());
642+
}
643+
644+
public function testSettingAndReturnDataArrayUsingSetDataForUnfilteredDataInterface()
645+
{
646+
$testArray = [
647+
'foo' => 'bar',
648+
];
649+
650+
$baseInputFilter = new BaseInputFilter();
651+
$baseInputFilter->setData($testArray);
652+
653+
self::assertSame($testArray, $baseInputFilter->getUnfilteredData());
654+
}
655+
656+
public function testSetDataUsingSetDataAndApplyFiltersReturningSameAsOriginalForUnfilteredData()
657+
{
658+
$filteredArray = [
659+
'bar' => 'foo',
660+
];
661+
662+
$unfilteredArray = array_merge(
663+
$filteredArray,
664+
[
665+
'foo' => 'bar',
666+
]
667+
);
668+
669+
/** @var BaseInputFilter $baseInputFilter */
670+
$baseInputFilter = (new BaseInputFilter())
671+
->add(new Input(), 'bar')
672+
->setData($unfilteredArray);
673+
674+
self::assertSame($unfilteredArray, $baseInputFilter->getUnfilteredData());
675+
self::assertSame($filteredArray, $baseInputFilter->getValues());
676+
self::assertSame($filteredArray, $baseInputFilter->getRawValues());
677+
}
678+
606679
public function addMethodArgumentsProvider()
607680
{
608681
$inputTypes = $this->inputProvider();

test/CollectionInputFilterTest.php

+31
Original file line numberDiff line numberDiff line change
@@ -765,4 +765,35 @@ public function testUsesMessageFromComposedNotEmptyValidatorWhenRequiredButColle
765765
[NotEmpty::IS_EMPTY => $message],
766766
], $this->inputFilter->getMessages());
767767
}
768+
769+
public function testSetDataUsingSetDataAndRunningIsValidReturningSameAsOriginalForUnfilteredData()
770+
{
771+
$filteredArray = [
772+
[
773+
'bar' => 'foo',
774+
'foo' => 'bar',
775+
],
776+
];
777+
778+
$unfilteredArray = array_merge(
779+
$filteredArray,
780+
[
781+
[
782+
'foo' => 'bar',
783+
],
784+
]
785+
);
786+
787+
/** @var BaseInputFilter $baseInputFilter */
788+
$baseInputFilter = (new BaseInputFilter())
789+
->add(new Input(), 'bar');
790+
791+
/** @var CollectionInputFilter $collectionInputFilter */
792+
$collectionInputFilter = (new CollectionInputFilter())->setInputFilter($baseInputFilter);
793+
$collectionInputFilter->setData($unfilteredArray);
794+
795+
$collectionInputFilter->isValid();
796+
797+
self::assertSame($unfilteredArray, $collectionInputFilter->getUnfilteredData());
798+
}
768799
}

0 commit comments

Comments
 (0)