\Phabstractic\Data\TypesRestrictions

Restrictions Class - Defines a predicate data structure that defines an allowed list of types

An instantiated Restrictions class acts like a big encapsulated if...else clause by defining a set of types and/or classes that can be allowed in a particular context. This is useful when defining TaggedUnions, which can take on a restricted number of data types, or a RestrictedSet which can (theoretically) act like a TaggedUnion but is usually worked as one particular type.

CHANGELOG

1.0: Documented Restrictions - March 5th, 2013 1.1: Added getDefaultRestriction static method - May 16th, 2013 1.2: Added checkRestrictedValues static method - May 16th, 2013 1.3: Added instanceof operator in allowed, allowing for polymorphism - May 16th, 2013 1.4: Renamed getDefaultRestriction(s) for clarity and clarified exceptions - October 7th, 2013 2.0: Refactored and reformatted for inclusion in Primus - April 11th, 2015 2.1: Implemented interface and inherited abstract for better decoupling - April 11th, 2015 2.2: Added comparison static function - September 5th, 2015 3.0: changed isAllowed to work with v3 of enumeration type added dynamic setting of allowed classes and types let $filter decide strictness in checkElements reformatted for inclusion in phabstractic - July 13th, 2016

Summary

Methods
Properties
Constants
configure()
saveSettings()
getSettings()
processSettings()
isAllowed()
checkElements()
getAllowedTypes()
getAllowedClasses()
setAllowedTypes()
addAllowedType()
removeAllowedType()
setAllowedClasses()
addAllowedClass()
removeAllowedClass()
__construct()
__debugInfo()
getDefaultRestrictions()
checkRestrictedValues()
compare()
No public properties found
No constants found
No protected methods found
$conf
N/A
isProperType()
classExists()
setBasicOrTyped()
$allowed
$classes
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

$allowed

$allowed : \Phabstractic\Data\Types\Phabstractic\Data\Types\Resource\SetInterface

The list of allowed data types

NOTE: An object of a particular type has the 'flag' Type::TYPED_OBJECT and the classes are defined in the member $classes set.

Type

\Phabstractic\Data\Types\Phabstractic\Data\Types\Resource\SetInterface

$classes

$classes : \Phabstractic\Data\Types\Phabstractic\Data\Types\Resource\SetInterface

The list of allowed classes and interfaces

When Type::TYPED_OBJECT is present in allowed member set then this set defines what classes the object is allowed to be.

Type

\Phabstractic\Data\Types\Phabstractic\Data\Types\Resource\SetInterface

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

isAllowed()

isAllowed(\Phabstractic\Data\Types\Phabstractic\Data\Types\Type|array|integer|string  $type, boolean  $strict = false) : boolean

Is a type allowed in this restriction?

This steps through a given type, be it Phabstractic\Data\Types\Type, Integer, String, or Array. If it's not a Type object, to specify a particular typed class you provide array( type [Type, Integer, String], className) because an array can be passed with one argument to turn into a Type object it still converts the array[0] to a Type object

For flexibility in type checking (if container object is strict or not) we provide a strict option in the function in addition to the object at constructor time.

NOTE: if Phabstractic\Data\Types\Type throws an \UnexpectedValueException, we just silently return false, unless strict parameter is true

Parameters

\Phabstractic\Data\Types\Phabstractic\Data\Types\Type|array|integer|string $type

The type to check

boolean $strict

Should we throw errors based on match?

Throws

\Phabstractic\Data\Types\Phabstractic\Data\Types\Exception\UnexpectedValueException

if OPTION strict rethrows the type construction error

Returns

boolean —

Allowed or not?

checkElements()

checkElements(array  $values, \Phabstractic\Data\Types\Resource\FilterInterface  $filter, boolean  $strict = null) : boolean

Checks values to see if they fit the restrictions defined for the Restricted object

This goes through each value, grabs its type, and compares it against the Restrictions

Parameters

array $values

The values to check against the restrictions

\Phabstractic\Data\Types\Resource\FilterInterface $filter
boolean $strict

Throw errors?

Throws

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

if the value is untypeable

Returns

boolean —

Valid types?

getAllowedTypes()

getAllowedTypes() : array

