1<?php
2
3/**
4 * EasyRdf
5 *
6 * LICENSE
7 *
8 * Copyright (c) 2009-2013 Nicholas J Humfrey.  All rights reserved.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions are met:
12 * 1. Redistributions of source code must retain the above copyright
13 *    notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright notice,
15 *    this list of conditions and the following disclaimer in the documentation
16 *    and/or other materials provided with the distribution.
17 * 3. The name of the author 'Nicholas J Humfrey" may be used to endorse or
18 *    promote products derived from this software without specific prior
19 *    written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
25 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 * POSSIBILITY OF SUCH DAMAGE.
32 *
33 * @package    EasyRdf
34 * @copyright  Copyright (c) 2009-2013 Nicholas J Humfrey
35 * @license    http://www.opensource.org/licenses/bsd-license.php
36 */
37
38/**
39 * Class to serialise an EasyRdf_Graph to JSON-LD
40 *
41 * @package    EasyRdf
42 * @copyright  Copyright (c) 2013 Alexey Zakhlestin
43 * @license    http://www.opensource.org/licenses/bsd-license.php
44 */
45class EasyRdf_Serialiser_JsonLd extends EasyRdf_Serialiser
46{
47    public function __construct()
48    {
49        if (!class_exists('\ML\JsonLD\JsonLD')) {
50            throw new LogicException('Please install "ml/json-ld" dependency to use JSON-LD serialisation');
51        }
52
53        parent::__construct();
54    }
55
56    /**
57     * @param EasyRdf_Graph $graph
58     * @param string        $format
59     * @param array         $options
60     * @throws EasyRdf_Exception
61     * @return string
62     */
63    public function serialise($graph, $format, array $options = array())
64    {
65        parent::checkSerialiseParams($graph, $format);
66
67        if ($format != 'jsonld') {
68            throw new EasyRdf_Exception(__CLASS__.' does not support: '.$format);
69        }
70
71
72        $ld_graph = new \ML\JsonLD\Graph();
73        $nodes = array(); // cache for id-to-node association
74
75        foreach ($graph->toRdfPhp() as $resource => $properties) {
76            if (array_key_exists($resource, $nodes)) {
77                $node = $nodes[$resource];
78            } else {
79                $node = $ld_graph->createNode($resource);
80                $nodes[$resource] = $node;
81            }
82
83            foreach ($properties as $property => $values) {
84                foreach ($values as $value) {
85                    if ($value['type'] == 'bnode' or $value['type'] == 'uri') {
86                        if (array_key_exists($value['value'], $nodes)) {
87                            $_value = $nodes[$value['value']];
88                        } else {
89                            $_value = $ld_graph->createNode($value['value']);
90                            $nodes[$value['value']] = $_value;
91                        }
92                    } elseif ($value['type'] == 'literal') {
93                        if (isset($value['lang'])) {
94                            $_value = new \ML\JsonLD\LanguageTaggedString($value['value'], $value['lang']);
95                        } elseif (isset($value['datatype'])) {
96                            $_value = new \ML\JsonLD\TypedValue($value['value'], $value['datatype']);
97                        } else {
98                            $_value = $value['value'];
99                        }
100                    } else {
101                        throw new EasyRdf_Exception(
102                            "Unable to serialise object to JSON-LD: ".$value['type']
103                        );
104                    }
105
106                    if ($property == "http://www.w3.org/1999/02/22-rdf-syntax-ns#type") {
107                        $node->addType($_value);
108                    } else {
109                        $node->addPropertyValue($property, $_value);
110                    }
111                }
112            }
113        }
114
115        // OPTIONS
116        $use_native_types = !(isset($options['expand_native_types']) and $options['expand_native_types'] == true);
117        $should_compact = (isset($options['compact']) and $options['compact'] == true);
118        $should_frame = isset($options['frame']);
119
120        // expanded form
121        $data = $ld_graph->toJsonLd($use_native_types);
122
123        if ($should_frame) {
124            $data = \ML\JsonLD\JsonLD::frame($data, $options['frame'], $options);
125
126        }
127
128        if ($should_compact) {
129            // compact form
130            $compact_context = isset($options['context']) ? $options['context'] : null;
131            $compact_options = array(
132                'useNativeTypes' => $use_native_types,
133                'base' => $graph->getUri()
134            );
135
136            $data = \ML\JsonLD\JsonLD::compact($data, $compact_context, $compact_options);
137        }
138
139        return \ML\JsonLD\JsonLD::toString($data);
140    }
141}
142