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

Commit

Permalink
Merging develop to master in preparation for 2.10.0 release.
Browse files Browse the repository at this point in the history
  • Loading branch information
weierophinney committed Jan 30, 2019
2 parents 9715401 + d6b9c62 commit 4f52b71
Show file tree
Hide file tree
Showing 10 changed files with 231 additions and 11 deletions.
15 changes: 13 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,22 @@

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

## 2.9.2 - TBD
## 2.10.0 - 2019-01-30

### Added

- Nothing.
- [#176](https://github.com/zendframework/zend-inputfilter/pull/176) adds the interface `UnfilteredDataInterface`, with the following methods:

```php
public function getUnfilteredData() : array|object;
public function setUnfilteredData(array|object $data) : $this;
```

By default, the `BaseInputFilter` now implements this interface.

The primary purpose of the interface is to allow the ability to access ALL
original raw data, and not just the data the input filter knows about. This is
particularly useful with collections.

### Changed

Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
},
"extra": {
"branch-alias": {
"dev-master": "2.9.x-dev",
"dev-develop": "2.10.x-dev"
"dev-master": "2.10.x-dev",
"dev-develop": "2.11.x-dev"
},
"zf": {
"component": "Zend\\InputFilter",
Expand Down
2 changes: 1 addition & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

49 changes: 49 additions & 0 deletions docs/book/unfiltered-data.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# Unfiltered Data

> Since 2.10.0
On input filters, there are several methods for retrieving the data:

- `getValues()` will return all known values after filtering them.
- `getRawValues()` will return all known values with no filtering applied.
- `getUnknown()` returns the set of all unknown values (values with no
corresponding input or input filter).

At times, particularly when working with collections, you may want access to the
complete set of original data provided to the input filter. This can be
accomplished by merging the sets returned by `getRawValues()` and
`getUnknown()` when working with normal input filters, but this approach breaks
down when working with collections.

Version 2.10.0 introduces a new interface, `Zend\InputFilter\UnfilteredDataInterface`,
for dealing with this situation. `Zend\InputFilter\BaseInputFilter`, which
forms the parent class for all shipped input filter implementations, implements
the interface, which consists of the following methods:

```php
interface UnfilteredDataInterface
{
/**
* @return array|object
*/
public function getUnfilteredData()
{
return $this->unfilteredData;
}

/**
* @param array|object $data
* @return $this
*/
public function setUnfilteredData($data)
{
$this->unfilteredData = $data;
return $this;
}
}
```

The `setUnfilteredData()` method is called by `setData()` with the full `$data`
provided to that method, ensuring that `getUnfilteredData()` will always provide
the original data with which the input filter was initialized, with no filtering
applied.
7 changes: 4 additions & 3 deletions mkdocs.yml
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
docs_dir: docs/book
site_dir: docs/html
pages:
- index.md
- Intro: intro.md
- Home: index.md
- Introduction: intro.md
- Reference:
- Specifications: specs.md
- Files: file-input.md
- "Optional Input Filters": optional-input-filters.md
- "Unfiltered Data": unfiltered-data.md
site_name: zend-inputfilter
site_description: zend-inputfilter
repo_url: 'https://github.com/zendframework/zend-inputfilter'
copyright: 'Copyright (c) 2016-2017 <a href="http://www.zend.com/">Zend Technologies USA Inc.</a>'
copyright: 'Copyright (c) 2016-2019 <a href="http://www.zend.com/">Zend Technologies USA Inc.</a>'
32 changes: 30 additions & 2 deletions src/BaseInputFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
* @copyright Copyright (c) 2005-2019 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/

Expand All @@ -18,13 +18,19 @@ class BaseInputFilter implements
InputFilterInterface,
UnknownInputsCapableInterface,
InitializableInterface,
ReplaceableInputInterface
ReplaceableInputInterface,
UnfilteredDataInterface
{
/**
* @var null|array
*/
protected $data;

/**
* @var array|object
*/
protected $unfilteredData = [];

/**
* @var InputInterface[]|InputFilterInterface[]
*/
Expand Down Expand Up @@ -194,8 +200,12 @@ public function setData($data)
(is_object($data) ? get_class($data) : gettype($data))
));
}

$this->setUnfilteredData($data);

$this->data = $data;
$this->populate();

return $this;
}

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

return $this;
}

/**
* @return array|object
*/
public function getUnfilteredData()
{
return $this->unfilteredData;
}

/**
* @param array|object $data
* @return $this
*/
public function setUnfilteredData($data)
{
$this->unfilteredData = $data;
return $this;
}
}
2 changes: 2 additions & 0 deletions src/CollectionInputFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,8 @@ public function setData($data)
));
}

$this->setUnfilteredData($data);