Retrieve the allowed types as an array of values

These values correspond to the constants found in Type::

Returns

array —

Allowed types

getAllowedClasses()

getAllowedClasses() : array

Retrieve the allowed classes as an array of values

These values correspond to the class names allowed for a Type::TYPED_OBJECT

Returns

array —

Allowed classes

setAllowedTypes()

setAllowedTypes(array  $allowed) 

Set the allowed types

If strict option is set (see constructor) then throw an error if type doesn't exist in TYPE class

Parameters

array $allowed

The allowed types

Throws

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

addAllowedType()

addAllowedType(integer  $type) 

Add allowed type to existing set

If strict option is set (see constructor) then throw an error if type doesn't exist in TYPE class

Parameters

integer $type

The type to add (from an enumeration)

Throws

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

removeAllowedType()

removeAllowedType(integer  $type) 

Remove an allowed type from ane existing set

If strict option is set (see constructor) then throw an error if type doesn't exist in TYPE class

Parameters

integer $type

The type to remove (from an enumeration)

Throws

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

setAllowedClasses()

setAllowedClasses(array  $classes) 

Set the allowed classes

Parameters

array $classes

The allowed classes (as strings)

addAllowedClass()

addAllowedClass(string  $class) 

Add a class to an existing set of allowed classes

Parameters

string $class

The FQN of the class

removeAllowedClass()

removeAllowedClass(string  $class) 

Remove a class from an existing set of allowed classes

Parameters

string $class

The FQN of the class

__construct()

__construct(array  $allowed, array  $classes = array(), array  $options = array()) 

Restrictions Class Constructor

Takes the allowed types ($allowed), the allowed classes (optional), and the options pertinent to the Restrictions class. Normalizes and sets up the predicate structures necessary to evaluate data types from the input data.

Options: autoload - Do we autoload classes when checking for their existence? allowed - Set Interface Dependency Injection (optional) classes - Set Interface Dependency Injection (optional) type_class - The class to instantiate for type checking strict_sets - pass strict on to sets

Parameters

array $allowed

(Required) the allowed data types as defined in Type::enumerator

array $classes

The classes to be allowed if $allowed contains Type::TYPED_OBJECT

array $options

The options ('autoload') for the given Restrictions object

Throws

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

if data type doesn't match any Type:: constants

\Phabstractic\Data\Types\Phabstractic\Data\Types\Exception\RuntimeException

if a class doesn't exist to match a class in the $classes member

__debugInfo()

__debugInfo() 

Debug Info (var_dump)

Display debug info

Requires PHP 5.6+

getDefaultRestrictions()

getDefaultRestrictions() : \Phabstractic\Data\Types\Phabstractic\Data\Type\Restrictions

Return a basic free form restrictions

Note: Cannot permit a typed object

Returns

\Phabstractic\Data\Types\Phabstractic\Data\Type\Restrictions —

The restrictions object

checkRestrictedValues()

checkRestrictedValues(array  $values,   $restrictions, boolean  $strict = null) : boolean

Alias for checkElements()

Backwards compatibility

Parameters

array $values

The values to check against the restrictions

$restrictions
boolean $strict

Throw errors?

Throws

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

if the value is untypeable

Returns

boolean —

Valid types?

compare()

compare(\Phabstractic\Data\Types\Restrictions  $R1, \Phabstractic\Data\Types\Restrictions  $R2) : boolean

Compare Restrictions

This allows restrictions to be compared despite their configurations This comes up when trying to compare restrictions like so:

$this->restrictions == $that->restrictions

The above takes into count configurations, so this avoids that.

Parameters

\Phabstractic\Data\Types\Restrictions $R1
\Phabstractic\Data\Types\Restrictions $R2

Returns

boolean —

Equal?

isProperType()

isProperType(integer  $type) : boolean

Is the given value type in the TYPE class?

Checks the values of the constants in the enumeration

Parameters

integer $type

The constant value to test

Returns

boolean

classExists()

classExists(  $class) 

Does the given class or interface exist?

Parameters

$class

setBasicOrTyped()

setBasicOrTyped() 

Set TYPED_OBJECT or BASIC_OBJECT appropriately depending on classes