Beispiel #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;
 }
 public function testExtractTwiceGivenSameEntityShouldProcessExtractionOnceAndReturnSameData()
 {
     $entity = new TestAsset\Entity('foo', 'Foo Bar');
     $entityHydratorManager = $this->getMockBuilder('ZF\\Hal\\EntityHydratorManager')->disableOriginalConstructor()->getMock();
     $entityHydratorManager->expects($this->once())->method('getHydratorForEntity')->with($entity)->will($this->returnValue(null));
     $extractor = new EntityExtractor($entityHydratorManager);
     $data1 = $extractor->extract($entity);
     $data2 = $extractor->extract($entity);
     $this->assertSame($data1, $data2);
 }