CodeIgniter v4.5 API

Fabricator
in package

Fabricator

Bridge class for using Faker to create example data based on model specifications.

Tags
see
FabricatorTest

Table of Contents

Properties

$defaultFormatter  : string
Default formatter to use when nothing is detected
$dateFields  : array<string|int, mixed>
Date fields present in the model
$faker  : Generator
Locale-specific Faker instance
$formatters  : array<string|int, mixed>|null
Map of properties and their formatter to use
$locale  : string
Locale used to initialize Faker
$model  : Model|object
Model instance (can be non-framework if it follows framework design)
$overrides  : array<string|int, mixed>
Array of data to add or override faked versions
$tableCounts  : array<string|int, mixed>
Array of counts for fabricated items
$tempOverrides  : array<string|int, mixed>|null
Array of single-use data to override faked versions
$modifiedFields  : array{unique: array, optional: array, valid: array}
Fields to be modified before applying any formatter.

Methods

__construct()  : mixed
Store the model instance and initialize Faker to the locale.
create()  : array<string|int, mixed>|object
Generate new entities from the database
downCount()  : int
Decrement the count for a table
getCount()  : int
Get the count for a specific table
getFaker()  : Generator
Returns the Faker generator
getFormatters()  : array<string|int, mixed>|null
Returns the current formatters
getLocale()  : string
Returns the locale
getModel()  : object
Returns the model instance
getOverrides()  : array<string|int, mixed>
Return and reset tempOverrides
make()  : array<string|int, mixed>|object
Generate new entities with faked data
makeArray()  : array<string|int, mixed>
Generate an array of faked data
makeObject()  : object
Generate an object of faked data
resetCounts()  : mixed
Reset internal counts
setCount()  : int
Set the count for a specific table
setFormatters()  : self
Set the formatters to use. Will attempt to autodetect if none are available.
setOptional()  : static
Set a field to be optional.
setOverrides()  : self
Set the overrides, once or persistent
setUnique()  : static
Set a field to be unique.
setValid()  : static
Set a field to be valid using a callback.
upCount()  : int
Increment the count for a table
createMock()  : array<string|int, mixed>|object
Generate new database entities without actually inserting them
detectFormatters()  : self
Try to identify the appropriate Faker formatter for each field.
guessFormatter()  : string
Guess at the correct formatter to match a field name.

Properties

$defaultFormatter

Default formatter to use when nothing is detected

public string $defaultFormatter = 'word'

$dateFields

Date fields present in the model

protected array<string|int, mixed> $dateFields = []

$faker

Locale-specific Faker instance

protected Generator $faker

$formatters

Map of properties and their formatter to use

protected array<string|int, mixed>|null $formatters

$locale

Locale used to initialize Faker

protected string $locale

$model

Model instance (can be non-framework if it follows framework design)

protected Model|object $model

$overrides

Array of data to add or override faked versions

protected array<string|int, mixed> $overrides = []

$tableCounts

Array of counts for fabricated items

protected static array<string|int, mixed> $tableCounts = []

$tempOverrides

Array of single-use data to override faked versions

protected array<string|int, mixed>|null $tempOverrides

$modifiedFields

Fields to be modified before applying any formatter.

private array{unique: array, optional: array, valid: array} $modifiedFields = ['unique' => [], 'optional' => [], 'valid' => []]

Methods

__construct()

Store the model instance and initialize Faker to the locale.

public __construct(object|string $model[, array<string|int, mixed>|null $formatters = null ][, string|null $locale = null ]) : mixed
Parameters
$model : object|string

Instance or classname of the model to use

$formatters : array<string|int, mixed>|null = null

Array of property => formatter

$locale : string|null = null

Locale for Faker provider

Tags
throws
InvalidArgumentException

create()

Generate new entities from the database

public create([int|null $count = null ][, bool $mock = false ]) : array<string|int, mixed>|object
Parameters
$count : int|null = null

Optional number to create a collection

$mock : bool = false

Whether to execute or mock the insertion

Tags
throws
FrameworkException
Return values
array<string|int, mixed>|object

