Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 64 additions & 1 deletion src/Filter.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ class Filter
*/
protected $globalChain = null;

protected $whiteList = [];

/**
* Set a filter for a value on a specific key
*
Expand Down Expand Up @@ -109,7 +111,9 @@ public function addFilterRule(FilterRule $rule, $key = null)
*/
public function filter(array $data)
{
$data = $this->filterArrayWithGlobalChain($data);
$data = $this->filterArrayWithGlobalChain(
$this->applyWhiteList($data)
);

$this->data = new Container($data);

Expand All @@ -118,6 +122,65 @@ public function filter(array $data)
return $this->data->getArrayCopy();
}

/**
* Set a list of keys to be returned in the end
*
* @param array $keys
* @return void
*/
public function whiteList(array $keys)
{
$this->whiteList = $keys;
}

/**
* Purge initial fields using white list
*
* @param array $data
* @return array
*/
protected function applyWhiteList(array $data)
{
if (empty($this->whiteList)) {
return $data;
}

$allowed = [];
foreach ($this->whiteList as $key) {
if (strpos($key, '.') !== false) {
$allowed = $this->deepWhitelist($key, $allowed, $data);
} else {
$allowed[$key] = $data[$key];
}
}

return $allowed;
}

/**
* Uses dot-notation to purge initial data
*
* @param string $key
* @param array $result
* @param array $data
* @return array
*/
protected function deepWhitelist($key, array $result, array $data)
{
$parts = explode('.', $key);

$ref = &$result;
$dataRef = &$data;
foreach ($parts as $part) {
$ref = &$ref[$part];
$dataRef = &$dataRef[$part];
}

$ref = $dataRef;

return $result;
}

/**
* Filter all set fields with a global chain, recursively
*
Expand Down
57 changes: 57 additions & 0 deletions tests/FilterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -326,4 +326,61 @@ public function testRemoveNullOnAllMultidimensionalValues()

$this->assertEquals(['test2' => 'test', 'test4' => 'test'], $result);
}

/**
* Ensure the filter only returns the keys we specify on white list
*/
public function testFilterWithWhiteList()
{
$this->filter->whiteList(['first_name']);

$result = $this->filter->filter([
'first_name' => 'john',
'spam' => 'spam'
]);

$this->assertEquals(['first_name' => 'john'], $result);
}

/**
* Ensure the filter only returns the keys we specify on white list,
* this time using dot notation
*/
public function testFilterWithWhiteListSubArrays()
{
$this->filter->whiteList([
'first_name',
'test1',
'test2.test',
'test2.test3.test'
]);

$result = $this->filter->filter([
'first_name' => 'john',
'test1' => [
'test' => 'hi'
],
'test2' => [
'test' => 'hi test2',
'test3' => [
'test' => 'hi inner'
],
'spam2' => 'spam'
],
'spam' => 'spam'
]);

$this->assertEquals([
'first_name' => 'john',
'test1' => [
'test' => 'hi'
],
'test2' => [
'test' => 'hi test2',
'test3' => [
'test' => 'hi inner'
]
]
], $result);
}
}