Phabstractic: Restrictions Predicate (Data Type)

asherwunk/phabstractic now has type restrictions for elements implemented through a filter class in Restrictions.php (github)

Filtering

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 (github) and the AbstractFilter (github) 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 (github).

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 Phabstractic\Data\Types\Restrictions::checkElements() static function as well as provides a shortcut of its own  Phabstractic\Data\Types\Restrictions::checkRestrictedValues() [for backwards compatibility]. You can use the class simply as follows (check out the unit test for more examples):

If you are interested in restricting types to a particular class (including sub-classes) or interface you can specify the following using the Phabstractic\Data\Types\Type::TYPED_OBJECT  constant:

Be sure to only pass type information to Phabstractic\Data\Types\Restrictions::isAllowed() , not actual variables themselves.  You can accomplish that by formatting your calls using Type Enumeration:

If you want to check if an actual variable holding a value is allowed through a restrictions filter, use the static Phabstractic\Data\Types\Restrictions::checkElements()  method instead:

The Restrictions Function

Phabstractic\Data\Types\Restrictions::isAllowed() is the ultimate function for checking the type of a variable against the allowed classes and types of the restriction.  You can see it below:

Dynamic Restrictions

Of course, you can change the predicate at run-time.  This is useful if a new class is loaded into the code and you want to include it in your restrictions:

“Dependency Injection”

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 Phabstractic\Data\Types\Restrictions::isAllowed() , although it will reference the Type:: constants.  ‘allowed’ and ‘classes’ must implement SetInterface.php (github).

NOTE: If these options aren’t provided they default to Phabstractic\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 Phabstractic\Data\Types\Type::get();

You can see how these are checked and incorporated in the __constructor:

Conclusion

Filters are important and very useful objects, allowing values or other logic to be filtered in and out of a particular set of data.  Restrictions.php offers a way to use type checking in PHP, but in a very flexible and dynamic way.  This can be useful for defining lists or sets that only accept certain values, which is useful for code cohesiveness and can reduce error checking.

View on GitHub

This is part of the Phabstractic Library.

If you appreciate my programming please help support me through my Patreon.

photo credit: Sigma 18-35, 24mm @ f/4, medium focus distance via photopin (license)

Liked it? Take a second to support kadar on Patreon!

kadar

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

You may also like...

Leave a Reply

%d bloggers like this: