/**
  * @see EntityHolder::getEntityId
  *
  * This implements lazy deserialization of the blob passed to the constructor.
  *
  * @param string $expectedClass The class with which the result is expected to be compatible.
  * Defaults to EntityDocument.
  *
  * @throws RuntimeException If the entity held by this EntityHolder is not compatible with $expectedClass.
  * @return EntityDocument
  */
 public function getEntity($expectedClass = 'Wikibase\\DataModel\\Entity\\EntityDocument')
 {
     if (!$this->entity) {
         $this->entity = $this->codec->decodeEntity($this->blob, $this->contentFormat);
         if (!$this->entity instanceof EntityDocument) {
             throw new RuntimeException('Deferred decoding resulted in an incompatible entity, ' . 'expected EntityDocument, got ' . gettype($this->entity));
         } elseif ($this->entity->getType() !== $this->entityType) {
             throw new RuntimeException('Deferred decoding resulted in an incompatible entity, ' . 'expected ' . $this->entityType . ', got ' . $this->entity->getType());
         } elseif ($this->entity->getId() === null) {
             throw new RuntimeException('Deferred decoding resulted in an incompatible entity, ' . 'expected an entity id to be set, got null');
         } elseif ($this->entityId && !$this->entity->getId()->equals($this->entityId)) {
             throw new RuntimeException('Deferred decoding resulted in an incompatible entity, ' . 'expected ' . $this->entityId . ', got ' . $this->entity->getId());
         }
     }
     if (!$this->entity instanceof $expectedClass) {
         throw new RuntimeException('Deferred decoding resulted in an incompatible entity, ' . 'expected ' . $expectedClass . ', got ' . get_class($this->entity));
     }
     return $this->entity;
 }
 /**
  * Construct an EntityRevision object from a database row from the revision and text tables.
  *
  * @see loadEntityBlob()
  *
  * @param object $row a row object as expected Revision::getRevisionText(). That is, it
  *        should contain the relevant fields from the revision and/or text table.
  *
  * @throws MWContentSerializationException
  * @return object[] list( EntityRevision|null $entityRevision, EntityRedirect|null $entityRedirect )
  * with either $entityRevision or $entityRedirect or both being null (but not both being non-null).
  */
 private function loadEntity($row)
 {
     $blob = $this->loadEntityBlob($row);
     $entity = $this->contentCodec->decodeEntity($blob, $row->rev_content_format);
     if ($entity) {
         $entityRevision = new EntityRevision($entity, (int) $row->rev_id, $row->rev_timestamp);
         $result = array($entityRevision, null);
     } else {
         $redirect = $this->contentCodec->decodeRedirect($blob, $row->rev_content_format);
         if (!$redirect) {
             throw new MWContentSerializationException('The serialized data contains neither an Entity nor an EntityRedirect!');
         }
         $result = array(null, $redirect);
     }
     return $result;
 }