PHP code example of dezer32 / dto-faker

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

    

dezer32 / dto-faker example snippets


Dezer32\Libraries\Dto\Faker\Faker\FakerProvider::class

//for object:
$faker->fakeDto(Class::class, ['additional' => 'parameter']);

//only array:
$faker->fakeDtoArray(Class::class, ['additional' => 'parameter']);



$dto = Faker::fake(ClassNameDto::class);

// if you need additional parameters:
$dto = Faker::fake(ClassNameDto::class, ['additional' => 'parameters']);



declare(strict_types=1);

use Dezer32\Libraries\Dto\DataTransferObject;
use Dezer32\Libraries\Dto\Faker\{
        Attributes\FieldFaker,
        Helpers\FakerMethod
    };

class SimpleDto extends DataTransferObject {
    public function __construct(
        #[FieldFaker(FakerMethod::NUMBER_BETWEEN, 10, 100)]
        private int $number
    ) {}
    
    public  function getNumber(): int{
        return $this->number;
    }
}


declare(strict_types=1);

use Dezer32\Libraries\Dto\DataTransferObject;use Dezer32\Libraries\Dto\Faker\Attributes\FieldFaker;use Dezer32\Libraries\Dto\Faker\Helpers\FakerMethod;

class AnyClass {
    public function __construct(
        ...$args
    ) {}
}



declare(strict_types=1);

use Dezer32\Libraries\Dto\DataTransferObject;
use Dezer32\Libraries\Dto\Faker\{
        Attributes\CastFieldFaker,
        Helpers\FakerMethod
    };

class SimpleDto extends DataTransferObject {
    public function __construct(
        #[CastFieldFaker(AnyClassFakeCaster::class, 'any_args')]
        private AnyClass $var 
    ) {}
    
    public  function getVar(): AnyClass{
        return $this->var;
    }
}



declare(strict_types=1);

namespace Dezer32\Libraries\Dto\Faker\Tests\Fixtures;

use Dezer32\Libraries\Dto\Faker\Contracts\FakeCasterInterface;

class AnyClassFakeCaster implements FakeCasterInterface
{
    public function __construct(
        private string $param,
    ) {
    }

    public function generate(): AnyClass
    {
        return new AnyClass($this->param);
    }
}