foreach ($data as $item) {
if (is_array($item) || $item instanceof Traversable) {
continue;
Expand Down
25 changes: 25 additions & 0 deletions src/UnfilteredDataInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php
/**
* @see https://github.com/zendframework/zend-inputfilter for the canonical source repository
* @copyright Copyright (c) 2019 Zend Technologies USA Inc. (https://www.zend.com)
* @license https://github.com/zendframework/zend-inputfilter/blob/master/LICENSE.md New BSD License
*/

namespace Zend\InputFilter;

/**
* Ensures Inputs store unfiltered data and are capable of returning it
*/
interface UnfilteredDataInterface
{
/**
* @return array|object
*/
public function getUnfilteredData();

/**
* @param array|object $data
* @return $this
*/
public function setUnfilteredData($data);
}
75 changes: 74 additions & 1 deletion test/BaseInputFilterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,16 @@
use ArrayIterator;
use ArrayObject;
use FilterIterator;
use PHPUnit_Framework_MockObject_MockObject as MockObject;
use PHPUnit\Framework\TestCase;
use PHPUnit_Framework_MockObject_MockObject as MockObject;
use stdClass;
use Zend\InputFilter\BaseInputFilter;
use Zend\InputFilter\Exception\InvalidArgumentException;
use Zend\InputFilter\Exception\RuntimeException;
use Zend\InputFilter\Input;
use Zend\InputFilter\InputFilterInterface;
use Zend\InputFilter\InputInterface;
use Zend\InputFilter\UnfilteredDataInterface;

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

public function testInstanceOfUnfilteredDataInterface()
{
$baseInputFilter = new BaseInputFilter();

self::assertInstanceOf(
UnfilteredDataInterface::class,
$baseInputFilter,
sprintf('%s should implement %s', BaseInputFilter::class, UnfilteredDataInterface::class)
);
}

public function testGetUnfilteredDataReturnsArray()
{
$baseInputFilter = new BaseInputFilter();

self::assertInternalType('array', $baseInputFilter->getUnfilteredData());
}

public function testSetUnfilteredDataReturnsBaseInputFilter()
{
$baseInputFilter = new BaseInputFilter();

self::assertInstanceOf(BaseInputFilter::class, $baseInputFilter->setUnfilteredData([]));
}

public function testSettingAndReturningDataArrayUnfilteredDataInterface()
{
$testArray = [
'foo' => 'bar',
];

$baseInputFilter = new BaseInputFilter();
$baseInputFilter->setUnfilteredData($testArray);

self::assertSame($testArray, $baseInputFilter->getUnfilteredData());
}

public function testSettingAndReturnDataArrayUsingSetDataForUnfilteredDataInterface()
{
$testArray = [
'foo' => 'bar',
];

$baseInputFilter = new BaseInputFilter();
$baseInputFilter->setData($testArray);

self::assertSame($testArray, $baseInputFilter->getUnfilteredData());
}

public function testSetDataUsingSetDataAndApplyFiltersReturningSameAsOriginalForUnfilteredData()
{
$filteredArray = [
'bar' => 'foo',
];

$unfilteredArray = array_merge(
$filteredArray,
[
'foo' => 'bar',
]
);

/** @var BaseInputFilter $baseInputFilter */
$baseInputFilter = (new BaseInputFilter())
->add(new Input(), 'bar')
->setData($unfilteredArray);

self::assertSame($unfilteredArray, $baseInputFilter->getUnfilteredData());
self::assertSame($filteredArray, $baseInputFilter->getValues());
self::assertSame($filteredArray, $baseInputFilter->getRawValues());
}

public function addMethodArgumentsProvider()
{
$inputTypes = $this->inputProvider();
Expand Down
31 changes: 31 additions & 0 deletions test/CollectionInputFilterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -765,4 +765,35 @@ public function testUsesMessageFromComposedNotEmptyValidatorWhenRequiredButColle
[NotEmpty::IS_EMPTY => $message],
], $this->inputFilter->getMessages());
}

public function testSetDataUsingSetDataAndRunningIsValidReturningSameAsOriginalForUnfilteredData()
{
$filteredArray = [
[
'bar' => 'foo',
'foo' => 'bar',
],
];

$unfilteredArray = array_merge(
$filteredArray,
[
[
'foo' => 'bar',
],
]
);

/** @var BaseInputFilter $baseInputFilter */
$baseInputFilter = (new BaseInputFilter())
->add(new Input(), 'bar');

/** @var CollectionInputFilter $collectionInputFilter */
$collectionInputFilter = (new CollectionInputFilter())->setInputFilter($baseInputFilter);
$collectionInputFilter->setData($unfilteredArray);

$collectionInputFilter->isValid();

self::assertSame($unfilteredArray, $collectionInputFilter->getUnfilteredData());
}
}

0 comments on commit 4f52b71

Please sign in to comment.