An array or object (based on returnType), or an array of returnTypes

downCount()

Decrement the count for a table

public static downCount(string $table) : int
Parameters
$table : string

Name of the target table

Return values
int

The new count value

getCount()

Get the count for a specific table

public static getCount(string $table) : int
Parameters
$table : string

Name of the target table

Return values
int

getFaker()

Returns the Faker generator

public getFaker() : Generator
Return values
Generator

getFormatters()

Returns the current formatters

public getFormatters() : array<string|int, mixed>|null
Return values
array<string|int, mixed>|null

getLocale()

Returns the locale

public getLocale() : string
Return values
string

getModel()

Returns the model instance

public getModel() : object
Return values
object

Framework or compatible model

getOverrides()

Return and reset tempOverrides

public getOverrides() : array<string|int, mixed>
Return values
array<string|int, mixed>

make()

Generate new entities with faked data

public make([int|null $count = null ]) : array<string|int, mixed>|object
Parameters
$count : int|null = null

Optional number to create a collection

Return values
array<string|int, mixed>|object

An array or object (based on returnType), or an array of returnTypes

makeArray()

Generate an array of faked data

public makeArray() : array<string|int, mixed>
Tags
throws
RuntimeException
Return values
array<string|int, mixed>

An array of faked data

makeObject()

Generate an object of faked data

public makeObject([string|null $className = null ]) : object
Parameters
$className : string|null = null

Class name of the object to create; null to use model default

Tags
throws
RuntimeException
Return values
object

An instance of the class with faked data

resetCounts()

Reset internal counts

public static resetCounts() : mixed

setCount()

Set the count for a specific table

public static setCount(string $table, int $count) : int
Parameters
$table : string

Name of the target table

$count : int

Count value

Return values
int

The new count value

setFormatters()

Set the formatters to use. Will attempt to autodetect if none are available.

public setFormatters([array<string|int, mixed>|null $formatters = null ]) : self
Parameters
$formatters : array<string|int, mixed>|null = null

Array of [field => formatter], or null to detect

Return values
self

setOptional()

Set a field to be optional.

public setOptional(string $field[, float $weight = 0.5 ][, mixed $default = null ]) : static
Parameters
$field : string
$weight : float = 0.5

A probability between 0 and 1, 0 means that we always get the default value.

$default : mixed = null
Return values
static

setOverrides()

Set the overrides, once or persistent

public setOverrides([array<string|int, mixed> $overrides = [] ][, bool $persist = true ]) : self
Parameters
$overrides : array<string|int, mixed> = []

Array of [field => value]

$persist : bool = true

Whether these overrides should persist through the next operation

Return values
self

setUnique()

Set a field to be unique.

public setUnique(string $field[, bool $reset = false ][, int $maxRetries = 10000 ]) : static
Parameters
$field : string
$reset : bool = false

If set to true, resets the list of existing values

$maxRetries : int = 10000

Maximum number of retries to find a unique value, After which an OverflowException is thrown.

Return values
static

setValid()

Set a field to be valid using a callback.

public setValid(string $field[, callable(mixed): bool|null $validator = null ][, int $maxRetries = 10000 ]) : static
Parameters
$field : string
$validator : callable(mixed): bool|null = null

A function returning true for valid values

$maxRetries : int = 10000

Maximum number of retries to find a valid value, After which an OverflowException is thrown.

Return values
static

upCount()

Increment the count for a table

public static upCount(string $table) : int
Parameters
$table : string

Name of the target table

Return values
int

The new count value

createMock()

Generate new database entities without actually inserting them

protected createMock([int|null $count = null ]) : array<string|int, mixed>|object
Parameters
$count : int|null = null

Optional number to create a collection

Return values
array<string|int, mixed>|object

An array or object (based on returnType), or an array of returnTypes

detectFormatters()

Try to identify the appropriate Faker formatter for each field.

protected detectFormatters() : self
Return values
self

guessFormatter()

Guess at the correct formatter to match a field name.

protected guessFormatter(string $field) : string
Parameters
$field : string

Name of the field

Return values
string

Name of the formatter


        
On this page

Search results