1. Go to this page and download the library: Download butschster/entity-faker 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/ */
butschster / entity-faker example snippets
use Butschster\EntityFaker\LaminasEntityFactory;
use Laminas\Hydrator\ReflectionHydrator;
use Faker\Factory as Faker;
$factory = new \Butschster\EntityFaker\Factory(
new LaminasEntityFactory(
new ReflectionHydrator()
),
Faker::create()
);
class User
{
private string $id;
private string $username;
private string $email;
public function __construct(string $id, string $username, string $email)
{
$this->id = $id;
$this->username = $username;
$this->email = $email;
}
}
class SuperUser extends User
{
private bool $isAdmin = false;
public function __construct(string $id, string $username, string $email, bool $isAdmin)
{
parent::__construct($id, $username, $email);
$this->isAdmin = $isAdmin;
}
}
$factory->define(User::class, function (Faker $faker, array $attributes) {
return [
'id' => $faker->uuid,
'username' => $faker->username,
'email' => $faker->email
];
});
$factory->define(SuperUser::class, function (Faker $faker, array $attributes) use($factory) {
$userAttributes = $factory->raw(User::class);
return $userAttributes + [
'isAdmin' => $faker->boolean
];
});
use Butschster\EntityFaker\EntityFactoryInterface;
use Faker\Factory as Faker;
use Cycle\ORM\ORMInterface;
use Cycle\ORM\TransactionInterface;
class CycleOrmEntityFactory implements EntityFactoryInterface
{
private array $afterCreation = [];
private array $beforeCreation = [];
protected ORMInterface $orm;
protected Transaction $transaction;
public function __construct(ORMInterface $orm)
{
$this->orm = $orm;
$this->beforeCreation(function () {
$this->transaction = new Transaction($this->orm);
});
$this->afterCreation(function () {
$this->transaction->run();
});
}
public function store(object $entity): void
{
$this->transaction->persist($entity);
}
public function hydrate(object $entity, array $data): object
{
return $this->orm->getMapper($entity)->hydrate($entity, $data);
}
/**
* Add a callback to run after creating an entity or array of entities.
* @param callable $callback
*/
public function afterCreation(callable $callback): void
{
$this->afterCreation[] = $callback;
}
public function afterCreationCallbacks(): array
{
return $this->afterCreation;
}
/**
* Add a callback to run before creating an entity or array of entities.
* @param callable $callback
*/
public function beforeCreation(callable $callback): void
{
$this->beforeCreation[] = $callback;
}
public function beforeCreationCallbacks(): array
{
return $this->beforeCreation;
}
}
$factory = new \Butschster\EntityFaker\Factory(
new CycleOrmEntityFactory(...),
Faker::create()
);
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.