/** * Load a JSON-LD document * * The document can be supplied directly as string, by passing a file * path, or by passing a URL. * * Usage: * <code> * $document = Processor::loadDocument('document.jsonld'); * print_r($document); * </code> * * @param null|string|array|object $input The JSON-LD document or a path * or URL pointing to one. * * @return mixed The loaded JSON-LD document * * @throws JsonLdException */ public static function loadDocument($input) { if (false === is_string($input)) { // Return as is - it has already been parsed return $input; } $document = FileGetContentsLoader::loadDocument($input); return $document->document; }
/** * Expand a JSON-LD document * * The document can be supplied directly as string, by passing a file * path, or by passing a URL. * * Usage: * * $expanded = JsonLD::expand('document.jsonld'); * print_r($expanded); * * It is possible to configure the expansion process by setting the options * parameter accordingly. Available options are: * * <dl> * <dt>base</dt> * <dd>The base IRI of the input document.</dd> * * <dt>expandContext</dt> * <dd>An optional context to use additionally to the context embedded * in input when expanding the input.</dd> * </dl> * * The options parameter might be passed as associative array or as * object. * * @param string|object|array $input The JSON-LD document to expand. * @param null|array|object $options Options to configure the expansion * process. * * @return array The expanded JSON-LD document. * * @throws JsonLdException * * @api */ public static function expand($input, $options = null) { $options = self::mergeOptions($options); $processor = new Processor($options); $activectx = array('@base' => null); if (is_string($input)) { $remoteDocument = FileGetContentsLoader::loadDocument($input); $input = $remoteDocument->document; $activectx['@base'] = new IRI($remoteDocument->documentUrl); if (null !== $remoteDocument->contextUrl) { $processor->processContext($remoteDocument->contextUrl, $activectx); } } if ($options->base) { $activectx['@base'] = $options->base; } if (null !== $options->expandContext) { $processor->processContext($options->expandContext, $activectx); } $processor->expand($input, $activectx); // optimize away default graph (@graph as the only property at the top-level object) if (is_object($input) && property_exists($input, '@graph') && 1 === count(get_object_vars($input))) { $input = $input->{'@graph'}; } if (false === is_array($input)) { $input = null === $input ? array() : array($input); } return $input; }