, (*1)
Formica allows to easily use the builder pattern in your projects e.g. to setup your test data., (*2)
Installation
Formica can be easily installed by running composer require dkplus/formica
., (*3)
Quick start
Lets assume that you have a class Acme\Issue
for which you want to have a builder for.
It has some properties and can be constructed through a named constructor., (*4)
namespace Acme;
class Issue
{
// …
public static function bug(string $id, string $title, string $text): self
{
// …
}
// …
}
For a builder you need another class Acme\IssueBuilder
which extends the base builder:, (*5)
namespace Acme;
use Dkplus\Formica\Builder;
/**
* @method IssueBuilder withTitle(string $title)
* @method IssueBuilder withText(string $text)
*/
class IssueBuilder extends Builder
{
public static function aBug()
{
return new self([
'74738ff5-5367-5958-9aee-98fffdcd1876',
'title' => 'It does not work',
'text' => 'A long error text',
], 'bug');
}
}
You can use the builder like this:, (*6)
$issue = IssueBuilder::aBug()->withTitle('Another title')->build();
Explanations
The Dkplus\Formica\Builder
expects three arguments:, (*7)
- The default arguments that should be passed to the constructing method.
- The name of the static method that should be used to construct the object.
If
null
is given it will use the constructor of the object.
- The class of the object. If
null
is given it will try to guess the class
by using the class of the builder and stripping the last 7 characters.
So if you name your builders <NameOfYourClass>Builder
you don't need to
pass the class.
While the constructor of Dkplus\Formica\Builder
is public, I advise to create
a static method that calls it and sets the default values., (*8)
All arguments that have been given with a key can be overriden by one of
withX()
or andX()
methods (both providing a fluent interface)., (*9)
If you want some autocompletion I suggest to put some annotations on top of
your builder (see the example above)., (*10)