php-shark-tank/anonymizer

Library to anonymize class properties

1.1.0 2023-05-13 21:24 UTC

README

Latest Stable Version Total Downloads Latest Unstable Version License

The Anonymizer is a library for PHP applications to make it easy to modify data of any object using PHP Attributes or other structured configurations.

Installation

With composer run

composer require php-shark-tank/anonymizer:^1.0

Documentation

Quick Example

<?php

declare(strict_types=1);

namespace App;

require_once __DIR__.'/../vendor/autoload.php';

use PHPSharkTank\Anonymizer\Anonymizer;
use PHPSharkTank\Anonymizer\Handler\CallbackHandler;
use PHPSharkTank\Anonymizer\Handler\NullHandler;
use PHPSharkTank\Anonymizer\Loader\AttributeLoader;
use PHPSharkTank\Anonymizer\Registry\HandlerRegistry;
use PHPSharkTank\Anonymizer\Visitor\GraphNavigator;
use PHPSharkTank\Anonymizer\Attribute\EnableAnonymize;
use PHPSharkTank\Anonymizer\Attribute\Handler;

#[EnableAnonymize]
class Person {
    #[Handler(value: 'callback', options: ['method' => 'getNameDefault'])]
    public string $name = '';
    
    #[Handler(value: 'null')]
    public ?string $nullable = '';

    public function getNameDefault(): string
    {
        return 'name';
    }
}

$anonymizer = new Anonymizer(new GraphNavigator(
    new AttributeLoader(),
        new HandlerRegistry([
            new CallbackHandler(),
            new NullHandler()
        ]),
));

$person = new Person();
$anonymizer->process($person);
var_dump($person);

Result:

/var/web/app ยป php index.php
/var/web/app/index.php.php:42:
class App\Person#10 (2) {
  public string $name =>
  string(4) "name"
  public ?string $nullable =>
  NULL
}