\Phabstractic\Data\TypesSet

Set Class - Defines A Set Data Structure

A set is a collection of values that are unique. In mathematical terms it is an implementation of a finite set. Usually you test if something exists or doesn't exist inside the set. You can also set up operations on sets themselves, much like an array map. You can also easily perform unions, intersections, and many operations traditional to set theory and the set data type.

CHANGELOG

1.0: Documented Set - March 5th, 2013 1.1: Added Closures to array set operations - October 7th, 2013 2.0: Refactored and included into Primus/Falcraft - April 10th, 2015 3.0: reorganized namespaces edited returnUniqueByReference calls to actually use references set only unique if unique option is set now !!! reformatted for inclusion in phabstractic - July 10th, 2016 3.0.1: implements configurationinterface - July 13th, 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()
No public properties found
No constants found
getNewIdentity()
$conf
$identityCounter
$identityPrefix
$data
N/A
checkUniqueValues()
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

$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

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(), array|\Phabstractic\Data\Types\Zend\Config\Config|string  $options = array()) 

Set Construction

Sets the data member to the given array.

NOTE: A set can contain any type of data, even mixed data. This means that a set might contain integers, objects, and anything else storable in a variable all together simultaneously.

IMPORTANT: If keys are specified in the initial array then these keys are used as their identifications.

Options -

'unique' => bool, do we check the incoming data for uniqueness? If data is not unique an exception is raised. 'strict' => bool, do we raise exceptions when the set is used improperly, such as removing a value that doesn't exist? 'reference' => bool, do we add in constructor by reference? defaults to true

Parameters

array $values

The values of the set

array|\Phabstractic\Data\Types\Zend\Config\Config|string $options

See options above.

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

Add a value to the set, respects 'unique' option

Parameters

mixed $value

The value to add to the set.

Returns

string —

The internal datum identifier

addReference()

addReference(mixed  $value) : integer

Add a value to the set as a reference, respects 'unique' option

Parameters

mixed $value

The value to add to the set.

Returns

integer —

The new count of the array

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\Set

Create a set from an array

Just hands back a Set created from a given array "creates a set structure with values x1,x2,…,xn."

Parameters

array $values

The values of the set (which are 'uniqued' in the construction)

Returns

\Phabstractic\Data\Types\Set

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

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?