Policies & Predicates

The main file to include:

<?php    
require_once('aperiplus/lib/predicates/predicates.php');

This contains a couple of basic predicates, Allow and Deny:

<?php    

class Allow {

    function 
isValid() {
        return 
true;
    }
}
class 
Deny {

    function 
isValid() {
        return 
false;
    }
}

Some other predicate classes might be found in aperiplus/lib/predicates/ but I haven't added many at the time of writing.

The Predicate interface:

<?php    

interface Predicate {    
    function 
isValid();
}

However php's idea of compliance is far too strict. Concrete methods must have an identical parameter list which can be a bit of a nuisance and so I don't usually bother with an "implements Predicate" in every class which does actually implement Predicate.

Conjunction (and), Disjunction (or) and Negation can be used to construct policies composed of elementary predicates.

Conjunction (and):

<?php    

require_once('aperiplus/lib/predicates/predicates.php');
require_once(
'aperiplus/lib/predicates/MatchesPattern.php');

$policy = new Conjunction;
$policy->add(new MatchesPattern('/beer/'));
$policy->add(new MatchesPattern('/whisky/'));

$policy->isValid('I want wine.'); // returns false
$policy->isValid('I want whisky.'); // returns false
$policy->isValid('I want beer.'); // returns false
$policy->isValid('I want beer and whisky.'); // returns true

Disjunction (or):

<?php    

$policy 
= new Disjunction;
$policy->add(new MatchesPattern('/beer/'));
$policy->add(new MatchesPattern('/whisky/'));

$policy->isValid('I want beer.'); // returns true
$policy->isValid('I want whisky.'); // returns true
$policy->isValid('I want wine.'); // returns false

Negation accepts either a simple predicate or a Conjunction/Disjunction instance.

<?php    

$policy 
= new Negation(new Disjunction);
$policy->add(new MatchesPattern('/beer/'));
$policy->add(new MatchesPattern('/whisky/'));

$policy->isValid('I want beer.'); // returns false
$policy->isValid('I want whisky.'); // returns false
$policy->isValid('I want wine.'); // returns true

SourceForge.net Logo