PHP code example of morrislaptop / popo-factory

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

    

morrislaptop / popo-factory example snippets


use Morrislaptop\PopoFactory\PopoFactory;

class PersonData
{
    public function __construct(
        public $firstName,
        public string $lastName,
        public string $email,
        public string $homeAddress,
        public ?string $companyName,
        public string $workAddress,
        public Carbon $dob,
        public PersonData $spouse,
    ) {
    }
}

PopoFactory::new(PersonData::class)->make();

// Creates two DTOs in an array
PopoFactory::new(PersonData::class)->count(2)->make();

// Sets the first name of every person to "Jim"
PopoFactory::new(PersonData::class)
    ->random()
    ->state([
        'firstName' => 'Jim',
    ])
    ->make();

// Alternates the names of each person between "Jim" and "Susie"
PopoFactory::new(PersonData::class)
    ->random()
    ->sequence(
        [ 'firstName' => 'Jim' ],
        [ 'firstName' => 'Susie' ]
    )
    ->make();


/**
 * @method PersonData make
 */
class PersonDataFactory extends PopoFactory
{
    public static function factory(): static
    {
        return static::new(PersonData::class)->state([
            'firstName' => 'Craig'
        ]);
    }

    public function gotNoJob() {
        return $this->state([
            'companyName' => null,
        ]);
    }

    public function worksAtHome() {
        return $this->state(function ($attributes) {
            return [
                'workAddress' => $attributes['homeAddress']
            ];
        });
    }
}

$person = PersonDataFactory::factory()
            ->gotNoJob()
            ->worksAtHome()
            ->make();


use Morrislaptop\PopoFactory\PropertyFactory;

use Anteris\FakerMap\FakerMap;

PropertyFactory::registerProvider('Carbon\Carbon', fn(FakerMap $fakerMap) => Carbon::parse(
    $fakerMap->closest('dateTime')->fake()
));