Custom rules
Sometimes, it is just not enough to use a preset of rules and you wish to validate a page by your own complex rules.
Kickoff provides an easy way to add custom rules. You just have to conform to a few simple requirements:
- Your class needs to implement the \Frickelbruder\KickOff\Rules\RuleInterface. Or better extend \Frickelbruder\KickOff\Rules\RuleBase, which implements already provides a basic implementation.
- Your class needs to be autoloaded via the composer autoloader.
This is nearly all you need to know add your own Rules.
Implementing the validate() method
When implementing the validate() method, you receive an \Frickelbruder\KickOff\Http\HttpResponse\HttpResponse-Item, which you can inspect to make the necessary validation.
When the Response does not meet your requirements, you should set an appropriate error message to the classes $errorMessage property and return false.
The RuleBase class provides 2 handy functions to inspect the HTTP response:
- findHeader(), allows you to fetch a certain header from the HTTP response
- getResponseBodyAsXml(), retrieves the body as XML to access the response bodys DOM
Registering your rule
It's easy to add your rule to the config. Just add a "Rule"-section your config YAML file. As always: Learn from an example:
[...]
config:
[...]
Section:
website:
rules:
- MyCustomRule
[...]
Rules:
MyCustomRule:
class: \My\Custom\Namespace\MyCoolrule
calls:
- ['AnyCall', ["Any-value"]]
HttpHeaderHasMyCustomValue:
class: \Frickelbruder\KickOff\Rules\HttpHeaderHasValue
calls:
- ["setName", ["HttpHeaderHasMyCustomValue"]]
- ["setHeaderToSearchFor", ["Custom-Header"]]
- ["setValue", ["custom-value"]]
[...]
As you can see, you add you rule under the Rules-section by providing a custom identifier for your rule. This identifier is used as reference in the section configs.
You then set the classname to your rules identifier. If required you can add some method calls to your class.
You can also use one of the existing classes and maybe extend them by adding custom validation values. Just browse the source and see, which Rule might fit your needs.
That's really all you need to know.