Esempio n. 1
0
 /**
  * Compact a JSON-LD document according a supplied context
  *
  * In contrast to {@link compact()}, this method assumes that the input
  * has already been expanded.
  *
  * @param array                    $input       The JSON-LD document to
  *                                              compact.
  * @param null|string|object|array $context     The context.
  * @param null|array|object        $options     Options to configure the
  *                                              compaction process.
  * @param bool                     $alwaysGraph If set to true, the resulting
  *                                              document will always explicitly
  *                                              contain the default graph at
  *                                              the top-level.
  *
  * @return object The compacted JSON-LD document.
  *
  * @throws JsonLdException
  */
 private static function doCompact($input, $context = null, $options = null, $alwaysGraph = false)
 {
     if (null !== $context) {
         $context = Processor::loadDocument($context);
     }
     if (is_object($context) && property_exists($context, '@context')) {
         $context = $context->{'@context'};
     }
     if (is_object($context) && 0 === count(get_object_vars($context))) {
         $context = null;
     } elseif (is_array($context) && 0 === count($context)) {
         $context = null;
     }
     $activectx = array('@base' => $options->base);
     $processor = new Processor($options);
     $processor->processContext($context, $activectx);
     $inversectx = $processor->createInverseContext($activectx);
     $processor->compact($input, $activectx, $inversectx);
     $compactedDocument = new Object();
     if (null !== $context) {
         $compactedDocument->{'@context'} = $context;
     }
     if (false === is_array($input) || 0 === count($input)) {
         if (false === $alwaysGraph) {
             $compactedDocument = (object) ((array) $compactedDocument + (array) $input);
             return $compactedDocument;
         }
         if (false === is_array($input)) {
             $input = array($input);
         }
     }
     $graphKeyword = isset($inversectx['@graph']['term']) ? $inversectx['@graph']['term'] : '@graph';
     $compactedDocument->{$graphKeyword} = $input;
     return $compactedDocument;
 }