\Phabstractic\Data\TypesStructure

Structure class - Defines a structure with specific fields

Some objects need modifiable and runtime specific member variables that can also be accessed with generic functions

Some fields may need to be restricted to specific types Coding for such fields can be simplified by using tagged unions

CHANGELOG

1.0: Created Structure - July 18th, 2013 1.1: Updated Documentation - October 7th, 2013 2.0: Updated file for Primus integration - August 25th, 2015 3.0: pass fields as keys and initial values as values of the array reformatted for inclusion in phabstractic - July 20th, 2016 3.0.1: implements configurationinterface - July 31st, 2016

Summary

Methods
Properties
Constants
configure()
saveSettings()
getSettings()
processSettings()
__construct()
getElements()
offsetSet()
offsetGet()
offsetUnset()
offsetExists()
getElementType()
getElementRestrictions()
getElement()
setElement()
clear()
toArray()
arrayReference()
No public properties found
No constants found
No protected methods found
$conf
N/A
denormalize()
elementExists()
$fields
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

$fields

$fields : array

The associative array making up the member variables

Key = The identifier for the specific field Value = The value for the specific field, may be a restricted tagged union

Type

array

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(array  $allowed = array(), array  $options = array()) 

Defines a Structure

This is where you specify the keys of the given fields as well as if any fields are restricted (tagged unions). Passing in a Restricitons object with tag (array) as an element of the array is all that is needed to initialize a tagged union item.

Ex: array('field1', 'field2', array($restrictions, 'field3'));

Options: strict - throw errors where applicable insensitive - make the keys (fields) insensitive (only for construction) Only one key of a particular value can then exist version2 - construct the fields using the algorithm in v2 false by default

Parameters

array $allowed

The field names as values, arrays for tagged unions and restrictions

array $options

The options for the object

getElements()

getElements() : array

Returns the name of the fields allowed in the structure

Returns

array —

The names of the fields allowed in the structure

offsetSet()

offsetSet(integer  $key, mixed  $value) : \Phabstractic\Data\Types\intNew

Set the offset in the list to the provided value

Parameters

integer $key

The index to the list item

mixed $value

The value to set to

Throws

\Phabstractic\Data\Types\Phabstractic\Data\Types\Exception\RangeException

if the value is not a stack index

Returns

\Phabstractic\Data\Types\intNew —

number of list elements

offsetGet()

offsetGet(integer  $key) : mixed

Retrieve the value in the list at the provided index

Parameters

integer $key

The index to the list item

Throws

\Phabstractic\Data\Types\Phabstractic\Data\Types\Exception\RangeException

if the value is not a stack index

Returns

mixed —

The value at the list index

offsetUnset()

offsetUnset(integer  $key) : boolean

Unset the index and value on the list

This isn't applicable in a structure so it is disabled

Note: Like the unset method, this throws no error if the index doesn't exist.

Parameters

integer $key

The index to the list item

Returns

boolean —

False if the index is improper, or not numeric, true otherwise

offsetExists()

offsetExists(integer  $key) : boolean

Does the given key exist in the list?

Note: This method also returns false if the key is out of range

Parameters

integer $key

The index into the stack

Returns

boolean —

Existing?

getElementType()

getElementType(  $element) : \Phabstractic\Data\Types\Phabstractic\Data\Types\Type|null

Returns the type being held in the structure field

Parameters

$element

Returns

\Phabstractic\Data\Types\Phabstractic\Data\Types\Type|null —

The type of value held

getElementRestrictions()

getElementRestrictions(string  $element) : null|\Phabstractic\Data\Types\Falcraft\Data\Types\Null|\Phabstractic\Data\Types\Falcraft\Data\Types\Restrictions

Returns the restrictions held by a given tagged union in the structure

Returns None() if there are no restrictions for that particular field but returns plain null if the key is not in the fields and strict is not enabled

Parameters

string $element

the key for the particular field

Returns

null|\Phabstractic\Data\Types\Falcraft\Data\Types\Null|\Phabstractic\Data\Types\Falcraft\Data\Types\Restrictions —

The restrictions

getElement()

getElement(string  $element) : mixed|\Phabstractic\Data\Types\Falcraft\Data\Types\Null

Retrieve the value of a given element

Wraps tagged union functionality if need be

Parameters

string $element

The key/field name of the value

Returns

mixed|\Phabstractic\Data\Types\Falcraft\Data\Types\Null —

Null() if key not present

setElement()

setElement(string  $element, mixed  $value) : boolean

Set a given field to a given value

Wraps tagged union functionality

Parameters

string $element

The field name

mixed $value

The value to set the field to

Throws

\Phabstractic\Data\Types\Phabstractic\Data\Types\Exception\InvalidArgumentException

If key doesn't exist in fields

Returns

boolean —

True on success

clear()

clear() 

Define all fields of the structure as null

Note: This is why making Type::BASIC_NULL a settable state on TaggedUnion fields is a must unless you never clear

toArray()

toArray() : array

Return internal array

Returns

array

arrayReference()

arrayReference() : \Phabstractic\Data\Types\&array

Return internal array reference

Returns

\Phabstractic\Data\Types\&array

denormalize()

denormalize(string  $field) : string

This function compares field names in upper case and then uses the defined field name

NOTE: This is only called if version2 is enabled

Parameters

string $field

Returns

string —

$key The actual defined field name (case-sensitive)

elementExists()

elementExists(  $element) : boolean

Checks to see if element identifier is present in structure

Parameters

$element

Throws

\Phabstractic\Data\Types\Phabstractic\Data\Types\Exception\InvalidArgumentException

if strict is enabled and the leement is not present

Returns

boolean