/** * 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'); }
/** * Merge the passed options with the options' default values. * * @param null|array|object $options The options. * * @return object The merged options. */ private static function mergeOptions($options) { $result = (object) array('base' => null, 'expandContext' => null, 'compactArrays' => true, 'optimize' => false, 'graph' => null, 'useNativeTypes' => false, 'useRdfType' => false, 'produceGeneralizedRdf' => false, 'documentFactory' => null); if (is_array($options) || is_object($options)) { $options = (object) $options; if (isset($options->{'base'})) { if (is_string($options->{'base'})) { $result->base = new IRI($options->{'base'}); } elseif ($options->{'base'} instanceof IRI && $options->{'base'}->isAbsolute()) { $result->base = clone $options->{'base'}; } else { throw \InvalidArgumentException('The "base" option must be set to null or an absolute IRI.'); } } if (property_exists($options, 'expandContext')) { if (is_string($options->expandContext)) { $result->expandContext = Processor::loadDocument($options->expandContext); } elseif (is_object($options->expandContext)) { $result->expandContext = $options->expandContext; } if (is_object($result->expandContext) && property_exists($result->expandContext, '@context')) { $result->expandContext = $result->expandContext->{'@context'}; } } if (property_exists($options, 'compactArrays') && is_bool($options->compactArrays)) { $result->compactArrays = $options->compactArrays; } if (property_exists($options, 'optimize') && is_bool($options->optimize)) { $result->optimize = $options->optimize; } if (property_exists($options, 'graph') && is_string($options->graph)) { $result->graph = $options->graph; } if (property_exists($options, 'useNativeTypes') && is_bool($options->useNativeTypes)) { $result->useNativeTypes = $options->useNativeTypes; } if (property_exists($options, 'useRdfType') && is_bool($options->useRdfType)) { $result->useRdfType = $options->useRdfType; } if (property_exists($options, 'produceGeneralizedRdf') && is_bool($options->produceGeneralizedRdf)) { $result->produceGeneralizedRdf = $options->produceGeneralizedRdf; } if (property_exists($options, 'documentFactory') && $options->documentFactory instanceof DocumentFactoryInterface) { $result->documentFactory = $options->documentFactory; } } return $result; }