\Phabstractic\PatternsBuilder

Builder Class - Creates and Defines OO-defined Builder Interfaces/Objects

The Builder class which when instantiated can define a builder interface and builder abstract class that can then be 'baked' into the appropriate namespace (or global namespace) and accessed, once baked, as a builder class to inherit from.

A static 'registry' makes sure that the programmer doesn't try to instantiate a fully qualified BuilderInterface twice, and throws an error.

CHANGELOG

1.0: created Builder - August 6th, 2016

Summary

Methods
Properties
Constants
configure()
saveSettings()
getSettings()
processSettings()
__construct()
bake()
setBuilderName()
getBuilderName()
isBaked()
setMethods()
getMethods()
addMethod()
addMethods()
removeMethod()
setNamespace()
getNamespace()
__debugInfo()
buildBuilder()
No public properties found
No constants found
No protected methods found
$conf
N/A
createBuilder()
getFQN()
setUpBuilders()
$builders
$builderName
$baked
$methods
$namespace
N/A

Properties

$conf

$conf : \Phabstractic\Features\Zend\Config\Config

The objects configuration object

Should be an instance of Zend/Config

Type

\Phabstractic\Features\Zend\Config\Config — The object configuration info

$builders

$builders : \Phabstractic\Patterns\Phabstractic\Data\Types\Set

Keeps track of already defined Builder interfaces/classes internally

Type

\Phabstractic\Patterns\Phabstractic\Data\Types\Set

$builderName

$builderName : string

The name of the builder interface/class to be generated

Type

string

$baked

$baked : boolean

Whether the class has defined itself

Type

boolean

$methods

$methods : \Phabstractic\Patterns\Phabstractic\Data\Types\Set

The abstract methods (turned into-> setMethodName)

Type

\Phabstractic\Patterns\Phabstractic\Data\Types\Set

$namespace

$namespace : string

The namespace under which the builder interface/class is to be defined

Type

string

Methods

configure()

configure(array|string|\Phabstractic\Features\Zend\Config\Config  $configuration, string  $format = null, mixed  $context = null) : boolean

Configure An Object

Expects an array for configuration however, you can also pass it a filepath where it will read the information from a file automatically detecting the format using the file extension. You can also pass a Zend/Config object already made.

You can also pass a format specifier (forced format) for use in a if $configuration is a string formatted with such information. E.G. to load from a string in the format ini:

$this->configure($configString, 'ini');

The $context argument is used for any additional reader constructor information, such as the constructor for the 'yaml' format.

NOTE: You can override/extend the classes used for reading formats by identifying an additional array in the property $this->configReaders. This will merge with the standard formats array.

Parameters

array|string|\Phabstractic\Features\Zend\Config\Config $configuration

The objects configuration information.

string $format

The forced format, or format for configuration string

mixed $context

Any additional information for a reader constructor, such as needed for the YAML format.

Returns

boolean —

True if instantiated

saveSettings()

saveSettings(string  $file, \Phabstractic\Features\Zend\Config\Writer\WriterInterface  $writer = null, boolean  $exclusive = true, mixed  $context = null) 

Save an Object's Configuration to a File

Takes an objects $conf property and writes the information contained therein to a file with a format automatically specified by the filename.

It is possible to retrieve a string of a particular format from this method by specifying the filename '#string' with an extension indicating the desired format, such as '#string.json'.

The $context argument is used for any additional reader constructor information, such as the constructor for the 'yaml' format.

NOTE: You can override/extend the classes used for writing formats by identifying an additional array in the property $this->configWriters. This will merge with the standard formats array.

Parameters

string $file

The file path to write to, or '#string.ext'

\Phabstractic\Features\Zend\Config\Writer\WriterInterface $writer

The optional writer object supplied to use (such as a MySQL writer)

boolean $exclusive

Argument provided to toFile(), file exclusive lock when writing

mixed $context

Any additionla writer constructor information (YAML)

getSettings()

getSettings(string  $format,   $context = null) : string|boolean

Retrieve an Object's Configuration Information As String

This is a shortcut to ::saveSettings() which specifies a format and forces the return of a string, using the #string.ext filename -see documentation for ::saveSettings()-

Parameters

string $format

The format to return, must be supported by ::saveSettings(), use $this->configWriters to support additional formats.

$context

Returns

string|boolean —

The formatted string, or false otherwise

processSettings()

processSettings(\Phabstractic\Features\Zend\Config\Processor\ProcessorInterface  $processor) 

Process an Object's Configuration

This uses a Zend\Config\Processor implementation to process the configuration information, such as constants. The processor must be supplied and implement ProcessorInterface

NOTE: Edits the $conf object in place.

Parameters

\Phabstractic\Features\Zend\Config\Processor\ProcessorInterface $processor

The given processor object

__construct()

__construct(string  $builderName, array  $methods = array(), array  $options = array()) 

Builder construction, Set Up builder parameters

This sets up the parameters for the builder to be generated It only generates the class when the option 'bake' is set to true

