A library extending the PHP DOMDocument to support the Microdata DOM API.
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
ci
src/MicrodataDOM
tests/src/MicrodataDOM/Tests
.gitignore
.travis.yml
LICENSE
README.md
composer.json
phpunit.xml.dist

README.md

microdata-dom.php

license build coverage loc

This library extends the native PHP DOMDocument providing methods described by the Microdata and DOM API specifications to support finding microdata items and describing their properties. It is well covered by tests and tries to be efficient as it traverses the DOM.

Usage

For a document with microdata, use MicrodataDOM\DOMDocument. It works just like a regular DOMDocument, but has the extra microdata accessors. The following example...

$dom = new MicrodataDOM\DOMDocument();
$dom->loadHTMLFile('http://dpb587.me/about.html');

// find Person types and get the first item
$dpb587 = $dom->getItems('http://schema.org/Person')->item(0);
echo $dpb587->itemId;

// items are still regular DOMElement objects
printf(" (from %s on line %s)\n", $dpb587->getNodePath(), $dpb587->getLineNo());

// there are a couple ways to access the first value of a named property
printf("givenName: %s\n", $dpb587->properties['givenName'][0]->itemValue);
printf("familyName: %s\n", $dpb587->properties['familyName']->getValues()[0]);

// or directly get the third, property-defining DOM element
$property = $dpb587->properties[3];
printf("%s: %s\n", $property->itemProp[0], $property->itemValue);

// use the toArray method to get a Microdata JSON structure
echo json_encode($dpb587->toArray(), JSON_UNESCAPED_SLASHES) . "\n";

Will output something like...

http://dpb587.me/ (from /html/body/article/section on line 97)
givenName: Danny
familyName: Berger
jobTitle: Software Engineer
{"id":"http://dpb587.me/","type":["http://schema.org/Person"],"properties":{"givenName":["Danny"],...snip...}

Installation

You can install this library via composer...

$ composer install dpb587/microdata-dom

Development

You can find runtime code in src and test code in tests/src. If you are making changes, you should already have PHPUnit installed before running the tests...

$ phpunit

Alternatively, have your Concourse execute the tests with your local bits...

$ fly execute -c ci/tasks/test.yml -i repo=$PWD -x

Builds are publicly accessible on Travis CI and internally with Concourse.

References

You might find these specifications useful...

License

MIT License