PHP 클래스 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
저자: Rolf Vreijdenberger
저자: Richard Ruiter
상속: implements izzum\rules\IRule
파일 보기 프로젝트 열기: rolfvreijdenberger/izzum-statemachine

공개 메소드들

메소드 설명
__toString ( )
andRule ( Rule $other ) : Rule Chain a 'AND' rule.
applies ( ) : boolean The applies method is the only point where the rule can be validated.
containsResult ( string $expected ) Check if this rule contains a certain expected result.
getCacheEnabled ( ) : boolean
getResults ( ) : RuleResult[] Gets an array of RuleResult to check if a rule has set a certain result for the client of the rule.
hasResult ( ) : boolean Has any result?
not ( ) : Rule Inverse current rule
orRule ( Rule $other ) : Rule Chain a 'OR' rule.
setCacheEnabled ( boolean $cached = true ) should we cache the result if the rule is applied more than once?
toString ( ) : string
xorRule ( Rule $other ) : Rule Chain a 'XOR' rule.

보호된 메소드들

메소드 설명
_applies ( ) : boolean A concrete rule should at least implement the _applies method and return a boolean.
addResult ( string $result ) Add a result to this rule.
getCache ( ) : boolean return the cached value
handleException ( Exception $e ) hook method for logging etc.

비공개 메소드들

메소드 설명
clearCache ( )
clearResult ( ) Clear the results
setCache ( $result )
shouldReturnCache ( ) : boolean should we return a cached value?

메소드 상세

__toString() 공개 메소드

public __toString ( )

_applies() 추상적인 보호된 메소드

When the rule logic cannot determine if it applies then an exception should be thrown instead of a boolean value
abstract protected _applies ( ) : boolean
리턴 boolean

addResult() 최종 보호된 메소드

this might be useful if a client wants to check if a rule has executed certain steps during the logic of rule execution.
final protected addResult ( string $result )
$result string

andRule() 최종 공개 메소드

This means both rules should apply.
final public andRule ( Rule $other ) : Rule
$other Rule
리턴 Rule

applies() 최종 공개 메소드

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.
final public applies ( ) : boolean
리턴 boolean

containsResult() 최종 공개 메소드

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()
또한 보기: Rule::getResults()
final public containsResult ( string $expected )
$expected string

getCache() 최종 보호된 메소드

return the cached value
final protected getCache ( ) : boolean
리턴 boolean

getCacheEnabled() 최종 공개 메소드

final public getCacheEnabled ( ) : boolean
리턴 boolean

getResults() 공개 메소드

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[]
리턴 RuleResult[]

handleException() 보호된 메소드

hook method for logging etc.
protected handleException ( Exception $e )
$e Exception

hasResult() 최종 공개 메소드

Has any result?
final public hasResult ( ) : boolean
리턴 boolean

not() 최종 공개 메소드

Inverse current rule
final public not ( ) : Rule
리턴 Rule

orRule() 최종 공개 메소드

This means one of the rules should apply.
final public orRule ( Rule $other ) : Rule
$other Rule
리턴 Rule

setCacheEnabled() 최종 공개 메소드

should we cache the result if the rule is applied more than once?
final public setCacheEnabled ( boolean $cached = true )
$cached boolean

toString() 공개 메소드

public toString ( ) : string
리턴 string

xorRule() 최종 공개 메소드

This means one of the rules should apply but not both.
final public xorRule ( Rule $other ) : Rule
$other Rule
리턴 Rule