\Phabstractic\Data\TypesRestrictedSet

Restricted Set Class - Defines A Restricted Set Data Structure

An instantiated Set is like an array, but only holds unique values without really any keys to the set itself. It's just a collection of unique values, like a pool of variables. This makes the Set only hold certain values, usually one type.

NOTE: You can only set the restrictions of this object in the constructor Which makes sense, what would you do half-way through if all of a sudden half your set doesn't qualify anymore? Seems like a mess.

CHANGELOG

1.0: Documented RestrictedSet - March 5th, 2013 1.1: Fixed Options initialization - May 16th, 2013 2.0: Refactored and re-formatted for inclusion in Primus - April 11th, 2015 3.0: added option filter_class to specify alternate static function call reformatted for inclusion in phabstractic - July 201th, 2016 3.0.1: implements configurationinterface - July 31st, 2016

Summary

Methods
Properties
Constants
configure()
saveSettings()
getSettings()
processSettings()
getArray()
getPlainArray()
getArrayReference()
__construct()
setIdentifierPrefix()
retrieveReference()
add()
addReference()
remove()
removeByIdentifier()
in()
isEmpty()
size()
iterate()
enumerate()
pop()
clear()
hash()
__debugInfo()
equal()
fold()
filter()
map()
walk()
build()
union()
intersection()
difference()
subset()
getRestrictions()
mapInternal()
No public properties found
No constants found
getNewIdentity()
$conf
$data
$identityCounter
$identityPrefix
N/A
checkUniqueValues()
$restrictions
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

$data

$data : array

Set Data

This is protected so that inheriting objects have access to the data

Type

array — The data of the set, implemented as an array

$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

$restrictions

$restrictions : \Phabstractic\Data\Types\Phabstractic\Data\Types\Resources\AbstractFilter

The restrictions predicate structure for the allowed set variables

Type

\Phabstractic\Data\Types\Phabstractic\Data\Types\Resources\AbstractFilter

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

getArray()

getArray() : array

Get the Set as Array

Return the set contents as an array

Returns

array —

The set represented as an array

getPlainArray()

getPlainArray() : array

Get the Set as a PLAIN Array

Return the set contents as an array without string keys

NOTE: All elements are references to internal array elements

Returns

array —

The set represented as a plain array

getArrayReference()

getArrayReference() : array

Get the Set's Array

Return the set's array by reference

Returns

array —

The set's internal array

__construct()

__construct(array  $values = array(), \Phabstractic\Data\Types\Phabstractic\Data\Types\Resource\AbstractFilter  $restrictions = null, array  $options = array()) 

RestrictedSet Class Constructor

This sets up the restrictions for the values, then provides the options to the parent set. This is the only place to set the restrictions for the set.

options - 'filter_class' => the class that the checkElements static function is called on

Parameters

array $values

The values for the RestrictedSet

\Phabstractic\Data\Types\Phabstractic\Data\Types\Resource\AbstractFilter $restrictions

The restrictions for the data type

array $options

The options for the parent set.

Throws

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

setIdentifierPrefix()

setIdentifierPrefix(  $prefix) 

Change Element Identifier Prefix

Parameters

$prefix

retrieveReference()

retrieveReference(mixed  $identifier) : mixed|\Phabstractic\Data\Types\Phabstractic\Data\Types\None;

If the value is in the set, this retrieves a reference to it

This only works if the set is considered unique

Parameters

mixed $identifier

The identifier returned by the add function

Throws

\Phabstractic\Data\Exception\RangeException

if strict

Returns

mixed|\Phabstractic\Data\Types\Phabstractic\Data\Types\None;

add()

add(mixed  $value) : string|false

Add a value to the set

Runs the value by the restrictions, only triggering if set is strict otherwise doing nothing and exiting the function

Parameters

mixed $value

Value to add to the set, must be of allowed type

Throws

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

if 'strict' option is set and type is not in Restrictions

Returns

string|false —

The identifier for the entry, false otherwise

addReference()

addReference(mixed  $value) : string|false

Add a value to the set as a reference

Runs the value by the restrictions, only triggering if set is strict otherwise doing nothing and exiting the function

Parameters

mixed $value

Value to add to the set, must be of allowed type

Throws

\Phabstractic\Data\Types\Exception\InvalidArgumentException

if 'strict' option is set and type is not in Restrictions

Returns

string|false —

The datum identifier, false otherwise

remove()

remove(mixed  $value) 

Remove a value from the set

Respects 'strict' option: the set must contain the value to be removed.

Parameters

mixed $value

The value to be removed from the set.

Throws

\Phabstractic\Data\Types\Exception\RangeException

if non-existent and strict is set

removeByIdentifier()

removeByIdentifier(  $identity) 

Remove a value from the set

This uses a previously given identifier to remove an element from the set. If the identifier doesn't exist it returns false.

Parameters

$identity

in()

in(mixed  $value) 

Basic in_array wrapper for the set

Parameters

mixed $value

The value to check against the data

isEmpty()

isEmpty() : boolean

Is the internal data array empty?

Returns

boolean

size()

size() : integer

Basic count wrapper for the set

Returns

integer —

Size of data array

iterate()

iterate() : \ArrayIterator

