Simple HTML5/XML DOM factory
dom-factory instantiates DOM objects from web documents (HTML5, SVG, XML, etc.). It differs from PHP's default DOMDocument
creation by
- Support for HTML5 elements (e.g.
<main>
,<header>
,<footer>
, etc.) - Support for inlined foreign formats (e.g. SVG, MathML, etc.)
- Support for XML style namespaces
The factory can be used with strings, files or URIs and returns regular PHP DOMDocument objects. Source data will first be treated as XML using PHP's native DOM implementation. If parsing fails (e.g. because of non-wellformedness), the factory falls back to the masterminds/html5 parser.
use Jkphl\Domfactory\Ports\Dom;
$dom = Dom::createFromString('<html><head/><body>...</body></html>');
use Jkphl\Domfactory\Ports\Dom;
$dom = Dom::createFromFile('/path/to/file.html');
use Jkphl\Domfactory\Ports\Dom;
$dom = Dom::createFromUri('https://example.com/path/to/file.xml');
// With HTTP client options
$options = [
'client' => ['timeout' => 30],
'request' => ['verify' => false],
];
$dom = Dom::createFromUri('https://example.com/path/to/file.xml', $options);
If the cURL PHP extension is available on your system the factory uses Guzzle to fetch the web resource or otherwise fall back to a native file_get_contents()
strategy. Depending on the request mechanism, the second (array) parameter for Dom::createFromUri()
will be used
- as Guzzle client / request options or
- as
$options
for creating a PHP stream context (client
key) respectively setting its parameters (request
key).
Please make sure to pass in a compatible set of options.
This library requires PHP >=5.6 or later. I recommend using the latest available version of PHP as a matter of principle. It has no userland dependencies. It's installable and autoloadable via Composer as jkphl/dom-factory.
composer require jkphl/dom-factory
Alternatively, download a release or clone this repository, then require or include its autoload.php
file.
To run the unit tests at the command line, issue composer install
and then phpunit
at the package root. This requires Composer to be available as composer
, and PHPUnit to be available as phpunit
.
This library attempts to comply with PSR-1, PSR-2, and PSR-4. If you notice compliance oversights, please send a patch via pull request.
Found a bug or have a feature request? Please have a look at the known issues first and open a new issue if necessary. Please see contributing and conduct for details.
If you discover any security related issues, please email joschi@kuphal.net instead of using the issue tracker.
Copyright © 2017 Joschi Kuphal / joschi@kuphal.net. Licensed under the terms of the MIT license.