Class: qJerry
Source Location: /qjerry.php
Class qJerry
Class Overview
|
Implements interfaces:
- IteratorAggregate (internal interface)
qJerry: write less, do more. Now in PHP.
qJerry is the PHP class that provides a way to work with XML documents in jQuery-like style. It has methods for creating, modifying, querying XML documents and more. Generally speaking, qJerry is jQuery for server side. qJerry has convenient API, similar to the jQuery's one, including naming and behavior of most methods. Almost all methods returns qJerry instance, so chaining is possible as well. People familiar with jQuery can start using qJerry immediately. Example. Say, we need to create the following XML document: <?xml version="1.0" encoding="UTF-8"?>
<items><item id="1"/><item id="2"/></items>
Do it using traditional DOM functions: $dom = new DOMDocument('1.0', 'UTF-8');
$dom->appendChild($dom->createElement('items'));
$dom->documentElement->appendChild($dom->createElement('item'))->setAttribute('id', '1');
$dom->documentElement->appendChild($dom->createElement('item'))->setAttribute('id', '2');
The same using qJerry:
As can be seen, working with XML using qJerry is much easier than using DOM, even in most trivial case, never mind when it comes to complex manipulations with multiple queries and modifications of XML tree. Creating qJerry instances. There are three equivalent ways to create instance of the qJerry class: $q = new qJerry($content); // using constructor
$q = q($content); // using shortcut function q()
The last two calls can be chained with qJerry method calls. See qJerry::__construct() for details. The special case is the qJerry::fromArray() static method, that can be used to create XML from equivalent array structure:
Querying XML documents. qJerry uses XPath as query language, and unlike jQuery, it does not have any special selectors. There is simple no need to use them, since XPath is much more powerful and has good support in PHP out of the box. Examples:
$items = $q->find("/users/user[group='members' and not(@block = 'yes')]");
// or
Iterating and looping. qJerry implements IteratorAggregate interface, so it is possible to pass qJerry instance directly to foreach() statement. In such case elements of the matched set can be accessed inside foreach() loop as qJerry instances: foreach($items as $item) {
}
It is also possible to iterate over the matched DOMElements: foreach($items->get() as $elem) {
echo $elem->getAttribute('id').' - '.$elem->getElementsByTagName('login')->item(0)->nodeValue."\n";
}
Error handling. qJerry methods throws a qJException on error. There are few situations that cause an exception to be thrown: - file I/O errors
- XML parsing errors
- invalid arguments passed to the methods
- array indexes out of bound
An empty or unchanged set can be returned by some methods. Make sure to check return values in your scripts.
Located in /qjerry.php [line 135]
Author(s):
Information Tags:
| Todo: | Some features to implement in future: - add support for attribute nodes
- add support for namespaces
- give more attention to text nodes
- write automated tests
|
|
Properties
|
Methods
|
Property Summary
| DOMDocument |
$document |
Instance of current working DOM document. |
| string |
$file |
Filename of current working document. |
| boolean |
$format |
Whether format or not XML tree when saving. |
| integer |
$length |
Number of matched elements. |
| array |
$nodes |
Array of matched elements. |
| DOMXPath |
$xp |
DOMXPath instance for working DOM document. |
Method Summary
| static
qJerry
|
load() |
Create qJerry object and load local XML file into it. |
| qJerry |
add() |
Add more elements, matched by the given expression, to the set of matched elements. |
| qJerry |
after() |
Insert content after each of the matched elements. |
| qJerry |
andSelf() |
Add the previous selection to the current selection. |
| qJerry |
append() |
Append content to the inside of every matched element. |
| qJerry |
appendTo() |
Append all of the matched elements to another, specified, set of elements. |
| string|false|qJerry |
attr() |
Access an attribute of the first matched element. |
| qJerry |
before() |
Insert content before each of the matched elements. |
| qJerry |
children() |
Get a set of elements containing all of the unique immediate children of each of the matched set of elements. |
| qJerry |
clear() |
Remove all child nodes from the set of matched elements. |
| qJerry |
copy() |
Clone matched DOM eslements and select the clones. |
| DOMDocument |
doc() |
Return current working DOM document. |
| qJerry |
dump() |
Dump the current working DOM document. |
| qJerry |
each() |
Execute a function within the context of every matched element. |
| qJerry |
end() |
Revert the most recent 'destructive' operation. |
| qJerry |
eq() |
Reduce the set of matched elements to a single element. |
| string|qJerry |
file() |
Get/set file name of the current working document. |
| qJerry |
filter() |
Filter the matched set using expression or callback function. |
| qJerry |
find() |
Search for all elements that match the specified expression. |
| qJerry |
format() |
Turn on/off formatting when saving the document. |
| DOMElement|array |
get() |
Return a single element at a specified index. |
| ArrayIterator |
getIterator() |
IteratorAggregate interface implementation. |
| integer|false |
index() |
Return the index of the element. |
| qJerry |
insertAfter() |
Insert all of the matched elements after another, specified, set of elements. |
| qJerry |
insertBefore() |
Insert all of the matched elements before another, specified, set of elements. |
| boolean |
is() |
Test current selection against an expression. |
| string|false |
name() |
The tag name of first matched element. |
| qJerry |
next() |
Return the very next sibling for each element. |
| qJerry |
nextAll() |
Find all sibling elements after all the matched elements. |
| qJerry |
not() |
Remove elements matching the specified expression from the set of matched elements. |
| qJerry |
parent() |
Get a set of elements containing the unique parents of the matched set of elements. |
| qJerry |
parents() |
Get a set of elements containing the unique ancestors of the matched set of elements (except for the root element). |
| qJerry |
prepend() |
Prepend content to the inside of every matched element. |
| qJerry |
prependTo() |
Prepend all of the matched elements to another, specified, set of elements. |
| qJerry |
prev() |
Get a set of elements containing the unique previous siblings of each of the matched set of elements. |
| qJerry |
prevAll() |
Find all sibling elements before the current element. |
| qJerry |
remove() |
Removes all matched elements from the DOM document. |
| qJerry |
replaceAll() |
Replaces the elements matched by the specified expression with the matched elements. |
| qJerry |
replaceWith() |
Replaces all matched elements with the specified HTML or DOM elements. |
| qJerry |
reset() |
Discard history of destructive operations. |
| qJerry |
root() |
Return qJerry instance initialized with the root element of the current working DOM document. |
| qJerry |
save() |
Save current working document to file. |
| string |
saveXML() |
Create string representation of the current working document. |
| qJerry |
siblings() |
Get a set of elements containing all of the unique siblings of each of the matched set of elements. |
| integer |
size() |
The number of elements in the qJerry object. |
| qJerry |
slice() |
Select a subset of the matched elements. |
| string|qJerry |
text() |
Get/set the text contents of all matched elements. |
| qJerry |
wrap() |
Wrap all matched elements with a structure of other elements. |
| qJerry |
wrapAll() |
Wrap all the elements in the matched set into a single wrapper element. |
| qJerry |
wrapInner() |
Wrap the inner child contents of each matched element (including text nodes) with an HTML structure. |
Properties
Instance of current working DOM document.
API Tags:
Filename of current working document.
API Tags:
Whether format or not XML tree when saving.
API Tags:
Number of matched elements.
API Tags:
Array of matched elements.
API Tags:
Parent qJerry instance.
API Tags:
DOMXPath instance for working DOM document.
API Tags:
Methods
static qJerry create(
[mixed
$content = null]
)
|
|
Create new qJerry instance.
This method is just a wrapper for 'new qJerry(...)' call.
Parameters:
|
mixed |
$content: |
Data with which qJerry object will be initialized. |
API Tags:
| Return: | New instance. |
| Access: | public |
static qJerry load(
string
$file
)
|
|
Create qJerry object and load local XML file into it.
Example: load file 'somefile.xml' from current directory.
Parameters:
|
string |
$file: |
File name to load. |
API Tags:
| Return: | New instance. |
| Access: | public |
qJerry __construct(
[string|DOMDocument|DOMElement|DOMNodeList|null
$content = null], [qJerry
$parent = null]
)
|
|
Constructor.
The constructor is used to create and initialize a new qJerry object instance with passed data. The $content parameter can be a value of several types. If the $content argument is a string and it starts with "<?xml", then try to load an XML document from it, or throw qJException on failure: $xml = '<?xml version="1.0" encoding="UTF-8"?>'."\n".
'<items><item id="1"/><item id="2"/></items>';
$q = new qJerry($xml);
Else, if string matches pattern "/^[a-z_][0-9a-z\-\._]*$/i", create new DOM document with the root element with specified name (useful for creating XML documents from scratch): $q = new qJerry('root-element');
Otherwise, treat string as file name or URL and try to load it (with transparent support for URL wrappers), or throw qJException on failure: $q = new qJerry('./filename.xml');
$q = new qJerry('http://qjerry.com/example.xml');
If $content is the DOMDocument, then load it and set its root element as the only member of the matched set: $dom = new DOMDocument('1.0', 'UTF-8');
$dom->appendChild($dom->createElement('root'));
$dom->documentElement->setAttribute('name', 'Root element');
If $content is the DOMElement, then load DOM document to which specified element belongs, and set this element as the only member of the matched set: $q = new qJerry($dom->documentElement);
If $content is the DOMNodeList, then convert node list to array and set it as the matched set: $q = new qJerry($dom->getElementsByTagName('root'));
If $content is not specified, then create an empty document (userful for creating XML documents from scratch):
12If $content is of any other type, then throw a qJException.
Parameters:
|
string|DOMDocument|DOMElement|DOMNodeList|null |
$content: |
Data with which qJerry object will be initialized. |
|
qJerry |
$parent: |
Optional parent qJerry object. For internal use only. |
API Tags:
qJerry add(
[string
$expr = null]
)
|
|
Add more elements, matched by the given expression, to the set of matched elements.
Parameters:
API Tags:
qJerry after(
string|qJerry|DOMDocument|DOMElement|DOMNodeList
$content
)
|
|
Insert content after each of the matched elements.
Parameters:
|
string|qJerry|DOMDocument|DOMElement|DOMNodeList |
$content: |
Content to be inserted. |
API Tags:
| Return: | The set of the inserted elements. |
| Access: | public |
Add the previous selection to the current selection.
Useful for traversing elements, and then adding something that was matched before the last traversion.
API Tags:
qJerry append(
string|qJerry|DOMDocument|DOMElement|DOMNodeList
$content, [string
$ns = '']
)
|
|
Append content to the inside of every matched element.
This operation is similar to calling appendChild on all of the specified elements, adding them into the document. Optional namespace for the appended element may be provided.
Parameters:
|
string|qJerry|DOMDocument|DOMElement|DOMNodeList |
$content: |
Content to be appended. |
|
string |
$ns: |
Optional namespace for appended element. |
API Tags:
qJerry appendTo(
qJerry|DOMDocument|DOMElement|DOMNodeList
$content
)
|
|
Append all of the matched elements to another, specified, set of elements.
This operation is, essentially, the reverse of doing a regular $(A).append(B), in that instead of appending B to A, you're appending A to B.
Parameters:
|
qJerry|DOMDocument|DOMElement|DOMNodeList |
$content: |
|
API Tags:
Information Tags:
string|false|qJerry attr(
[string
$name = null], [string
$value = null], [array
$properties = null], callback
$callback
)
|
|
Access an attribute of the first matched element.
Set or get values of single or multiple attributes, on all matched elements.
Parameters:
|
string |
$name: |
The name of the attribute to set/get. |
|
string |
$value: |
The value to set the attribute to. |
|
array |
$properties: |
Array of key=>value pairs. |
|
callback |
$callback: |
|
API Tags:
qJerry before(
mixed
$content
)
|
|
Insert content before each of the matched elements.
Parameters:
|
mixed |
$content: |
Content to be inserted. |
API Tags:
| Return: | The set of the inserted elements. |
| Access: | public |
qJerry children(
[string
$expr = null]
)
|
|
Get a set of elements containing all of the unique immediate children of each of the matched set of elements.
This set can be filtered with an optional expression that will cause only elements matching the expression to be collected. Also note: while parents() will look at all ancestors, children() will only consider immediate child elements.
Parameters:
|
string |
$expr: |
An expression with which to filter the result set. |
API Tags:
Remove all child nodes from the set of matched elements.
This method is synonym of jQuery's empty() method, since 'empty' is reserved word in PHP.
API Tags:
Clone matched DOM eslements and select the clones.
This method is synonym of jQuery's clone() method, since the word 'clone' is reserved in PHP.
API Tags:
| Return: | A set of copied (cloned) elements. |
| Access: | public |
Return current working DOM document.
API Tags:
| Return: | Current working DOM document. |
| Access: | public |
Dump the current working DOM document.
API Tags:
qJerry each(
callback
$callback
)
|
|
Execute a function within the context of every matched element.
Every time the callback function is executed the 1st argument points to the qJerry object of specific DOMElement, and the 2nd argument represents the index of that element in the matched set. Returning 'false' from within the callback function completely stops the loop through all of the elements (this is like using a 'break' with a normal loop). Returning 'true' from within the loop skips to the next iteration (this is like using a 'continue' with a normal loop).
Parameters:
|
callback |
$callback: |
Callback function name. |
API Tags:
Revert the most recent 'destructive' operation.
Change the set of matched elements to its previous state, right before the destructive operation. If there was no destructive operation before, current set is returned. Note: jQuery's version of this method returns an empty set if there was no destructive operation before.
API Tags:
qJerry eq(
[integer
$index = 0]
)
|
|
Reduce the set of matched elements to a single element.
If no $index specified, then the set will be reduced to the very first element.
Parameters:
|
integer |
$index: |
Index of the element. |
API Tags:
string|qJerry file(
[string|null
$file = null]
)
|
|
Get/set file name of the current working document.
Parameters:
API Tags:
qJerry filter(
[string|callback
$expr = null]
)
|
|
Filter the matched set using expression or callback function.
Remove all elements from the set of matched elements that do not match the specified expression or function.
Parameters:
|
string|callback |
$expr: |
An expression or function to pass to the filter. |
API Tags:
qJerry find(
[string
$expr = null]
)
|
|
Search for all elements that match the specified expression.
Important note: although the expression is tested in context of each matched element, the resulting set may contain elements from any place of the document, depending on XPath passed as an expression.
Parameters:
|
string |
$expr: |
An expression with which to find. |
API Tags:
qJerry format(
[boolean
$format = true]
)
|
|
Turn on/off formatting when saving the document.
Parameters:
API Tags:
qJerry fromArray(
array
$array, [string
$name = null], [mixed
$value = null], [qJerry
$node = null]
)
|
|
Create DOM document from the multi-dimensional array.
Value returned by this method highly depends on correct structure of input array. There are the basic rules: - one array key corresponds to one DOM element
- attribute name should begin with '@' character
- value of an attribute should be of scalar type
- elements with the same name on the same level should end with suffix '~1', '~2', ...
This method is useful for processing HTML forms. In such case, the 'name' attribute of each input element of the form should follow the syntax of HTML array feature. Example form: <form action="user.php" method="post">
< input type= "hidden" name= "user[@id]" value= "100"/>
< input type= "checkbox" name= "user[@block]" value= "no" checked= "checked"/>
< input type= "text" name= "user[login]" value= "myUserName"/>
< input type= "password" name= "user[password]" value= "somePassword"/>
< input type= "text" name= "user[group~1]" value= "members"/>
< input type= "text" name= "user[group~2]" value= "bloggers"/>
< input type= "text" name= "user[group~3]" value= "admins"/>
<input type="submit"/>
</form>
Example form handler: <?php
?>
This will output: Array
(
[user] => Array
(
[@id] => 100
[@block] = no
[login] => myUserName
[password] => somePassword
[group~1] => members
[group~2] => bloggers
[group~3] => admins
)
)
<?xml version="1.0" encoding="UTF-8"?>
<user id="100" block="no">
<login>myUserName</login>
<password>somePassword</password>
<group>members</group>
<group>bloggers</group>
<group>admins</group>
</user>
Parameters:
|
array |
$array: |
Input array. |
|
string |
$name: |
The key from which to start. |
|
mixed |
$value: |
Value. Internal use only. |
|
qJerry |
$node: |
Current node. Internal use only. |
DOMElement|array get(
[integer
$index = null]
)
|
|
Return a single element at a specified index.
If no $index specified, then whole array will be returned.
Parameters:
|
integer |
$index: |
Index of the element. |
API Tags:
ArrayIterator getIterator(
)
|
|
IteratorAggregate interface implementation.
There is no need to call this method directly. It allows to iterate over the matched set using foreach() loop, just like over an array of qJerry instances. foreach($q as $item)
echo $item->attr('id'). "\n";
API Tags:
Implementation of:
- IteratorAggregate::getIterator
integer|false index(
DOMElement
$element
)
|
|
Return the index of the element.
If no such element found in the matched set, then return false.
Parameters:
|
DOMElement |
$element: |
Element, which index need to be found. |
API Tags:
qJerry insertAfter(
mixed
$content
)
|
|
Insert all of the matched elements after another, specified, set of elements.
This operation is, essentially, the reverse of doing a regular q(A)->after(B), in that instead of inserting B after A, you're inserting A after B.
Parameters:
|
mixed |
$content: |
Content to be inserted. |
API Tags:
| Return: | The set of the inserted elements. |
| Access: | public |
qJerry insertBefore(
mixed
$content
)
|
|
Insert all of the matched elements before another, specified, set of elements.
This operation is, essentially, the reverse of doing a regular q(A).before(B), in that instead of inserting B before A, you're inserting A before B.
Parameters:
|
mixed |
$content: |
Content to be inserted. |
API Tags:
| Return: | The set of the inserted elements. |
| Access: | public |
boolean is(
[string
$expr = null]
)
|
|
Test current selection against an expression.
Checks the current selection against an expression and returns true, if at least one element of the selection fits the given expression.
Parameters:
|
string |
$expr: |
The expression with which to filter. |
API Tags:
The tag name of first matched element.
API Tags:
qJerry next(
[string
$expr = null]
)
|
|
Return the very next sibling for each element.
An optional expression to filter the result may be provided.
Parameters:
|
string |
$expr: |
An expression to filter the returned set. |
API Tags:
qJerry nextAll(
[string
$expr = null]
)
|
|
Find all sibling elements after all the matched elements.
Optional expression to filter the matched set may be used.
Parameters:
|
string |
$expr: |
An expression with which to filter the result. |
API Tags:
qJerry not(
[string|array|DOMElement
$expr = null]
)
|
|
Remove elements matching the specified expression from the set of matched elements.
Parameters:
|
string|array|DOMElement |
$expr: |
An expression with which to remove matching elements. |
API Tags:
qJerry parent(
[string
$expr = null]
)
|
|
Get a set of elements containing the unique parents of the matched set of elements.
Optional expression to filter the matched set may be used.
Parameters:
|
string |
$expr: |
An expression with which to filter the result. |
API Tags:
qJerry parents(
[string
$expr = null]
)
|
|
Get a set of elements containing the unique ancestors of the matched set of elements (except for the root element).
Parameters:
|
string |
$expr: |
An expression with which to filter the result. |
API Tags:
Information Tags:
| Todo: | Make sure that exception for the root element applied. |
qJerry prepend(
string|qJerry|DOMDocument|DOMElement|DOMNodeList
$content, [
$ns = null]
)
|
|
Prepend content to the inside of every matched element.
This operation is the best way to insert elements inside, at the beginning, of all matched elements.
Parameters:
|
string|qJerry|DOMDocument|DOMElement|DOMNodeList |
$content: |
Content to be prepended. |
|
|
$ns: |
|
API Tags:
qJerry prependTo(
qJerry|DOMDocument|DOMElement|DOMNodeList
$content
)
|
|
Prepend all of the matched elements to another, specified, set of elements.
This operation is, essentially, the reverse of doing a regular q(A).prepend(B), in that instead of prepending B to A, you're prepending A to B.
Parameters:
|
qJerry|DOMDocument|DOMElement|DOMNodeList |
$content: |
An element set to prepend to. |
API Tags:
qJerry prev(
[string
$expr = null]
)
|
|
Get a set of elements containing the unique previous siblings of each of the matched set of elements.
An optional expression to filter the result may be provided.
Parameters:
|
string |
$expr: |
An expression to filter the returned set. |
API Tags:
qJerry prevAll(
[string
$expr = null]
)
|
|
Find all sibling elements before the current element.
An optional expression to filter the result may be provided.
Parameters:
|
string |
$expr: |
An expression to filter the returned set. |
API Tags:
qJerry remove(
[string
$expr = null]
)
|
|
Removes all matched elements from the DOM document.
Parameters:
|
string |
$expr: |
An expression to filter the matched set. |
API Tags:
| Return: | A set of removed elements. |
| Access: | public |
qJerry removeAttr(
string
$name
)
|
|
Remove an attribute from each of the matched elements.
Parameters:
|
string |
$name: |
The name of the attribute to remove. |
API Tags:
qJerry replaceAll(
[string
$expr = null]
)
|
|
Replaces the elements matched by the specified expression with the matched elements.
This function is the complement to replaceWith() which does the same task with the parameters reversed.
Parameters:
API Tags:
qJerry replaceWith(
string|DOMDocument|DOMElement|DOMNodeList
$elem
)
|
|
Replaces all matched elements with the specified HTML or DOM elements.
Parameters:
|
string|DOMDocument|DOMElement|DOMNodeList |
$elem: |
|
API Tags:
Discard history of destructive operations.
API Tags:
Return current working document as server response.
This method is useful when writing AJAX enabled applications.
API Tags:
Return qJerry instance initialized with the root element of the current working DOM document.
API Tags:
qJerry save(
[string
$file = '']
)
|
|
Save current working document to file.
Parameters:
|
string |
$file: |
File name to which to save the document. |
API Tags:
Create string representation of the current working document.
API Tags:
qJerry siblings(
[string
$expr = null]
)
|
|
Get a set of elements containing all of the unique siblings of each of the matched set of elements.
Can be filtered with an optional expression.
Parameters:
|
string |
$expr: |
An expression to filter the returned set. |
API Tags:
The number of elements in the qJerry object.
API Tags:
| Return: | Number of elements in the matched set. |
| Access: | public |
qJerry slice(
integer
$offset, integer
$length
)
|
|
Select a subset of the matched elements.
Attention: this is a PHP-style version of jQuery's slice() method. It relies on array_slice() function and inherits its behavior.
Parameters:
|
integer |
$offset: |
|
|
integer |
$length: |
|
API Tags:
string|qJerry text(
[string
$text = null]
)
|
|
Get/set the text contents of all matched elements.
Set the text contents of all matched elements to specified value. If there was no value specified, the combined text contents of all matched elements is returned. Note: after setting text values, qJerry object with the same set of the elements is returned.
Parameters:
|
string |
$text: |
The text to which to set matched elements. |
API Tags:
qJerry wrap(
string|qJerry|DOMDocument|DOMElement|DOMNodeList
$elem
)
|
|
Wrap all matched elements with a structure of other elements.
Parameters:
|
string|qJerry|DOMDocument|DOMElement|DOMNodeList |
$elem: |
The element name or the DOM tree to wrap elements with. |
API Tags:
| Return: | The root element of the wrapping structure. |
| Access: | public |
qJerry wrapAll(
string|qJerry|DOMDocument|DOMElement|DOMNodeList
$elem
)
|
|
Wrap all the elements in the matched set into a single wrapper element.
This is different from wrap() method where each element in the matched set would get wrapped with an element.
Parameters:
|
string|qJerry|DOMDocument|DOMElement|DOMNodeList |
$elem: |
The element name or the DOM tree to wrap elements with. |
API Tags:
| Return: | The root element of the wrapping structure. |
| Access: | public |
qJerry wrapInner(
string|qJerry|DOMDocument|DOMElement|DOMNodeList
$elem
)
|
|
Wrap the inner child contents of each matched element (including text nodes) with an HTML structure.
Parameters:
|
string|qJerry|DOMDocument|DOMElement|DOMNodeList |
$elem: |
The element name or the DOM tree to wrap elements with. |
API Tags:
|
|