1. Go to this page and download the library: Download natitech/builder-generator 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/ */
//From /path/to/MyClass.php file = the built class
class MyClass {
public int $id;
public string $label;
}
//This new file /path/to/MyClassBuilder.php will be generated = the builder class
class MyClassBuilder {
private int $id;
private string $label;
public function __construct(Faker $faker)
{
$this->id = $faker->number();
$this->label = $faker->word;
}
public function build(): MyClass
{
$myClass = new MyClass();
$myClass->id = $this->id;
$myClass->label = $this->label;
return $myClass;
}
//this will have to be generated by your IDE
public function withLabel(string $label): self
{
$this->label = $label;
return $this;
}
}
//The ultimate goal is to use this in tests
/** @test */
public function canTestSomething()
{
$this->assertEquals(
'test',
$this->service->something($this->myClass()->withLabel('used in test')->build())
);
}
private function myClass(): MyClassBuilder
{
return new MyClassBuilder(Faker\Factory::create());
}
//Built class
class MyClass {
private int $id;
public function __construct(int $id, private string $label)
{
$this->id = $id;
}
}
//Builder class
class MyClassBuilder {
private int $id;
private string $label;
public function __construct(Faker $faker)
{
$this->id = $faker->number();
$this->label = $faker->word;
}
public function build(): MyClass
{
return new MyClass(
$this->id,
$this->label
);
}
}
//Built class
class MyClass {
protected int $id;
protected string $label;
public function setId(int $id)
{
$this->id = $id;
return $this;
}
public function setLabel(string $label)
{
$this->label = $label;
return $this;
}
}
//Builder class
class MyClassBuilder {
private int $id;
private string $label;
public function __construct(Faker $faker)
{
$this->id = $faker->number();
$this->label = $faker->word;
}
public function build(): MyClass
{
return (new MyClass())
->setId($this->id)
->setLabel($this->label);
}
}
//Built class
class MyClass {
private int $id;
private ?string $label = null;
public static function create(int $id): self
{
$myClass = new self();
$myClass->id = $id;
return $myClass;
}
public function updateLabel(string $label): self
{
$this->label = $label;
return $this;
}
//This method will have to be generated by you IDE from the builder class
//It allows you to hack the state of this object without relying on its public interface
public static function build(int $id, string $label): self
{
$myClass = new self();
$myClass->id = $this->id;
$myClass->label = $this->label;
return $myClass;
}
}
//Builder class
class MyClassBuilder {
private int $id;
private string $label;
public function __construct(Faker $faker)
{
$this->id = $faker->number();
$this->label = $faker->word;
}
public function build(): MyClass
{
return MyClass::build(
$this->id,
$this->label
);
}
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.