conjecto/json-ld

PHP JSON-LD 处理器

1.0.3 2014-09-15 10:41 UTC

README

JsonLD 是一个完全符合规范的 PHP 编写的 JSON-LD 处理器。它经过广泛测试,并通过了官方 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());

商业支持

可提供商业支持,请提出请求。