2017 © Pedro Peláez
 

library microdata-dom

A library extending the PHP DOMDocument to support the Microdata DOM API.

image

dpb587/microdata-dom

A library extending the PHP DOMDocument to support the Microdata DOM API.

  • Monday, April 4, 2016
  • by dpb587
  • Repository
  • 1 Watchers
  • 6 Stars
  • 466 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 1 Forks
  • 0 Open issues
  • 1 Versions
  • 2 % Grown

The README.md

microdata-dom.php

license build coverage loc, (*1)

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., (*2)

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..., (*3)

$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..., (*4)

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..., (*5)

$ 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..., (*6)

$ phpunit

Alternatively, have your Concourse execute the tests with your local bits..., (*7)

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

Builds are publicly accessible on Travis CI and internally with [Concourse][8]., (*8)

References

You might find these specifications useful..., (*9)

License

MIT License, (*10)

The Versions

04/04 2016

dev-master

9999999-dev

A library extending the PHP DOMDocument to support the Microdata DOM API.

  Sources   Download

MIT

structured-data microdata