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
30 changes: 30 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,36 @@ $redactor = (new ArrayRedactor($login, ['password', 'session_id']))->ink(null)->
];
```

If the value you pass into the third constructor argument or the `->ink()` method is callable, it will be invoked when redacting each value, and its return value will be used as the redacted value in the result:

```php
$login = [
'email' => '[email protected]',
'password' => 'secret123',
'data' => [
'session_id' => 'z481jf0an4kasnc8a84aj831'
],
];

$ink_function = function($val, $key) {
return $key === 'email' ? substr($val, stripos($val, '@')) : '[REDACTED]';
};

$redactor = (new ArrayRedactor($login, ['email', 'password', 'session_id'], $ink_function))->redact();
// or...
$redactor = (new ArrayRedactor($login, ['email', 'password', 'session_id']))->ink($ink_function)->redact();

// $redactor will return:
[
'email' => '@domain.com',
'password' => '[REDACTED]'
'data' => [
'session_id' => '[REDACTED]'
],
];

```

You can call the ``ArrayRedactor`` as a function and the magic ``__invoke()`` method will call the ``redact`` method for you.

```php
Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@
]
},
"require": {
"php": ">=5.6"
"php": ">=7.4|>=8.0"
},
"require-dev": {
"phpunit/phpunit": "^6.4"
"phpunit/phpunit": "^9"
},
"autoload-dev": {
"psr-4": {
Expand Down
4 changes: 2 additions & 2 deletions src/ArrayRedactor.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public function keys($keys = [])
*/
public function ink($ink = '[REDACTED]')
{
$this->ink = is_callable($ink) ? $ink() : $ink;
$this->ink = $ink;
return $this;
}

Expand All @@ -99,7 +99,7 @@ public function redact()
// Recursively traverse the array and redact the specified keys
array_walk_recursive($this->content, function (&$value, $key) {
if (in_array($key, $this->keys, true)) {
$value = $this->ink;
$value = is_callable($this->ink) ? call_user_func($this->ink, $value, $key) : $this->ink;
}
});

Expand Down
11 changes: 6 additions & 5 deletions tests/ArrayRedactorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

class ArrayRedactorTest extends TestCase
{
protected function setUp()
protected function setUp(): void
{
$this->content = [
'email' => '[email protected]',
Expand Down Expand Up @@ -98,10 +98,11 @@ public function can_omit_constructor_arguments_in_favor_of_methods()
/** @test */
public function can_accept_closure_in_ink()
{
$array = $this->content;
$result = (new ArrayRedactor())->content($this->content)->keys(['email'])->ink(function() use ($array) {
return substr($array['email'], stripos($array['email'], '@'));
})->redact();
$func = function(string $val, string $key): string {
return $key === 'email' ? substr($val, stripos($val, '@')) : $val;
};

$result = (new ArrayRedactor())->content($this->content)->keys(['email'])->ink($func)->redact();
$this->assertEquals(array_merge($this->content, [
'email' => '@gmail.com'
]), $result);
Expand Down