k00ni / json-ld
PHP JSON-LD 处理器
1.2.1
2022-08-22 13:41 UTC
Requires (Dev)
- json-ld/tests: 1.0
- phpunit/phpunit: ^4
This package is auto-updated.
Last update: 2024-09-03 08:19:22 UTC
README
JsonLD 是一个完全符合标准的 JSON-LD 处理器,用 PHP 编写。它经过广泛测试并通过了 官方 JSON-LD 测试套件。
还有一个 在线沙盒,您可以在其中评估处理器的基本功能。
除了 JSON-LD API 规范 中定义的功能之外,JsonLD 还支持 framing(包括 值匹配、深度过滤、激进重嵌入 和 命名图)以及一个实验性的 面向对象的 JSON-LD 文档接口。
安装
安装 JsonLD
最简单的方法是通过 Composer 需求它。
composer require ml/json-ld
... 并将 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());
商业支持
如有需要,可提供商业支持。