PHP code example of haseebmirza / laravel-compliance-export

1. Go to this page and download the library: Download haseebmirza/laravel-compliance-export 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/ */

    

haseebmirza / laravel-compliance-export example snippets


use HaseebMirza\ComplianceExport\ComplianceExport;

$payload = ComplianceExport::for('user', 5)->toArray();
$json    = ComplianceExport::for('user', 5)->toJson();
$csv     = ComplianceExport::for('user', 5)->toCsv();
$pdf     = ComplianceExport::for('user', 5)->toFormat('pdf');

// config/compliance-export.php
'subject_models' => [
    'user' => \App\Models\User::class,
],

app(ComplianceExport::class)->registerSubjectResolver('user', function (string $subjectType, mixed $subjectId) {
    return User::query()->with('roles')->find($subjectId);
});

use HaseebMirza\ComplianceExport\Contracts\ContextualDataSource;
use HaseebMirza\ComplianceExport\Support\SubjectContext;

class NotesDataSource implements ContextualDataSource
{
    public function resolveForSubject(SubjectContext $subject): array
    {
        return [
            'subject_id' => $subject->subjectId(),
            'resolved' => $subject->isResolved(),
        ];
    }
}

app(ComplianceExport::class)->registerSource('notes', function (string $subjectType, mixed $subjectId): array {
    return [
        ['body' => 'Performance review shared'],
        ['body' => 'Payroll profile updated'],
    ];
});

$payload = ComplianceExport::for('user', 5)
    ->withSource('notes')
    ->toArray();

// config/compliance-export.php
'presets' => [
    'default' => ['profile', 'activity'],
    'audit'   => ['profile', 'activity', 'notes'],
],

$payload = ComplianceExport::for('user', 5)
    ->withPreset('audit')
    ->toArray();

$payload = ComplianceExport::for('user', 5)
    ->withPreset('default')
    ->withSource('notes')
    ->toArray();

$json = ComplianceExport::for('user', 5)->toJson();
$csv  = ComplianceExport::for('user', 5)->toCsv();
$pdf  = ComplianceExport::for('user', 5)->toFormat('pdf');

app(ComplianceExport::class)->registerExporter('txt', App\Exports\PlainTextExporter::class);

$path = ComplianceExport::for('user', 5)
    ->saveTo(storage_path('app/exports/user-5.json'));

$path = ComplianceExport::for('user', 5)
    ->save(storage_path('app/exports'));

ComplianceExport::for('user', 5)->saveFormat('csv', storage_path('app/exports'));
ComplianceExport::for('user', 5)->saveFormat('pdf', storage_path('app/exports'));

app(ComplianceExport::class)->mergeMeta([
    'exported_by' => 'compliance-bot',
    'request_reference' => 'REQ-1001',
]);

use HaseebMirza\ComplianceExport\Contracts\PayloadValidator;
use HaseebMirza\ComplianceExport\Exceptions\InvalidExportPayloadException;

class EmployeeOnlyValidator implements PayloadValidator
{
    public function validate(array $payload): void
    {
        if (($payload['meta']['subject_type'] ?? null) !== 'employee') {
            throw new InvalidExportPayloadException('Only employee exports are allowed.');
        }
    }
}

// config/compliance-export.php
'ui' => [
    'enabled' => true,
    'path' => 'compliance-export',
    'launcher' => [
        'enabled' => true,
        'local_only' => true,
    ],
],
bash
php artisan vendor:publish --tag=compliance-export-config

/compliance-export

/compliance-export?subject_type=user&subject_id=5&format=csv