PHP code example of mtownsend / array-redactor

1. Go to this page and download the library: Download mtownsend/array-redactor library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

mtownsend / array-redactor example snippets


/*
 * Package Service Providers...
 */
Mtownsend\ArrayRedactor\Providers\ArrayRedactorServiceProvider::class,

$app->register(Mtownsend\ArrayRedactor\Providers\ArrayRedactorServiceProvider::class);

use Mtownsend\ArrayRedactor\ArrayRedactor;

// An example array, maybe a request being made to/from an API application you wish to log in your database
$login = [
    'email' => '[email protected]',
    'password' => 'secret123',
    'data' => [
        'session_id' => 'z481jf0an4kasnc8a84aj831'
    ],
];

$redactor = (new ArrayRedactor($login, ['password', 'session_id']))->redact();

// $redactor will return:
[
    'email' => '[email protected]',
    'password' => '[REDACTED]',
    'data' => [
        'session_id' => '[REDACTED]'
    ],
];

$json = json_encode([
    'email' => '[email protected]',
    'password' => 'secret123',
    'data' => [
        'session_id' => 'z481jf0an4kasnc8a84aj831'
    ],
]);

$redactor = (new ArrayRedactor($json, ['password', 'session_id']))->redact();

// $redactor will return:
[
    'email' => '[email protected]',
    'password' => '[REDACTED]',
    'data' => [
        'session_id' => '[REDACTED]'
    ],
];

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

$redactor = (new ArrayRedactor($login, ['password', 'session_id']))->redactToJson();

// $redactor will return:
"{
	"email": "[email protected]",
	"password": "[REDACTED]",
	"data": {
		"session_id": "[REDACTED]"
	}
}"

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

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

// $redactor will return:
[
    'email' => '[email protected]',
    'password' => null,
    'data' => [
        'session_id' => null
    ],
];

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

$redactor = (new ArrayRedactor($login, ['password', 'session_id'], null))();

// $redactor will return:
[
    'email' => '[email protected]',
    'password' => null,
    'data' => [
        'session_id' => null
    ],
];

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

$redactor = (new ArrayRedactor)->content($login)->keys(['password'])->ink(null)->redact();

// $redactor will return:
[
    'email' => '[email protected]',
    'password' => null,
    'data' => [
        'session_id' => 'z481jf0an4kasnc8a84aj831'
    ],
];

array_redactor($array, $keys, $ink)->redact();
// or...
array_redactor()->content($array)->keys(['current_password', 'new_password'])->ink('████████')->redact();

'ArrayRedactor' => Mtownsend\ArrayRedactor\Facades\ArrayRedactor::class,

use ArrayRedactor;

// Laravel prefills our keys() and ink() methods for us from the config file
ArrayRedactor::content($array)->redact();

try {
    $redactor = (new ArrayRedactor('i am an invalid argument', ['password']))->redact();
} catch (\Mtownsend\ArrayRedactor\Exceptions\ArrayRedactorException $exception) {
    // do something...
}
`
php artisan vendor:publish --provider="Mtownsend\ArrayRedactor\Providers\ArrayRedactorServiceProvider"