dkplus/formica
Composer 安装命令:
composer require dkplus/formica
包简介
Formica allows to easily use the builder pattern in your projects e.g. to setup your test data.
README 文档
README
Formica allows to easily use the builder pattern in your projects e.g. to setup your test data.
Installation
Formica can be easily installed by running composer require dkplus/formica.
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.
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:
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:
$issue = IssueBuilder::aBug()->withTitle('Another title')->build();
Explanations
The Dkplus\Formica\Builder expects three arguments:
- 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
nullis given it will use the constructor of the object. - The class of the object. If
nullis 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>Builderyou 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.
All arguments that have been given with a key can be overriden by one of
withX() or andX() methods (both providing a fluent interface).
If you want some autocompletion I suggest to put some annotations on top of your builder (see the example above).
统计信息
- 总下载量: 116
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 0
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2016-09-21