PHP Class izzum\rules\Rule
Rules should never have side effects and should only return true or false.
This Rule serves as a base class for all your business logic, encapsulating
and centralizing the logic in a class and making it reusable through it's
interface.
This is a way of seperating mechanism and policy of code.
In other words: how it is done versus what should be done. The mechanism is a
rule
library, the policy is what is defined in the rule.
https://en.wikipedia.org/wiki/Separation_of_mechanism_and_policy
The rules package aims to prevent code like this:
if ($name == 'blabla' && $password == 'lala') {
do something here. the statement above might be duplicated in a lot of
places
}
and substitute it by this:
$rule = new AccesRule($name, $password);
if($rule->applies()) {
do something
}
or:
$rule = new IsOrderTaggedForDelivery($order);
$rule->applies();
usage:
Clients should subclass this Rule and implement the
protected '_applies' method and let that return a boolean value.
A concrete Rule (a subclass) can (and should be) injected with contextual
data via
dependency injection in the constructor
Show file
Open project: rolfvreijdenberger/izzum-statemachine
Public Methods
Protected Methods
Private Methods
Method Details
__toString()
public method
_applies()
abstract protected method
When the rule logic cannot determine if it applies then an exception
should
be thrown instead of a boolean value
addResult()
final protected method
this might be useful if a client wants to check if a rule
has executed certain steps during the logic of rule execution.
andRule()
final public method
This means both rules should apply.
final public andRule ( Rule $other ) : Rule |
$other |
Rule |
|
return |
Rule |
|
applies()
final public method
Internally each rule implements the _applies() method to do the actual
validation.
There are only two types of outcome for each rule.
Either the rule applies (true) or doesn't (false).
Any other outcome should always be thrown as an exception so no false
assumptions can be made by the caller. For example when a rule returns a
NULL value the caller may asume that false is meant. The rule cannot
trust that the caller checks the boolean type so we will.
containsResult()
final public method
This is only matched on the string, not on the class that generated
the result. you can check this against constants in a class eg:
Rule::RESULT_<*>.
In case you want to also know the class or classname, use getResults()
getCache()
final protected method
getCacheEnabled()
final public method
getResults()
public method
This will mostly be useful to check what actually happened when a rule
has failed and if the _applies() method actually calls 'addResult()' to
state what has happened in a rule.
public getResults ( ) : RuleResult[] |
return |
RuleResult[] |
|
handleException()
protected method
hook method for logging etc.
hasResult()
final public method
not()
final public method
final public not ( ) : Rule |
return |
Rule |
|
orRule()
final public method
This means one of the rules should apply.
final public orRule ( Rule $other ) : Rule |
$other |
Rule |
|
return |
Rule |
|
setCacheEnabled()
final public method
should we cache the result if the rule is applied more than once?
xorRule()
final public method
This means one of the rules should apply but not both.
final public xorRule ( Rule $other ) : Rule |
$other |
Rule |
|
return |
Rule |
|