/** * @param Graph $graph * @return array */ public static function findProperties(Graph $graph) { $properties = array(); foreach ($graph->getNodesByType(HydraClassFactory::IRI_SUPPORTED_PROPERTY) as $node) { $properties[] = HydraPropertyFactory::fromNode($node); } return $properties; }
/** * Tests the serialization of graphs */ public function testSerializeGraph() { // This is the expanded and flattened version of the test document // (the blank node labels have been renamed from _:t... to _:b...) $expected = Processor::loadDocument('[{ "@id": "_:b0", "http://vocab.com/nested": [{ "@value": "1.1" }] }, { "@id": "_:b1", "http://vocab.com/nested": [{ "@value": "2.1" }] }, { "@id": "_:b2", "http://vocab.com/nested": [{ "@value": "2.2" }] }, { "@id": "_:b3", "http://vocab.com/nested": [{ "@value": "3.1" }] }, { "@id": "http://example.com/node/1", "@type": ["http://vocab.com/type/node"], "http://vocab.com/contains": [{ "@id": "_:b0" }], "http://vocab.com/link": [{ "@id": "http://example.com/node/2" }], "http://vocab.com/name": [{ "@value": "1" }] }, { "@id": "http://example.com/node/2", "@type": ["http://vocab.com/type/nodeWithAliases"], "http://vocab.com/aliases": [{ "@value": "node2" }, { "@value": 2, "@type": "http://www.w3.org/2001/XMLSchema#integer" }], "http://vocab.com/contains": [{ "@id": "_:b1" }, { "@id": "_:b2" }], "http://vocab.com/lang": [{ "@language": "en", "@value": "language-tagged string" }], "http://vocab.com/link": [{ "@id": "http://example.com/node/3" }], "http://vocab.com/name": [{ "@value": "2" }], "http://vocab.com/typed": [{ "@type": "http://vocab.com/type/datatype", "@value": "typed value" }] }, { "@id": "http://example.com/node/3", "@type": ["http://vocab.com/type/node"], "http://vocab.com/contains": [{ "@id": "_:b3" }], "http://vocab.com/lang": [{ "@language": "en", "@value": "language-tagged string: en" }, { "@language": "de", "@value": "language-tagged string: de" }], "http://vocab.com/link": [{ "@id": "http://example.com/node/1" }], "http://vocab.com/name": [{ "@value": "3" }], "http://vocab.com/typed": [{ "@type": "http://vocab.com/type/datatype", "@value": "typed value" }, { "@language": "ex:/type/otherDataType", "@value": "typed value" }, { "@language": "ex:/type/datatype", "@value": "typed value" }] }, { "@id": "http://vocab.com/type/node" }, { "@id": "http://vocab.com/type/nodeWithAliases" }]'); $this->assertEquals($expected, $this->graph->toJsonLd(false), 'Serialize graph'); }
/** * @param \EasyRdf\Graph $graph * @param string $format * @param array $options * * @throws Exception * @return string */ public function serialise($graph, $format, array $options = array()) { parent::checkSerialiseParams($graph, $format); if ($format != 'jsonld') { throw new Exception(__CLASS__ . ' does not support: ' . $format); } $ld_graph = new LD\Graph(); $nodes = array(); // cache for id-to-node association foreach ($graph->toRdfPhp() as $resource => $properties) { if (array_key_exists($resource, $nodes)) { $node = $nodes[$resource]; } else { $node = $ld_graph->createNode($resource); $nodes[$resource] = $node; } foreach ($properties as $property => $values) { foreach ($values as $value) { if ($value['type'] == 'bnode' or $value['type'] == 'uri') { if (array_key_exists($value['value'], $nodes)) { $_value = $nodes[$value['value']]; } else { $_value = $ld_graph->createNode($value['value']); $nodes[$value['value']] = $_value; } } elseif ($value['type'] == 'literal') { if (isset($value['lang'])) { $_value = new LD\LanguageTaggedString($value['value'], $value['lang']); } elseif (isset($value['datatype'])) { $_value = new LD\TypedValue($value['value'], $value['datatype']); } else { $_value = $value['value']; } } else { throw new Exception("Unable to serialise object to JSON-LD: " . $value['type']); } if ($property == "http://www.w3.org/1999/02/22-rdf-syntax-ns#type") { $node->addType($_value); } else { $node->addPropertyValue($property, $_value); } } } } // OPTIONS $use_native_types = !(isset($options['expand_native_types']) and $options['expand_native_types'] == true); $should_compact = (isset($options['compact']) and $options['compact'] == true); $should_frame = isset($options['frame']); // expanded form $data = $ld_graph->toJsonLd($use_native_types); if ($should_frame) { $data = LD\JsonLD::frame($data, $options['frame'], $options); } if ($should_compact) { // compact form $compact_context = isset($options['context']) ? $options['context'] : null; $compact_options = array('useNativeTypes' => $use_native_types, 'base' => $graph->getUri()); $data = LD\JsonLD::compact($data, $compact_context, $compact_options); } return LD\JsonLD::toString($data); }
/** * Tests whether it is possible to add an type which is not part of the * graph * * @expectedException InvalidArgumentException */ public function testAddTypeNotInGraph() { $graph = new Graph(); $newType = $graph->createNode(); $node1 = $this->graph->getNode('http://example.com/node/1'); $node1->addType($newType); }