/** * Returns a string representation of the identifier of a given entity. * * @param mixed $entity * @param EntityMetadata $metadata * * @return string */ public function getEntityId($entity, EntityMetadata $metadata) { $result = null; $idFieldNames = $metadata->getIdentifierFieldNames(); $idFieldNamesCount = count($idFieldNames); if ($idFieldNamesCount === 1) { $fieldName = reset($idFieldNames); if (!$this->objectAccessor->hasProperty($entity, $fieldName)) { throw new \RuntimeException(sprintf('An object of the type "%s" does not have the identifier property "%s".', $metadata->getClassName(), $fieldName)); } $result = $this->entityIdTransformer->transform($this->objectAccessor->getValue($entity, $fieldName)); } elseif ($idFieldNamesCount > 1) { $id = []; foreach ($idFieldNames as $fieldName) { if (!$this->objectAccessor->hasProperty($entity, $fieldName)) { throw new \RuntimeException(sprintf('An object of the type "%s" does not have the identifier property "%s".', $metadata->getClassName(), $fieldName)); } $id[$fieldName] = $this->objectAccessor->getValue($entity, $fieldName); } $result = $this->entityIdTransformer->transform($id); } else { throw new \RuntimeException(sprintf('The "%s" entity does not have an identifier.', $metadata->getClassName())); } if (empty($result)) { throw new \RuntimeException(sprintf('The identifier value for "%s" entity must not be empty.', $metadata->getClassName())); } return $result; }
/** * @param mixed $object * @param AssociationMetadata $associationMetadata * * @return array The resource identifier */ protected function processRelatedObject($object, AssociationMetadata $associationMetadata) { $targetMetadata = $associationMetadata->getTargetMetadata(); $preparedValue = $this->prepareRelatedValue($object, $associationMetadata->getTargetClassName(), $targetMetadata); if ($preparedValue['idOnly']) { $resourceId = $this->getResourceIdObject($preparedValue['entityType'], $this->entityIdTransformer->transform($preparedValue['value'])); } else { $resourceId = $this->getResourceIdObject($preparedValue['entityType'], $this->entityIdAccessor->getEntityId($preparedValue['value'], $targetMetadata)); $this->addRelatedObject($preparedValue['value'], $targetMetadata); } return $resourceId; }