Options: strict => throw errors bake => define the classes immediately with the given parameters namespace => define the namespace

Parameters

string $builderName

The name of the factory class, turned into {BuilderName}BuildableInterface, Abstract{BuilderName}Factory, {BuilderName}Builder

array $methods

The methods of the class, turned into makeMethod

array $options

See above options

bake()

bake() : boolean

Generates the abstract factory class in the desired namespace

Generates the abstract factory class in the desired namespace using the given parameters, in essence solidifying them in place. Once an abstract factory class is 'baked' it cannot be changed.

This function also pushes the qualified name onto a static set so that future abstract factories defined through this method don't clash in name, allowing the class ro raise a RangeException

Returns

boolean —

Success or failure?

setBuilderName()

setBuilderName(  $builderName) 

Sets the name of the class to be generated

Checks to make sure the proposed fully qualified name doesn't clash with an already created enumerator object, raising a RangeException error

Remember, factoryName becomes Abstract{FactoryName}Factory

Parameters

$builderName

Throws

\Phabstractic\Patterns\Phabstractic\Patterns\Exception\RangeException

when the qualified name clashes with an already created enumerator class

\Phabstractic\Patterns\Phabstractic\Patterns\Exception\RuntimeException

when the class has already been baked

getBuilderName()

getBuilderName() : string

Retrieve factory name

Returns

string —

The factory name of the typed object

isBaked()

isBaked() : boolean

Check to see if class has been defined or 'baked'

Returns

boolean —

Whether the abstract factory has been baked.

setMethods()

setMethods(array  $methods) 

Sets the abstract factory methods

Remember, method names are transformed into makeMethod

Parameters

array $methods

An array containing the method names

Throws

\Phabstractic\Patterns\Phabstractic\Patterns\Exception\RuntimeException

when the class has already been generated (baked)

getMethods()

getMethods() : array

Gets the abstract factory methods

Returns

array —

Array of abstract factory methods

addMethod()

addMethod(string  $method) 

Add a single method to the method list

This method overrides any method names already defined with the new proposed values

Remember: method becomes makeMethod

Parameters

string $method

The name of the method

Throws

\Phabstractic\Patterns\Phabstractic\Patterns\Exception\RuntimeException

if the class has already been baked

addMethods()

addMethods(array  $methods) 

Add multiple methods to the method list

This method employs the array structure identifier => value. Overrides any method names already defined with the new proposed values

Parameters

array $methods

The array of methods to add to the list

Throws

\Phabstractic\Patterns\Phabstractic\Patterns\Exception\RuntimeException

if the class has already been baked

removeMethod()

removeMethod(string  $method) 

Remove a method from the method list

This method removes a method from the method list only if the class has not been generated, otherwise it throws an error

Remember: NOT makeMethod, just method

Parameters

string $method

Name of the method

Throws

\Phabstractic\Patterns\Phabstractic\Patterns\Exception\RuntimeException

if the class has already been baked

setNamespace()

setNamespace(string  $namespace) 

Sets up the namespace to be used when generating the abstract factory class

This sets the namespace that will be used when the enumerator class is generated How? Eval statements operate in the global namespace, or basically a clean slate enabling us to put a namespace statement at the beginning of our generated code Any namespace will do, if there is no namespace, you must access your abstract factory using the global namespace. ex: \Months::January, \Colors::Red, ...

Parameters

string $namespace

The namespace specified

Throws

\Phabstractic\Patterns\Phabstractic\Patterns\Exception\RangeException

when the class/namespace has already been generated via Enum

\Phabstractic\Patterns\Phabstractic\Patterns\Exception\RuntimeException

if the class has already been generated/baked.

getNamespace()

getNamespace() : string

Retrieves the namespace for the generated abstract factory

Returns

string —

The namespace for the generated abstract factory

__debugInfo()

__debugInfo() 

Debug Info (var_dump)

Display debug info

Requires PHP 5.6+

buildBuilder()

buildBuilder(  $builderName, array  $methods, array  $options = array()) 

Straight out define the abstract factory class without instantiating

Takes care of the instantiation and defines the abstract factory according to parameters

Parameters

$builderName
array $methods

The methods to be defined in the factory

array $options

See (__construct)

createBuilder()

createBuilder() : boolean

Generates the required class code and evaluates it

The function that pieces the generated code together and evaluates it

Evaluations: $this->builderName The name of the generated interface/classes (is turned into BuilderNameBuildableInterface, and AbstractBuilderNameBuildable, and BuilderNameBuilder) $this->methods The methods to put in the interface/classes (are turned into setMethod)

Throws

\Phabstractic\Patterns\Phabstractic\Patterns\Exception\CodeGenerationException

if class couldn't be generated

Returns

boolean —

if interfaces and classes are generated without errors

getFQN()

getFQN(  $builderName = '',   $namespace = '') : string

Construct FQN for Factory based on properties

Parameters

$builderName
$namespace

Returns

string

setUpBuilders()

setUpBuilders() 

Make sure self::factories exists