PHP code example of digital-creative / fakeable-data-transfer-object

1. Go to this page and download the library: Download digital-creative/fakeable-data-transfer-object 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/ */

    

digital-creative / fakeable-data-transfer-object example snippets


use DigitalCreative\FakeableDataTransferObject\DataTransferObject;

class SomeObject extends DataTransferObject
{
    public string $name;
    public int $age;
}

$dto1 = SomeObject::fake();
$dto2 = SomeObject::fake(age: 18);

echo $dto1->name; // random word
echo $dto1->age; // random int

echo $dto2->name; // random word
echo $dto2->age; // 18



namespace App\Providers;

use BenSampo\Enum\Enum;
use DigitalCreative\FakeableDataTransferObject\FakerRegistrar;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\ServiceProvider;

class AppServiceProvider extends ServiceProvider
{
    public function boot(): void
    {
        FakerRegistrar::register(Enum::class, function (string $class, mixed $value = null) {
            return blank($value) ? $class::getRandomInstance() : $class::fromValue($value);
        });
        
        FakerRegistrar::register(UploadedFile::class, function (string $class, mixed $value = null) {
            /** @var UploadedFile $class */
            return blank($value) ? $class::fake()->create('file.png') : $value;
        });

    }
}

use DigitalCreative\FakeableDataTransferObject\DataTransferObject;

class SomeObject extends DataTransferObject
{
    public GenderEnum $gender;
    public UploadedFile $attachment;
}