Library
Class

Library\Factory

class Factory extends AbstractStaticCreator implements FactoryInterface

Factory will try to create an object following user rules and passing it arguments

Usage

$factory = \Library\Factory::create()
    // all methods are optional
    ->factoryName('A Name To Identify Error Message')
    ->mustImplement('RequiredInterface')
    ->mustImplementAll(array('RequiredInterface1', 'RequiredInterface2'))
    ->mustExtend('RequiredInheritance')
    ->mustImplementOrExtend(array('RequiredInheritance', 'OR', 'RequiredInterface'))
    ->defaultNamespace('\Possible\Namespace')
    ->mandatoryNamespace('\Required\Namespace')
    ->classNameMask(array('%s', '%s_Suffix'))
    ->callMethod('load')
    ;

You can also define all options as an array to the creator:

$factory = \Library\Factory::create(array(
    // all options are optional
    'factory_name' => 'A Name To Identify Error Message',
    'must_implement' => 'RequiredInterface',
    'must_implement_all' => array('RequiredInterface1', 'RequiredInterface2'),
    'must_extend' => 'RequiredInheritance',
    'must_implement_or_extend' => array('RequiredInheritance', 'OR', 'RequiredInterface'),
    'default_namespace' => '\Possible\Namespace',
    'mandatory_namespace' => '\Required\Namespace',
    'class_name_mask' => array('%s', '%s_Suffix'),
    'call_method' => 'load',
));

Then, to try to build the object:

$object = $factory->build($name, $params);

Errors are thrown by default. You can avoid this using the GRACEFULLY_FAILURE constant flag. All error messages are loaded in the $logs last parameter of the build() and findBuilder() methods.

Method parameters

When the object creation method is called, the parameters passed to the Factory::build() method are re-organized before to pass them to the method. This way, you can define the parameters as an array using explicit indexes corresponding to the parameters names in method declaration without working about their order.

Specific method to build the instance

In the case of a specific $call_method (not the classic __construct), the builder will try to first construct the object except if the call method is static. If it is not static, any existing constructor will be first called without parameters and then the defined call method passing it the parameters.

Methods

void init(array $options = null)

Initialize the factory with an array of options

Factory __call(string $name, array $arguments)

Magic method to allow usage of $factory->propertyInCamelCase() for each class property

Factory setOptions(array $options)

Set the object options like property => value

object build(string $name, array $parameters = null, int $flag = self::ERROR_ON_FAILURE, array $logs = array())

Build the object instance following current factory settings

null|string findBuilder(string $name, int $flag = self::ERROR_ON_FAILURE, array $logs = array())

Find the object builder class following current factory settings

Details

at line 202
public void init(array $options = null)

Initialize the factory with an array of options

The options must be defined like property => value

Parameters

array $options

Return Value

void

at line 216
public Factory __call(string $name, array $arguments)

Magic method to allow usage of $factory->propertyInCamelCase() for each class property

Parameters

string $name
array $arguments

Return Value

Factory

at line 236
public Factory setOptions(array $options)

Set the object options like property => value

Parameters

array $options

Return Value

Factory

at line 261
public object build(string $name, array $parameters = null, int $flag = self::ERROR_ON_FAILURE, array $logs = array())

Build the object instance following current factory settings

Errors are thrown by default but can be "gracefully" skipped using the flag GRACEFULLY_FAILURE. In all cases, error messages are loaded in final parameter $logs passed by reference.

Parameters

string $name
array $parameters
int $flag One of the class constants flags
array $logs Passed by reference

Return Value

object

Exceptions

RuntimeException if the class is not found
RuntimeException if the class doesn't implement or extend some required dependencies
RuntimeException if the class method for construction is not callable

at line 350
public null|string findBuilder(string $name, int $flag = self::ERROR_ON_FAILURE, array $logs = array())

Find the object builder class following current factory settings

Errors are thrown by default but can be "gracefully" skipped using the flag GRACEFULLY_FAILURE. In all cases, error messages are loaded in final parameter $logs passed by reference.

Parameters

string $name
int $flag One of the class constants flags
array $logs Passed by reference

Return Value

null|string

Exceptions

RuntimeException if the class is not found
RuntimeException if the class doesn't implement or extend some required dependencies
RuntimeException if the class method for construction is not callable