michaelstivala / json-ld
PHP JSON-LD 处理器
1.1.0
2018-07-03 15:27 UTC
Requires (Dev)
- json-ld/tests: 1.0
- phpunit/phpunit: ^4
This package is auto-updated.
Last update: 2024-09-20 08:17:17 UTC
README
JsonLD 是一个完全符合规范的 JSON-LD 处理器,使用 PHP 编写。它经过了广泛测试,并通过了 官方 JSON-LD 测试套件。
有一个在线 游乐场,您可以评估处理器的基本功能。
除了由 JSON-LD API 规范 定义的功能外,JsonLD 还支持 框架(包括 值匹配、深度过滤、激进重新嵌入 和 命名图)以及一个实验性的 面向对象的 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());
商业支持
如有需要,可提供商业支持。