1. Go to this page and download the library: Download directorytree/dummy 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/ */
directorytree / dummy example snippets
namespace App\Data;
class Reservation
{
/**
* Create a new reservation instance.
*/
public function __construct(
public string $name,
public string $email,
public DateTime $date,
) {}
}
namespace App\Data;
use DateTime;
use Faker\Generator;
use DirectoryTree\Dummy\HasFactory;
class Reservation
{
use HasFactory;
/**
* Create a new reservation instance.
*/
public function __construct(
public string $name,
public string $email,
public DateTime $date,
) {}
/**
* Define the factory's default state.
*/
protected function getFactoryDefinition(Generator $faker): array
{
return [
'name' => $faker->name(),
'email' => $faker->email(),
'datetime' => $faker->dateTime(),
];
}
/**
* Create a new instance of the class using the factory definition.
*/
protected static function toFactoryInstance(array $attributes): static
{
return new static(
$attributes['name'],
$attributes['email'],
$attributes['datetime'],
);
}
}
$factory = Reservation::factory();
namespace App\Factories;
use App\Data\Reservation;
use DirectoryTree\Dummy\Factory;
class ReservationFactory extends Factory
{
/**
* Define the factory's default state.
*/
protected function definition(): array
{
return [
'name' => $this->faker->name(),
'email' => $this->faker->email(),
'datetime' => $this->faker->dateTime(),
];
}
/**
* Generate a new instance of the class.
*/
protected function generate(array $attributes): Reservation
{
return new Reservation(
$attributes['name'],
$attributes['email'],
$attributes['datetime'],
);
}
}
// Using the trait:
$reservation = Reservation::factory()->make();
// Using the factory class:
$reservation = ReservationFactory::new()->make();
class ReservationFactory extends Factory
{
// ...
/**
* Indicate that the reservation is for tomorrow.
*/
public function tomorrow(): Factory
{
return $this->state(function (array $attributes) {
return ['datetime' => new DateTime('tomorrow')];
});
}
}
class ReservationFactory extends Factory
{
// ...
/**
* Configure the dummy factory.
*/
protected function configure(): static
{
return $this->afterMaking(function (Reservation $reservation) {
// ...
});
}
}
Reservation::factory()
->count(3)
->sequence(
['datetime' => new Datetime('tomorrow')],
['datetime' => new Datetime('next week')],
['datetime' => new Datetime('next month')],
)
->make();
class ReservationFactory extends Factory
{
// ...
/**
* Create a new collection of classes.
*/
public function collect(array $instances = []): ReservationCollection
{
return new ReservationCollection($instances);
}
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.