Exemplo n.º 1
0
 /**
  * Create a entity and/or collection based on a metadata map
  *
  * @param  object $object
  * @param  Metadata $metadata
  * @param  bool $renderEmbeddedEntities
  * @return Entity|Collection
  * @throws Exception\RuntimeException
  */
 public function createEntityFromMetadata($object, Metadata $metadata, $renderEmbeddedEntities = true)
 {
     if ($metadata->isCollection()) {
         return $this->createCollectionFromMetadata($object, $metadata);
     }
     $data = $this->entityExtractor->extract($object);
     $entityIdentifierName = $metadata->getEntityIdentifierName();
     if ($entityIdentifierName && !isset($data[$entityIdentifierName])) {
         throw new Exception\RuntimeException(sprintf('Unable to determine entity identifier for object of type "%s"; no fields matching "%s"', get_class($object), $entityIdentifierName));
     }
     $id = $entityIdentifierName ? $data[$entityIdentifierName] : null;
     if (!$renderEmbeddedEntities) {
         if ($id) {
             $object = ['id' => $id];
         } else {
             $object = [];
         }
     }
     $halEntity = new Entity($object, $id);
     $links = $halEntity->getLinks();
     $this->marshalMetadataLinks($metadata, $links);
     $forceSelfLink = $metadata->getForceSelfLink();
     if ($forceSelfLink && !$links->has('self')) {
         $link = $this->marshalLinkFromMetadata($metadata, $object, $id, $metadata->getRouteIdentifierName());
         $links->add($link);
     }
     return $halEntity;
 }
Exemplo n.º 2
0
 /**
  * Create a entity and/or collection based on a metadata map
  *
  * @param  object $object
  * @param  Metadata $metadata
  * @return Entity|Collection
  */
 public function createEntityFromMetadata($object, Metadata $metadata, $renderEmbeddedEntities = true)
 {
     if ($metadata->isCollection()) {
         return $this->createCollectionFromMetadata($object, $metadata);
     }
     $data = $this->convertEntityToArray($object);
     $entityIdentifierName = $metadata->getEntityIdentifierName();
     if ($entityIdentifierName and !isset($data[$entityIdentifierName])) {
         throw new Exception\RuntimeException(sprintf('Unable to determine entity identifier for object of type "%s"; no fields matching "%s"', get_class($object), $entityIdentifierName));
     }
     $id = $entityIdentifierName ? $data[$entityIdentifierName] : null;
     if (!$renderEmbeddedEntities) {
         $data = array();
     }
     $entity = new Entity($data, $id);
     $links = $entity->getLinks();
     $this->marshalMetadataLinks($metadata, $links);
     if (!$links->has('self')) {
         $link = $this->marshalSelfLinkFromMetadata($metadata, $object, $id, $metadata->getRouteIdentifierName());
         $links->add($link);
     }
     return $entity;
 }