dpb587/microdata-dom

此包已被废弃且不再维护。未建议替代包。

一个扩展PHP DOMDocument以支持Microdata DOM API的库。

dev-master 2016-04-04 05:09 UTC

This package is not auto-updated.

Last update: 2022-03-19 04:03:18 UTC


README

license build coverage loc

此库扩展了原生 PHP DOMDocument,提供了由 MicrodataDOM API 规范描述的方法,以支持查找微数据项及其属性的描述。它在测试中得到很好的覆盖,并试图在遍历DOM时尽可能高效。

使用方法

对于带有微数据的文档,使用 MicrodataDOM\DOMDocument。它的工作方式就像一个常规的 DOMDocument,但具有额外的微数据访问器。以下示例...

$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";

将输出类似...

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...}

安装

您可以通过 composer 安装此库...

$ composer install dpb587/microdata-dom

开发

您可以在 src 中找到运行时代码,在 tests/src 中找到测试代码。如果您正在做更改,您应该在运行测试之前已经安装了 PHPUnit...

$ phpunit

或者,让您的Concourse使用本地代码执行测试...

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

构建在 Travis CI 上公开可访问,并在内部使用 Concourse

参考

您可能会发现以下规范很有用...

许可证

MIT 许可证