Description
Located in File: /src/QueryPath/QueryPathExtension.php
A QueryPathExtension is a tool that extends the capabilities of a QueryPath object.
Extensions to QueryPath should implement the QueryPathExtension interface. The only requirement is that the extension provide a constructor that takes a QueryPath object as a parameter.
Here is an example QueryPath extension:
<?php
class StubExtensionOne implements QueryPathExtension {
private $qp = NULL;
public function __construct(QueryPath $qp) {
$this->qp = $qp;
}
public function stubToe() {
$this->qp->find(':root')->append('<toe/>')->end();
return $this->qp;
}
}
?>
In this example, the StubExtensionOne class implements QueryPathExtension. The constructor stores a local copyof the QueryPath object. This is important if you are planning on fully integrating with QueryPath's Fluent Interface.
Finally, the stubToe() function illustrates how the extension makes use of QueryPath internally, and remains part of the fluent interface by returning the $qp object.
Notice that beneath the class, there is a single call to register the extension with QueryPath's registry. Your extension should end with a line similar to this.
How is a QueryPath extension called?
QueryPath extensions are called like regular QueryPath functions. For example, the extension above can be called like this:
qp('some.xml')->stubToe
();
Since it returns the QueryPath ($qp) object, chaining is supported:
print
qp('some.xml')->stubToe
()->xml
();
When you write your own extensions, anything that does not need to return a specific value should return the QueryPath object. Between that and the extension registry, this will provide the best developer experience.