\Phabstractic\PatternsPrototype

Prototype Class - Defines a JavaScript Like Prototype

In order for a prototype to work in the hierarchy correctly, we must chain prototype objects together using __parent. If you don't use a base prototype object, you must use something with an interface that will be compatible. It is thus necessary to always use new Prototype(), ::fromRegistry($key), ::fromPrototype($prototype).

You can register prototypes into a 'global' registry using a strings. If you 'add' a registry entry with the same key, the old entry is overwritten.

CHANGELOG

1.0: Created Prototype - November, 23th, 2015 2.0: eliminated map for registry, made it swappable eliminated local static methods reformatted for inclusion in phabstractic - August 4th, 2016

Summary

Methods
Properties
Constants
configure()
saveSettings()
getSettings()
processSettings()
setRegistry()
getFromRegistry()
addToRegistry()
removeFromRegistry()
fromRegistry()
fromPrototype()
__construct()
getIdentifier()
__call()
__get()
__debugInfo()
No public properties found
No constants found
getNewIdentity()
$conf
$identityCounter
$identityPrefix
$registry
$identity
N/A
No private methods found
No private properties found
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

$identityCounter

$identityCounter : mixed

The Global Identity Generator Marker

When the identity is polled, this marker is 'advanced'. It's static to the class, in that way it's unique for each one.

Type

mixed — The current identity

$identityPrefix

$identityPrefix : mixed

The Identity Marker Prefix

Specific to a given class, this is placed with/in front of the identity (context) This is a bit strange to understand why it's not static, but imagine a finer grain of control

Type

mixed — The class's context/marker prefix

$registry

$registry : array

The 'global' prototype registry

Defined as an array of (key, indices)

Type

array

$identity

$identity : string

The unique identifier for this prototype

See Features\Identity

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

setRegistry()

setRegistry(\ArrayAccess  $newRegistry) 

Set the registry object

This enables us to swap out a map if we want it

Parameters

\ArrayAccess $newRegistry

The new registry

getFromRegistry()

getFromRegistry(mixed  $key) : \Phabstractic\Patterns\Falcraft\Patterns\Prototype

Retrieve a Prototype from the 'global' registry

Parameters

mixed $key

Returns

\Phabstractic\Patterns\Falcraft\Patterns\Prototype

addToRegistry()

addToRegistry(\Phabstractic\Patterns\Falcraft\Patterns\Prototype  $prototype, mixed  $key = '') : \Phabstractic\Patterns\result

Add a value to the map with a key that previously did not exist

Parameters

\Phabstractic\Patterns\Falcraft\Patterns\Prototype $prototype
mixed $key

Returns

\Phabstractic\Patterns\result —

of setting operation

removeFromRegistry()

removeFromRegistry(mixed  $key) : boolean

Remove prototype from 'global' registry using key

Parameters

mixed $key

Returns

boolean

fromRegistry()

fromRegistry(mixed  $key,   $options = array()) : \Phabstractic\Patterns\Falcraft\Patterns\Prototype

Create a Prototype whose __parent is a prototype from the registry

Parameters

mixed $key
$options

Returns

\Phabstractic\Patterns\Falcraft\Patterns\Prototype —

the new prototype

fromPrototype()

fromPrototype(  $parentPrototype,   $options = array()) : \Phabstractic\Patterns\Falcraft\Patterns\Prototype

Create a Prototype whose __parent is given prototype

Parameters

$parentPrototype
$options

Returns

\Phabstractic\Patterns\Falcraft\Patterns\Prototype —

the new prototype

__construct()

__construct(array  $options = array()) 

The Prototype Constructor

The constructor itself establishes the options that a particular prototype might have. If you wish to change a prototype's configuration call $prototype->configure($options) You can pass an array, or a a Zend Conf object.

The key/value pair is constructed according to the array elements data type.

Currently available options: strict => whether to output errors or remain silent

Parameters

array $options

The options for the Map as outlined above

getIdentifier()

getIdentifier() : string

Retrieve the identifier of this prototype

NOTE: There is no setIdentity, as the identity should be unique

Returns

string

__call()

__call(string  $name, array  $arguments) : mixed

Magical Call Function

This uses call_user_func_array to call the closures set in the object. It overrides the error handling temporarily so that it can catch warnings about non-existing properties. If it can't find a property, it looks up the __parent chain to see if it can resolve.

If it can't find anything, it reports an error.

It's important that at the end that it deregisters its handler for normal function

NOTE: This catches a specific RunTime Exception instance, and should pass all others... hopefully.

Parameters

string $name

of desired function

array $arguments

of desired function

Returns

mixed —

Result of user function

__get()

__get(  $var) : mixed

Magical Get Function

This one is simpler than the magical call function. This simply searches up the __parent chain looking for a property until it runs out of parents.

NOTE: This is useful because it will also return the function objects associated with a key (closures) if you want access to the closure itself.

Parameters

$var

name of variable

Returns

mixed

__debugInfo()

__debugInfo() 

Debug Info (var_dump)

Display debug info

Requires PHP 5.6+

getNewIdentity()

getNewIdentity() : mixed

Poll for New Identity

Overridable in a using class so that anything could be used as an identity, but by default it's an integer attached to a prefix. As new identities are polled, the integer increases.

Returns

mixed —

The new identity