Extending Heuristics
The Heuristics class is a collection of static methods that return boolean values. When extending Heuristics, you should make your best effort to use the patterns it has put in place to work for you. If you don't it might work against you. :grin:
Best Practices
Heuristics methods should follow the following interface:
public static function awesome_heuristic(AiCrawler &$node, array $args = [])- Heuristic method names should never intersect with argument names of other heuristics.
- Heuristic method names should not have integers in them, subset methods use these!
- Usage of static class properties that match method names is encouraged.
- Usage of
arg,arr,booleanandtexthelper methods is encouraged. - Heuristic methods should
return static::subset(...)instead of true when possible. - Subset methods (e.g.
children()) should notreturn static::subset(...). - Heuristic methods should not persist data to the node's
$extraproperty when at all possible.
Example Custom Heuristics Class
class CustomHeuristics extends Heuristics
{
/**
* Even function. Determine if a node is even among its siblings.
*
* @param AiCrawler $node
* @param array $args
*
* @return bool
*/
public static function even(AiCrawler &$node, array $args = [])
{
try {
return $node->previousAll()->count() % 2 != 0;
} catch (InvalidArgumentException $e) {
return false;
}
}
/**
* Odd function. Determine if a node is odd among its siblings.
*
* @param AiCrawler $node
* @param array $args
*
* @return bool
*/
public static function odd(AiCrawler &$node, array $args = [])
{
try {
return $node->previousAll()->count() % 2 == 0;
} catch (InvalidArgumentException $e) {
return true;
}
}
}