2017 © Pedro Peláez
 

library builder

PHP package for instantiating objects using configurations

image

nayjest/builder

PHP package for instantiating objects using configurations

  • Thursday, February 11, 2016
  • by nayjest
  • Repository
  • 2 Watchers
  • 12 Stars
  • 57,973 Installations
  • PHP
  • 3 Dependents
  • 0 Suggesters
  • 0 Forks
  • 4 Open issues
  • 8 Versions
  • 8 % Grown

The README.md

Builder

PHP package for constructing objects using configurations., (*1)

Codacy Badge Code Climate Scrutinizer Code Quality Code Coverage Build Status Latest Stable Version Dependency Status, (*2)

SensioLabsInsight, (*3)

1. Requirements

  • php 5.4+

This package is not tested with outdated versions of PHP., (*4)

If you need to use nayjest/builder in environments with php <= 5.3, test it on your own., (*5)

2. Installation

The recommended way of installing the component is through Composer., (*6)

Run following command:, (*7)

composer require nayjest/builder

3. Overview

3.1 Purpose

Building complex objects from configurations in architecturally beutiful way., (*8)

3.2 Explanation

If you are confident with builder design pattern, this package allows you to create builders for your classes in declarative style, based on build configuration., (*9)

3.3 Usage

3.3.1 Class Blueprints

When you need to construct objects of some type, first of all, you will create blueprint for that type. In terminology of this package blueprint specifies how to construct object of some specified type. It stores class name and set of build instructiuons., (*10)

use Nayjest\Builder\Blueprint;
$blueprint = new Blueprint(
  '\My\ExamplePackage\Person',  # Constructed class name
  []                            # Array of build instructions
);
3.3.2 Builders

Next, you will instantiate a builder with previously created blueprint as constructor argument. In terminology of this package, builder is a instance of Nayjest\Builder\Builder class, initialized with a blueprint of constructed class. This object has public build($input) method that accepts constructed class configurations, builds instance and returns it., (*11)

Builders are suitable for reusing. You can build multiple objects specified by same blueprint using same builder., (*12)

use Nayjest\Builder\Builder;

$builder = new Builder($blueprint);

$john = $builder->build([
  'name' => 'John',
  'age' => 27
]);

var_dump($john instanceof \My\ExamplePackage\Person); // result: true
var_dump($john->getAge());  // result: 27
3.3.3 Scaffold and Instructions

During taget object building, builder creates temporary object, called scaffold. Scaffold contains all information needed to build object (input config, class name, constructor arguments, target instance when it's ready)., (*13)

This class is importand since all build instructions works with scaffold, scaffold provides interface for accessing all required data to instructions., (*14)

And finally, instructions. In terminology of this package, instruction is an instance of class that implements Nayjest\Builder\Instructions\Base\InstructionInterface and can modify data inside scaffold., (*15)

There is a set of predefined instructions, but you can create your own (the dirty way is usage of Nayjest\Builder\Instructions\Base\Instruction\CustomInstruction initialized by user function that will perform operations you want)., (*16)

Also, instantiating target object and setting public properties or properties that has setters with corresponding names in camel case (i.e. setSomeAttribute($val) for 'some_attribute' input field) does not requires specific instruction in class blueprint, it's operations, performed by default., (*17)

4. Build Instructions

Nayjest\Builder\Instructions\CustomInstruction

Applies user function to scaffold., (*18)

Nayjest\Builder\Instructions\SetValue

Can be used to specify default values in build configuration or overwrite existing., (*19)

Nayjest\Builder\Instructions\Remove

Removes value from input configuration if exists., (*20)

Nayjest\Builder\Instructions\Mapping\Build

Replaces value by object builded using specified class blueprint., (*21)

Nayjest\Builder\Instructions\Mapping\BuildChildren

Replaces array elements inside target field by objects builded using specified class blueprint., (*22)

Nayjest\Builder\Instructions\Mapping\ClassName

Uses value in specified field as target class name., (*23)

Nayjest\Builder\Instructions\Mapping\ConstructorArgument

Uses value in specified field as constructor argument., (*24)

Nayjest\Builder\Instructions\Mapping\CustomMapping

Applies user function to specified field., (*25)

Nayjest\Builder\Instructions\Mapping\Rename

Renames specified field inside input configuration., (*26)

5. Testing

Run following command:, (*27)

phpunit

6. License

© 2014 — 2015 Vitalii Stepanenko, (*28)

Licensed under the MIT License., (*29)

Please see License File for more information., (*30)

The Versions

11/02 2016

dev-master

9999999-dev

PHP package for instantiating objects using configurations

  Sources   Download

MIT

The Requires

 

The Development Requires

by Vitalii Stepanenko

11/02 2016

v2.1.0

2.1.0.0

PHP package for instantiating objects using configurations

  Sources   Download

MIT

The Requires

 

The Development Requires

by Vitalii Stepanenko

04/06 2015

v2.0.0

2.0.0.0

PHP package for instantiating objects using configurations

  Sources   Download

MIT

The Requires

 

The Development Requires

by Vitalii Stepanenko

25/03 2015

v1.1.3

1.1.3.0

PHP package for instantiating objects using configurations

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

by Vitalii Stepanenko

05/03 2015

v1.1.2

1.1.2.0

PHP package for instantiating objects using configurations

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

by Vitalii Stepanenko

04/03 2015

v1.1.0

1.1.0.0

PHP package for instantiating objects using configurations

  Sources   Download

The MIT License (MIT)

The Requires

  • php >=5.4.0

 

The Development Requires

by Vitalii Stepanenko

02/03 2015

1.0

1.0.0.0

PHP package for instantiating objects using configurations

  Sources   Download

The MIT License (MIT)

The Requires

  • php >=5.4.0

 

The Development Requires

by Vitalii Stepanenko

19/02 2015

0.1

0.1.0.0

PHP package for instantiating objects using configurations

  Sources   Download

The MIT License (MIT)

The Requires

  • php >=5.4.0

 

The Development Requires

by Vitalii Stepanenko