\League\FactoryMuffinFactory

This is the factory class.

This class is not intended to be used directly, but should be used through the provided facade. The only time where you should be directly calling methods here should be when you're using method chaining after initially using the facade.

Summary

Methods
Properties
Constants
setFakerLocale()
setSaveMethod()
setDeleteMethod()
setCustomMaker()
setCustomSetter()
setCustomSaver()
setCustomDeleter()
seed()
create()
pending()
isPending()
saved()
isSaved()
isPendingOrSaved()
deleteSaved()
instance()
attributesFor()
define()
generateAttr()
getFaker()
loadFactories()
No public properties found
No constants found
No protected methods found
No protected properties found
N/A
persist()
triggerCallback()
make()
getGroup()
getModelClass()
makeClass()
setAttribute()
save()
delete()
getFactoryAttrs()
loadDirectory()
$factories
$callbacks
$pending
$saved
$saveMethod
$deleteMethod
$customMaker
$customSetter
$customSaver
$customDeleter
$faker
$fakerLocale
N/A

Properties

$factories

$factories : array

The array of factories.

Type

array

$callbacks

$callbacks : array

The array of callbacks to trigger on instance/create.

Type

array

$pending

$pending : array

The array of objects we have created and are pending save.

Type

array

$saved

$saved : array

The array of objects we have created and have saved.

Type

array

$saveMethod

$saveMethod : string

This is the method used when saving objects.

Type

string

$deleteMethod

$deleteMethod : string

This is the method used when deleting objects.

Type

string

$customMaker

$customMaker : \Closure

This is the custom model maker closure.

Type

\Closure

$customSetter

$customSetter : \Closure

This is the custom attribute setter closure.

Type

\Closure

$customSaver

$customSaver : \Closure

This is the custom model saver closure.

Type

\Closure

$customDeleter

$customDeleter : \Closure

This is the custom model deleter closure.

Type

\Closure

$faker

$faker : \Faker\Generator

The faker instance.

Type

\Faker\Generator

$fakerLocale

$fakerLocale : string

The faker localization.

Type

string

Methods

setFakerLocale()

setFakerLocale(string  $local) : $this

Set the faker locale.

Parameters

string $local

The faker locale.

Returns

$this

setSaveMethod()

setSaveMethod(string  $method) : $this

Set the method we use when saving objects.

Parameters

string $method

The save method name.

Returns

$this

setDeleteMethod()

setDeleteMethod(string  $method) : $this

Set the method we use when deleting objects.

Parameters

string $method

The delete method name.

Returns

$this

setCustomMaker()

setCustomMaker(\Closure  $maker) : $this

Set the custom maker closure.

Parameters

\Closure $maker

Returns

$this

setCustomSetter()

setCustomSetter(\Closure  $setter) : $this

Set the custom setter closure.

Parameters

\Closure $setter

Returns

$this

setCustomSaver()

setCustomSaver(\Closure  $saver) : $this

Set the custom saver closure.

Parameters

\Closure $saver

Returns

$this

setCustomDeleter()

setCustomDeleter(\Closure  $deleter) : $this

Set the custom deleter closure.

Parameters

\Closure $deleter

Returns

$this

seed()

seed(integer  $times, string  $model, array  $attr = array()) : array

Returns multiple versions of an object.

These objects are generated by the create function, so are saved to the database.

Parameters

integer $times

The number of models to create.

string $model

The model class name.

array $attr

The model attributes.

Returns

array

create()

create(string  $model, array  $attr = array()) : object

Creates and saves in db an instance of the model.

This object will be generated with mock attributes.

Parameters

string $model

The model class name.

array $attr

The model attributes.

Returns

object

pending()

pending() : array

Return an array of objects to be saved.

Returns

array

isPending()

isPending(object  $object) : boolean

Is the object going to be saved?

Parameters

object $object

The model instance.

Returns

boolean

saved()

saved() : array

Return an array of saved objects.

Returns

array

isSaved()

