Primus2\Falcraft: New Data Type (restrictions predicate)


This code is now obsolete and has been moved to another library.  It is now part of asherwunk/phabstractic.  You can also read the current blog post about this functionality.

Primus2\Falcraft now has type restrictions for elements implemented through a filter class in Restrictions.php


A filter is a predicate data structure that compares values against some internal logic to see if they ‘pass through’ the filter or not.  This is the idea behind the FilterInterface and the AbstractFilter class.  Because the static method ::checkElements() can be used out of the box it was defined in the AbstractFilter class as follows:

This function can of course be overriden by the inheriting class, as it is in the case of Restrictions.php.

Type Restrictions

An example of a filter is to delineate what kinds of values can be accepted (into say, a set).  You can allow multiple kinds of values, or just one.  You can also specify if objects are of/inherit from a particular class or interface (it checks them up the inheritance tree).  This uses the Type predicate data object to figure out what type a given value is.  It also overrides the ::checkElements() static function as well as provides a shortcut of its own ::checkRestrictedValues() [for backwards compatibility]. You can use the class simply as follows:

Because I want to encourage and make room for flexibility you can also inject your own set classes using the ‘allowed’ and ‘classes’ options in the constructor.  You can also specify a particular Type class to be instantiated during the check for ::isAllowed(), although it will reference the Type:: constants.  ‘allowed’ and ‘classes’ must implement SetInterface.

NOTE: If these options aren’t provided they default to Falcraft\Data\Types\Set instances, instantiated in the constructor function.  This is important for unit testing purposes.  ‘type_class’ must also be a valid classname that implements ::get();

View on GitHub

photo credit: via photopin (license)


I'm just a wunk, trying to enjoy life. I am a cofounder of http// and I like computers, code, creativity, and friends.

You may also like...

Leave a Reply

%d bloggers like this: