View on GitHub

Brainite

QuipXML

QuipXml

CI

QuipXml is chainable PHP objects for manipulating XML. Unlike other libraries that attempt to replicate jQuery syntax throughout, Quip attempts to provide lightweight extensions to SimpleXML to facilitate cleaner code without imposing JavaScript conventions on a PHP project. The end result is fast and easy to use.

Requires PHP 8.3 or later.

Basic Usage

use QuipXml\Quip;

// The 'load' factory method aligns with SimpleXml constructor arguments.
// Markup that is not well-formed XML is read with the HTML parser.
$quip = Quip::load($xml_path, 0, TRUE);
$quip = Quip::load($xml_string);

// jQuery method names are used where appropriate.
$html = $quip->html();

// Queries carry a 'q' prefix: SimpleXMLElement::xpath() returns an array,
// and an override returning anything else is not legal PHP.
$quip->qxparent();
$quip->qxprev();

// While jQuery syntax is wonderful, you also get PHP advantages:
//  1. xpath
//  2. access children like properties
//  3. use foreach loops
$ul = $quip->qxpath("//ul")->eq(0);
$ul->li->after('<li>New bullet.</li>');
foreach ($quip->qxpath("//li") as $li) {
}

// For advanced operations, just like in jQuery, you can access the DOMNode for a given XML node.
$el = $ul->dom();

Nodes and sets

qxpath() returns a QuipXmlElementIterator holding every match, or an empty QuipXmlElement when nothing matched. Both implement QuipXmlElementInterface, so a chain does not need to know how many nodes it holds:

A prefix registered with registerXPathNamespace() carries over to every node Quip returns from that node (query results, children(), attributes(), addChild()), so a nested query needs no second registration. Nodes SimpleXML makes itself — property access such as $quip->child, or foreach over a SimpleXML list — start without it.

An attribute node (from attributes() or qxpath('//@name')) reads and writes its value through text() and is deleted by remove(); the structural verbs throw a \LogicException on it.

Upgrading from 0.x

Development

composer install
composer lint      # PHP_CodeSniffer: strict_types in every file
composer analyse   # PHPStan level 8
composer test      # PHPUnit; `vendor/bin/phpunit --group benchmark` for the memory benchmark