定制 runopencode/abstract-builder 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

runopencode/abstract-builder

Composer 安装命令:

composer require runopencode/abstract-builder

包简介

Simple prototype class for implementing builder pattern.

README 文档

README

Packagist Scrutinizer Code Quality Code Coverage Build Status Build Status

SensioLabsInsight

If you intend to use builder pattern in your project (see article on Wikipedia) you can use a prototype implementation of builder pattern from this library.

In order to do so, you have to create your own builder class, extend RunOpenCode\AbstractBuilder\AbstractBuilder and implement methods configureParameters and getObjectFqcn.

For those extra lazy developers, there is a RunOpenCode\AbstractBuilder\ReflectiveAbstractBuilder as well, which implements configureParameters method by introspecting constructor parameters and default values.

NOTE: Builder class MUST NOT validate provided parameters. Class which ought to be builded must do such validation, because construction process of any instance of any class must be valid whether is executed with or without builder implementation.

Implementation example

Let's say that we have some class Message for which we have to provide a builder class. Here is how to do so:

<?php

class Message
{
    private $id;
    private $message;
    private $timestamp;

    public function __construct($id, $message, $timestamp)
    {
        $this->id = $id;
        $this->message = $message;
        $this->timestamp = $timestamp;
    }
}

final class MessageBuilder extends \RunOpenCode\AbstractBuilder\AbstractBuilder
{
    /**
     * Get builded message.
     *
     * Alias to \RunOpenCode\AbstractBuilder\AbstractBuilder::build() method.
     *
     * @see \RunOpenCode\AbstractBuilder\AbstractBuilder::build()
     *
     * @return object
     */
    public function getMessage()
    {
        return $this->build();
    }

    /**
     * {@inheritdoc}
     */
    protected function configureParameters()
    {
        return array(
            'id' => null,
            'message' => null,
            'timestamp' => new \DateTime('now')
        );
    }

    /**
     * {@inheritdoc}
     */
    protected function getObjectFqcn()
    {
        return Message::class;
    }
}

Builder usage example

Start with creating a builder, you can use plain constructor:

$builder = new MessageBuilder();

or you can use a static method for that:

$builder = MessageBuilder::create();

You can get/set each individual configured builder property via:

Property access:

$builder->id = 1;
$id = $builder->id;

Setter method:

$builder->setId(1);
$id = $builder->getId();

Array access:

$builder['id'] = 1;
$id = $builder['id'];

Multiple properties, via array:

$builder->fromArray([ 'id' => 1, 'message' => 'Some message' ]);
$allProperties = $builder->toArray();
$someProperties = $builder->toArray([ 'id', 'message' ]);

And finaly, you can build conrete object by calling build() method, or invoking builder as method:

$message = $builder->build();
$message = $builder();

Chaining (fluent interface)

Fluent interface is supported for setter methods, so you can chain them, example:

$message = MessageBuilder::create()
                ->setId(1)
                ->setMessage('Some message')
                ->setTimestamp(new \DateTime('now'))
                ->build();

统计信息

  • 总下载量: 215
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 3
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2016-11-20

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固