This allows us to use the Set data in a basic iterator Being, \ArrayIterator

Returns

\ArrayIterator —

The iterator to use in a loop

enumerate()

enumerate() : array

Synonym for getArray()

Returns

array —

The data PLAIN array of the set

pop()

pop() : mixed

Pop the first/next value defined the set

This deletes the value from the set

Returns

mixed —

The value 'popped' from the Set

clear()

clear() 

Eliminate Set data completely

hash()

hash() : string

Return a hash (unique) of the entire set

Derived from the Set data: "returns a hash value for the static set S such that if equal(S1, S2) then hash(S1) = hash(S2)"

Returns

string —

The hash of the set data

__debugInfo()

__debugInfo() 

Debug Info (var_dump)

Display debug info

Requires PHP 5.6+

fold()

fold(callable  $F, \Phabstractic\Data\Types\Set  $S) : mixed

Apply a function to each element of the set, reducing to a single function

"returns the value A|S| after applying Ai+1 := F(Ai, e) for each element e of S."

Parameters

callable $F

The applicable function.

\Phabstractic\Data\Types\Set $S

The Set to fold

Returns

mixed —

The value resulting from the 'fold' (array_reduce)

filter()

filter(callable  $F, \Phabstractic\Data\Types\Set  $S) : array

Returns only elements that satisfy a 'predicate'

The predicate here is a function that returns true or false on any given item "returns the subset containing all elements of S that satisfy a given predicate P."

Parameters

callable $F

The applicable predicate function.

\Phabstractic\Data\Types\Set $S

The Set to filter

Returns

array —

The filtered aray

map()

map(callable  $F, \Phabstractic\Data\Types\Set  $S) : array

Apply a function to each element of the set

"returns the set of distinct values resulting from applying function F to each element of S."

Parameters

callable $F

The applicable function.

\Phabstractic\Data\Types\Set $S

The Set to map

Returns

array —

The resulting distinct set

walk()

walk(\Phabstractic\Data\Types\Resource\SetInterface  $S, callable  $F, mixed  $D) : boolean

Walk (non-recursively) the array

Applies the given function, plus any additional arguments ($args) past the given function argument to each element of the set.

Note: Unlike Map, this operates on a reference of the set

Parameters

\Phabstractic\Data\Types\Resource\SetInterface $S
callable $F

the applicablt function

mixed $D

the user data

Returns

boolean —

True on Success, False otherwise

build()

build(array  $values = array(), \Phabstractic\Data\Types\Phabstractic\Data\Types\Resource\AbstractFilter  $restrictions = null, array  $options = array()) : \Phabstractic\Data\Types\Phabstractic\Data\Types\RestrictedSet

Generates a RestrictedSet from an array

Default restrictions are all but TYPED_OBJECT

NOTE: This method signature expands on the Set::build static function PHP allows you to do this with default arguments

Parameters

array $values

The values to put in the Restricted Set

\Phabstractic\Data\Types\Phabstractic\Data\Types\Resource\AbstractFilter $restrictions

The Restrictions to place on the generated set

array $options

The restricted set options (See constructor)

Returns

\Phabstractic\Data\Types\Phabstractic\Data\Types\RestrictedSet —

Generated RestrictedSet

union()

union() : array

Set Union

"returns the union of sets S and T."

This function can take multiple arrays, sets, or values mixed together

Returns

array —

Unioned array

intersection()

intersection() : array

Set Intersection

"returns the intersection of sets S and T."

This function can take multiple arrays, Sets, or values mixed together

Returns

array —

Intersected array

difference()

difference() : array

Set Difference

"returns the difference of sets S and T."

This function can take multiple arrays, Sets, or values mixed together

Returns

array —

Difference array

subset()

subset(\Phabstractic\Data\Types\Set  $S, \Phabstractic\Data\Types\Set  $T) : boolean

Subset

"a predicate that tests whether the set S is a subset of set T."

This function tests whether all elements of S are in T

Parameters

\Phabstractic\Data\Types\Set $S

The first set

\Phabstractic\Data\Types\Set $T

The comparison/parent set

Returns

boolean

getRestrictions()

getRestrictions() : \Phabstractic\Data\Types\Phabstractic\Data\Types\Restrictions

Retrieve the Restrictions object/structure from the set

Useful for Reflection purposes

Returns

\Phabstractic\Data\Types\Phabstractic\Data\Types\Restrictions

mapInternal()

mapInternal(string  $method, array  $args, \Phabstractic\Data\Types\Phabstractic\Data\Types\Set  $S) 

Calls an internal member function of a set member with arguments

Like map, only it calls a particular method on the given object, this is possible because the set can be guaranteed to hold only certain objects. !! Do not attempt on a mixed set. !!

Parameters

string $method

The method name to call

array $args

The arguments to pass to the method

\Phabstractic\Data\Types\Phabstractic\Data\Types\Set $S

The set to act on

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

checkUniqueValues()

checkUniqueValues(array  $values = array()) : boolean

Check an array for unique values

Useful for 'strict' option to warn of incoming duplicate entries

Parameters

array $values

The array to check

Throws

\Phabstractic\Data\Types\Exception\RangeException

When a value is not unique in the array if configured strict

Returns

boolean —

Successful?