Fabricator
in package
Fabricator
Bridge class for using Faker to create example data based on model specifications.
Table of Contents
- $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
- __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() : $this
- Set the formatters to use. Will attempt to autodetect if none are available.
- setOverrides() : $this
- Set the overrides, once or persistent
- upCount() : int
- Increment the count for a table
- createMock() : array<string|int, mixed>|object
- Generate new database entities without actually inserting them
- detectFormatters() : $this
- 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
Methods
__construct()
Store the model instance and initialize Faker to the locale.
public
__construct(string|object $model[, array<string|int, mixed>|null $formatters = null ][, string|null $locale = null ]) : mixed
Parameters
- $model : string|object
-
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
Return values
mixed —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
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
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
Return values
object —An instance of the class with faked data
resetCounts()
Reset internal counts
public
static resetCounts() : mixed
Return values
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 ]) : $this
Parameters
- $formatters : array<string|int, mixed>|null = null
-
Array of [field => formatter], or null to detect
Return values
$this —setOverrides()
Set the overrides, once or persistent
public
setOverrides([array<string|int, mixed> $overrides = [] ][, bool $persist = true ]) : $this
Parameters
- $overrides : array<string|int, mixed> = []
-
Array of [field => value]
- $persist : bool = true
-
Whether these overrides should persist through the next operation
Return values
$this —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() : $this
Return values
$this —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