Example #1
0
 /**
  * Serialize resource to JsonLD frame
  * @param $resource
  * @param null $frame
  * @param array $options
  * @return array
  */
 public function serialize($resource, $frame = null, $options = array())
 {
     $frame = $frame ? $frame : $this->frame;
     $options = $options ? $options : $this->options;
     $parentClasses = array();
     $typeResource = $this->typeMapperRegistry->getRdfClass(get_class($resource));
     if (!$frame) {
         $metadata = $this->metadataFactory->getMetadataForClass(get_class($resource));
         // if no frame provided try to find the default one in the resource metadata
         if (!$frame) {
             $frame = $metadata->getFrame();
         }
     }
     // load the frame
     $frame = $this->loadFrame($frame, $typeResource, isset($options['includeParentClassFrame']) && $options['includeParentClassFrame'] === true);
     // if compacting without context, extract it from the frame
     if ($frame && !empty($options['compact']) && empty($options['context']) && isset($frame['@context'])) {
         $options['context'] = $frame['@context'];
     }
     // if the $data is a resource, add the @id in the frame
     if ($resource instanceof Resource && $frame && !isset($frame['@id'])) {
         $frame['@id'] = $resource->getUri();
     }
     // get the graph form the GraphProvider
     $graph = $this->provider->getGraph($resource, $frame);
     $options['frame'] = json_encode($frame, JSON_FORCE_OBJECT);
     return $graph->serialise('jsonld', $options);
 }
 /**
  * Transform an elastica document to a resource.
  *
  * @param Document $document
  *
  * @return Resource|null
  */
 public function reverseTransform(Document $document)
 {
     if ($document) {
         $uri = $document->getParam('_id');
         $type = $document->getParam('_type');
         $graph = $this->serializerHelper->getGraph($document->getParam('_index'), $uri, $type);
         $phpClass = $this->typeMapperRegistry->get($type);
         if ($phpClass) {
             return new $phpClass($uri, $graph);
         }
         return new Resource($uri, $graph);
     }
     return;
 }