$conf
$conf : \Phabstractic\Features\Zend\Config\Config
The objects configuration object
Should be an instance of Zend/Config
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
$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.
$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.
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.
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. |
True if instantiated
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.
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(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()-
string | $format | The format to return, must be supported by ::saveSettings(), use $this->configWriters to support additional formats. |
$context |
The formatted string, or false otherwise
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.
\Phabstractic\Features\Zend\Config\Processor\ProcessorInterface | $processor | The given processor object |
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
\Phabstractic\Data\Types\Phabstractic\Data\Types\Type|array|integer|string | $type | The type to check |
boolean | $strict | Should we throw errors based on match? |
if OPTION strict rethrows the type construction error
Allowed or not?
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
array | $values | The values to check against the restrictions |
\Phabstractic\Data\Types\Resource\FilterInterface | $filter | |
boolean | $strict | Throw errors? |
if the value is untypeable
Valid types?
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
integer | $type | The type to add (from an enumeration) |
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
integer | $type | The type to remove (from an enumeration) |
__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
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 |
if data type doesn't match any Type:: constants
if a class doesn't exist to match a class in the $classes member
checkRestrictedValues(array $values, $restrictions, boolean $strict = null) : boolean
Alias for checkElements()
Backwards compatibility
array | $values | The values to check against the restrictions |
$restrictions | ||
boolean | $strict | Throw errors? |
if the value is untypeable
Valid types?
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.
\Phabstractic\Data\Types\Restrictions | $R1 | |
\Phabstractic\Data\Types\Restrictions | $R2 |
Equal?