Skip to content

Commit

Permalink
Merge b9ccca9 into 9289be0
Browse files Browse the repository at this point in the history
  • Loading branch information
albertcansado committed Aug 22, 2019
2 parents 9289be0 + b9ccca9 commit a9e4c7e
Show file tree
Hide file tree
Showing 2 changed files with 121 additions and 1 deletion.
65 changes: 64 additions & 1 deletion src/Filter.php
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
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);
}
}

0 comments on commit a9e4c7e

Please sign in to comment.