isSaved(object  $object) : boolean

Is the object saved?

Parameters

object $object

The model instance.

Returns

boolean

isPendingOrSaved()

isPendingOrSaved(object  $object) : boolean

Is the object saved or will be saved?

Parameters

object $object

The model instance.

Returns

boolean

deleteSaved()

deleteSaved() 

Call the delete method on any saved objects.

Throws

\League\FactoryMuffin\Exceptions\DeletingFailedException

return $this

instance()

instance(string  $model, array  $attr = array()) : object

Return an instance of the model.

This does not save it in the database. Use create for that.

Parameters

string $model

The model class name.

array $attr

The model attributes.

Returns

object

attributesFor()

attributesFor(object  $object, array  $attr = array()) : array

Returns the mock attributes for the model.

Parameters

object $object

The model instance.

array $attr

The model attributes.

Returns

array

define()

define(string  $model, array  $definition = array(), \Closure|null  $callback = null) : $this

Define a new model factory.

Parameters

string $model

The model class name.

array $definition

The attribute definitions.

\Closure|null $callback

The closure callback.

Returns

$this

generateAttr()

generateAttr(string  $kind, object|null  $object = null) : string|object

Generate the attributes.

This method will return a string, or an instance of the model.

Parameters

string $kind

The kind of attribute.

object|null $object

The model instance.

Returns

string|object

getFaker()

getFaker() : \Faker\Generator

Get the faker instance.

Returns

\Faker\Generator

loadFactories()

loadFactories(string|array<mixed,string>  $paths) : $this

Load the specified factories.

This method expects either a single path to a directory containing php files, or an array of directory paths, and will include_once every file. These files should contain factory definitions for your models.

Parameters

string|array<mixed,string> $paths

The directory path(s) to load.

Throws

\League\FactoryMuffin\Exceptions\DirectoryNotFoundException

Returns

$this

persist()

persist(object  $object) : void

Save the object to the database.

Parameters

object $object

The model instance.

Throws

\League\FactoryMuffin\Exceptions\SaveFailedException

triggerCallback()

triggerCallback(object  $object) : boolean

Trigger the callback if we have one.

Parameters

object $object

The model instance.

Returns

boolean

make()

make(string  $model, array  $attr, boolean  $save) : object

Make an instance of the model.

Parameters

string $model

The model class name.

array $attr

The model attributes.

boolean $save

Are we saving, or just creating an instance?

Returns

object

getGroup()

getGroup(string  $model) : string|null

Returns the group name for this factory definition.

Parameters

string $model

The model class name.

Returns

string|null

getModelClass()

getModelClass(string  $model, string|null  $group) : string

Returns the real model class without the group prefix.

Parameters

string $model

The model class name.

string|null $group

The model group name.

Returns

string

makeClass()

makeClass(string  $class) : object

Make an instance of the class.

Parameters

string $class

The class name.

Throws

\League\FactoryMuffin\Exceptions\ModelNotFoundException

Returns

object

setAttribute()

setAttribute(object  $object, string  $name, mixed  $value) : void

Set an attribute on a model instance.

Parameters

object $object

The model instance.

string $name

The attribute name.

mixed $value

The attribute value.

save()

save(object  $object) : mixed

Save our object to the db, and keep track of it.

Parameters

object $object

The model instance.

Throws

\League\FactoryMuffin\Exceptions\SaveMethodNotFoundException

Returns

mixed

delete()

delete(object  $object) : mixed

Delete our object from the db.

Parameters

object $object

The model instance.

Throws

\League\FactoryMuffin\Exceptions\DeleteMethodNotFoundException

Returns

mixed

getFactoryAttrs()

getFactoryAttrs(string  $model) : array

Get factory attributes.

Parameters

string $model

The model class name.

Throws

\League\FactoryMuffin\Exceptions\NoDefinedFactoryException

Returns

array

loadDirectory()

loadDirectory(string  $path) : void

Load all the files in a directory.

Parameters

string $path

The directory path to load.