PHP code example of edulazaro / laranon

1. Go to this page and download the library: Download edulazaro/laranon 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/ */

    

edulazaro / laranon example snippets


use EduLazaro\Laranon\Laranon;

$result = Laranon::scope("chat-{$sessionId}")->anonymize(
    'Our client John Smith, SSN 536-90-4399, requests the transfer to GB29 NWBK 6016 1331 9268 19.'
);

$result->text;
// "Our client «PER_1» «AP_1», SSN «SSN_1», requests the transfer to «IBAN_1»."

$llmResponse = $chat->send($result->text);

$result->restore($llmResponse);
// Tokens are replaced back with the real values. The map never left your server.

use EduLazaro\Laranon\Laranon;

$result = Laranon::anonymize($text);   // AnonymizedText { text, map }
$restored = $result->restore($output); // tokens back to original values

use EduLazaro\Laranon\Anonymizer;

$anon = Anonymizer::create();          // the map lives inside $anon

// Anonymize the whole prompt (targets the 'content' key of each message)
$messages = $anon->anonymize($messages, 'content');

// ...call the model, it decides to use a tool...

// Decode the tool arguments before running the tool (search on real values)
$args = $anon->restore($response->toolCall, 'arguments');
$result = runTool($args);
$messages[] = ['role' => 'tool', 'content' => json_encode($result)];
$messages = $anon->anonymize($messages, 'content'); // fresh PII, same map

// ...second model call...

// Decode the reply before showing it to the user
$reply = $anon->restore($response->content);
// $anon goes out of scope here; the map is gone. Nothing was persisted.

$anon->anonymize($string);                              // a string
$anon->anonymize([$a, $b, $c]);                          // a list of strings
$anon->anonymize($messages, 'content');                 // key in each element
$anon->anonymize($messages, 'payload.text');            // nested key (dot)
$anon->anonymize($messages, ['content', 'extra']);      // several keys
$anon->anonymize($messages, 'tool_calls.*.arguments');  // '*' wildcard (nested lists)

// Turn 1
Laranon::scope('chat-42')->anonymize('SSN 536-90-4399');      // «SSN_1»

// Turn 2: same value, same token
Laranon::scope('chat-42')->anonymize('On file: 536-90-4399'); // «SSN_1»

// Restore later without carrying the map around
Laranon::scope('chat-42')->restore($llmResponse);

// Drop the map: pseudonymization becomes effective anonymization
Laranon::scope('chat-42')->forget();

Laranon::strategy('faker')->anonymize($text);  // valid surrogates, same format
Laranon::strategy('redact')->anonymize($text); // [DNI], one-way, nothing vaulted

Laranon::withEntities([
    ['value' => 'Acme Health Ltd', 'type' => 'entity'],
    ['value' => '[email protected]', 'type' => 'email'],
])->anonymize($text);

class Client extends Model
{
    use \EduLazaro\Laranon\Concerns\Anonymizable;

    protected array $anonymizable = ['email', 'phone'];
}

Laranon::withModels($case->clients)->anonymize($text);

Laranon::withStableEntities([
    ['value' => $client->company_name, 'type' => 'entity', 'id' => $client->id],
])->anonymize($text); // always «ENT_{$client->id}»

Laranon::only(['dni', 'iban'])->anonymize($text);
Laranon::except('url')->anonymize($text);

$stream = Laranon::stream($result); // or ->scope('chat-42')->stream()

foreach ($chunks as $chunk) {
    echo $stream->push($chunk); // safe to emit; split tokens are buffered
}

echo $stream->flush();

// config/logging.php
'stack' => [
    'driver' => 'stack',
    'tap' => [\EduLazaro\Laranon\Integrations\ScrubLogsTap::class],
    // ...
],

Http::scrubPii()->post($url, $payload); // one-way redaction of the request body
bash
php artisan vendor:publish --tag=laranon-config
php artisan vendor:publish --tag=laranon-migrations
bash
php artisan laranon:scan storage/corpus.txt --types=dni,iban,person