1. Go to this page and download the library: Download zero-to-prod/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/ */
zero-to-prod / factory example snippets
class UserFactory
{
use \Zerotoprod\Factory\Factory;
protected function definition(): array
{
return [
'first_name' => 'John',
'last_name' => 'N/A'
];
}
public static function factory(array $context = []): self
{
return new self($context);
}
}
// Create an array with default values
$user = UserFactory::factory()->make();
echo $user['first_name']; // 'John'
echo $user['last_name']; // 'N/A'
// Override specific values
$user = UserFactory::factory(['last_name' => 'Doe'])->make();
echo $user['first_name']; // 'John'
echo $user['last_name']; // 'Doe'
class User
{
public const first_name = 'first_name';
public $first_name;
public static function factory(array $context = []): UserFactory
{
return new UserFactory($context);
}
}
class UserFactory
{
use \Zerotoprod\Factory\Factory;
protected function definition(): array
{
return [
User::first_name => 'John'
];
}
}
// Call factory directly from the model
$user = User::factory()->make();
echo $user['first_name']; // 'John'
// Pass context to make() method
$user = User::factory()->make([User::first_name => 'Jane']);
echo $user['first_name']; // 'Jane'
class UserFactory
{
use \Zerotoprod\Factory\Factory;
protected function definition(): array
{
return [
'first_name' => 'John',
'last_name' => 'N/A',
'address' => [
'street' => '123 Main St',
],
'shipping_address' => [
'street' => '123 Main St',
]
];
}
// Simple field setting with array syntax
public function setFirstName(string $value): self
{
return $this->state(['first_name' => $value]);
}
// Nested field setting with dot notation
public function setAddress(string $street): self
{
return $this->state('address.street', $street);
}
// Complete array replacement
public function setShippingAddress(array $address): self
{
return $this->state('shipping_address', $address);
}
public static function factory(array $context = []): self
{
return new self($context);
}
}
// Usage examples
$user = User::factory()
->setFirstName('Jim') // Sets first_name to 'Jim'
->setAddress('bogus') // Sets address.street to 'bogus'
->setShippingAddress(['city' => 'NYC']) // Replaces entire shipping_address
->make();
echo $user['first_name']; // 'Jim'
echo $user['address']['street']; // 'bogus'
echo $user['shipping_address']['city']; // 'NYC'
class UserFactory
{
use \Zerotoprod\Factory\Factory;
protected function definition(): array
{
return [
'first_name' => 'John',
'last_name' => 'N/A',
'address' => [
'street' => 'Memory Lane'
]
];
}
public function setStreet(string $value): self
{
/** Dot Syntax */
return $this->state('address.street', $value);
}
public function setFirstName(string $value): self
{
/** Array Syntax */
return $this->state(['first_name' => $value]);
}
public function setLastName(): self
{
/** Closure Syntax - access context values to create dynamic state */
return $this->state(function ($context) {
return ['last_name' => $context['first_name']];
});
}
/** Static factory method for fluent instantiation */
public static function factory(array $context = []): self
{
return new self($context);
}
/* Optionally implement for better static analysis */
public function make(array $context = []): array
{
return $this->resolve($context);
}
}
$User = UserFactory::factory([User::last_name => 'Doe'])
->setFirstName('Jane')
->make();
User::factory([User::last_name => 'Doe'])->make(); // Also works for this example
echo $User['first_name']; // 'Jane'
echo $User['last_name']; // 'Doe'
class UserFactory
{
use \Zerotoprod\Factory\Factory;
protected function definition(): array
{
return [
'first_name' => 'John',
'last_name' => 'N/A'
];
}
public function setLastName(): self
{
return $this->state(function ($context) {
// Set last_name to the same value as first_name
return ['last_name' => $context['first_name']];
});
}
}
$User = UserFactory::factory()
->setLastName()
->make();
echo $User['first_name']; // 'John'
echo $User['last_name']; // 'John' (copied from first_name)
->set('first_name', 'John')
->set(User::first_name, 'John') // Using constants