powerkernel / json-ld
PHP 的 JSON-LD 处理器
1.0.9
2018-04-19 17:43 UTC
Requires (Dev)
- json-ld/tests: 1.0
- phpunit/phpunit: ^4
This package is auto-updated.
Last update: 2024-09-11 02:06:46 UTC
README
JsonLD 是一个完全符合标准的 PHP 编写的 JSON-LD 处理器。它经过了广泛的测试,并通过了 官方 JSON-LD 测试套件。
还有一个 在线沙盒,您可以在此处评估处理器的基本功能。
除了由 JSON-LD API 规范 定义的特性之外,JsonLD 还支持 framing(包括 值匹配、深度筛选、积极重嵌入 和 命名图)以及一个实验性的 面向对象的 JSON-LD 文档接口。
安装
使用 JsonLD 的最简单方法是将它作为依赖项集成到您的项目的 composer.json 文件中
{ "require": { "ml/json-ld": "1.*" } }
然后只需运行 composer 即可安装
php composer.phar install
... 并将 Composer 的自动加载器包含到您的项目中
require('vendor/autoload.php');
当然,您也可以从 Github 下载 JsonLD 的 ZIP 归档。
JsonLD 需要 PHP 5.3 或更高版本。
使用方法
该库支持官方的 JSON-LD API 以及 JSON-LD 文档的面向对象接口(尚未完全实现,请参阅 问题 #15 获取详细信息)。
所有类都有详细的文档。请查看源代码。
// Official JSON-LD API $expanded = JsonLD::expand('document.jsonld'); $compacted = JsonLD::compact('document.jsonld', 'context.jsonld'); $framed = JsonLD::frame('document.jsonld', 'frame.jsonld'); $flattened = JsonLD::flatten('document.jsonld'); $quads = JsonLD::toRdf('document.jsonld'); // Output the expanded document (pretty print) print JsonLD::toString($expanded, true); // Serialize the quads as N-Quads $nquads = new NQuads(); $serialized = $nquads->serialize($quads); print $serialized; // And parse them again to a JSON-LD document $quads = $nquads->parse($serialized); $document = JsonLD::fromRdf($quads); print JsonLD::toString($document, true); // Node-centric API $doc = JsonLD::getDocument('document.jsonld'); // get the default graph $graph = $doc->getGraph(); // get all nodes in the graph $nodes = $graph->getNodes(); // retrieve a node by ID $node = $graph->getNode('http://example.com/node1'); // get a property $node->getProperty('http://example.com/vocab/name'); // add a new blank node to the graph $newNode = $graph->createNode(); // link the new blank node to the existing node $node->addPropertyValue('http://example.com/vocab/link', $newNode); // even reverse properties are supported; this returns $newNode $node->getReverseProperty('http://example.com/vocab/link'); // serialize the graph and convert it to a string $serialized = JsonLD::toString($graph->toJsonLd());
商业支持
如有需求,可提供商业支持。