Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
80.00% covered (warning)
80.00%
4 / 5
75.00% covered (warning)
75.00%
3 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
JsonLd
80.00% covered (warning)
80.00%
4 / 5
75.00% covered (warning)
75.00%
3 / 4
5.20
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 expand
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
2
 compact
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 compactToJson
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3declare(strict_types=1);
4
5namespace LambdaTwelve\OneRecord\JsonLd;
6
7use LambdaTwelve\OneRecord\Rdf\BlankNode;
8use LambdaTwelve\OneRecord\Rdf\Graph;
9use LambdaTwelve\OneRecord\Rdf\Iri;
10
11/**
12 * Entry points for the JSON-LD subset: text or decoded array in, graph out,
13 * and back.
14 */
15final class JsonLd
16{
17    private function __construct() {}
18
19    /**
20     * @param string|array<string, mixed> $document
21     * @param Iri|list<Iri>|null $preferredRoot the node(s) to root a flattened document at, in order of preference
22     */
23    public static function expand(string|array $document, Iri|array|null $preferredRoot = null): ExpandedDocument
24    {
25        $decoded = \is_string($document) ? Json::decodeObject($document) : $document;
26
27        return (new Expander())->expand($decoded, $preferredRoot);
28    }
29
30    /**
31     * @return array<string, mixed>
32     */
33    public static function compact(Graph $graph, Iri|BlankNode $root, ?Context $context = null): array
34    {
35        return (new Writer())->write($graph, $root, $context ?? Context::oneRecord());
36    }
37
38    public static function compactToJson(Graph $graph, Iri|BlankNode $root, ?Context $context = null, bool $pretty = true): string
39    {
40        return Json::encode(self::compact($graph, $root, $context), $pretty);
41    }
42}