abstract class Factory

Base abstract class for all factory classes.

Properties

protected array $afterCreatingCallbacks

Array containing callbacks that are used after database record is successfully saved.

protected int $count

Value for how many insertions to be performed by factory.

protected FactoryFaker $faker

Instance of Faker\Factory object.

protected string $modelName

The model used by the child factory class.

protected array $states

States for overriding definition.

Methods

__construct()

Constructor for Faker class. Should be called by any child class that overrides this constructor.

array
definition()

Define the model's default state.

afterCreating(callable $callback)

Adds callback to afterCreatingCallbacks array.

string
append()

Helper function for satisfying complexity or length requirements.

array|object
create(array $attributes = [])

Create a record(s) in the database.

configure()

Configure function used for registering afterCreating callbacks.

count(int $count)

Specify number of records to insert into the database.

static Factory
factory(mixed ...$params)

Returns instance of new child factory class.

object|null
insert(array $data, string $modelName)

Insert data into the database table.

void
invokeAfterCreatingCallbacks(object $model)

Calls all registered afterCreating callbacks associated with a factory.

array
invokeStateCallbacks(array $attributes, array $data)

Calls state callback functions.

sequence(array ...$sequence)

Registers sequence callbacks.

state(callable $state)

Manages state for values of keys that should be overridden by state functions.

Details

at line 52
__construct()

Constructor for Faker class. Should be called by any child class that overrides this constructor.

at line 62
abstract protected array definition()

Define the model's default state.

Return Value

array

at line 70
Factory afterCreating(callable $callback)

Adds callback to afterCreatingCallbacks array.

Parameters

callable $callback

The anonymous callback for afterCreating.

Return Value

Factory

at line 81
protected string append()

Helper function for satisfying complexity or length requirements.

Return Value

string

A string containing a random digit, a lower case character, an upper case character, and a special character.

at line 97
array|object create(array $attributes = [])

Create a record(s) in the database.

Parameters

array $attributes

The an associative array attributes used to override default definition values.

Return Value

array|object

The array of models that were created or a single object model if just one record is inserted.

at line 125
protected Factory configure()

Configure function used for registering afterCreating callbacks.

Return Value

Factory

at line 135
Factory count(int $count)

Specify number of records to insert into the database.

Parameters

int $count

The number of records to create.

Return Value

Factory

at line 147
static Factory factory(mixed ...$params)

Returns instance of new child factory class.

Parameters

mixed ...$params

Params for factory.

Return Value

Factory

The child factory class.

at line 159
protected object|null insert(array $data, string $modelName)

Insert data into the database table.

Parameters

array $data

Data to be saved in new database record.

string $modelName

The name of the model to reference correct table.

Return Value

object|null

The model object if save is successful. Otherwise, we return null.

at line 192
protected void invokeAfterCreatingCallbacks(object $model)

Calls all registered afterCreating callbacks associated with a factory.

Parameters

object $model

The parent model

Return Value

void

at line 207
protected array invokeStateCallbacks(array $attributes, array $data)

Calls state callback functions.

Parameters

array $attributes

The attributes used to override default definition values.

array $data

Data to be saved in new database record.

Return Value

array

$data The data that contains values that overrides defaults found in definition.

at line 222
Factory sequence(array ...$sequence)

Registers sequence callbacks.

Parameters

array ...$sequence

Sequence of values to alternate between for consecutive record creations when using count.

Return Value

Factory

at line 236
Factory state(callable $state)

Manages state for values of keys that should be overridden by state functions.

Parameters

callable $state

The anonymous function for overriding a value for a specified key.

Return Value

Factory