/** * 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; }
/** * Checks whether a given object has only identity property(s) * or any other properties as well. * * @param array $object * @param EntityMetadata $metadata * * @return bool */ protected function isIdentity(array $object, EntityMetadata $metadata) { $idFields = $metadata->getIdentifierFieldNames(); return count($object) === count($idFields) && count(array_diff_key($object, array_flip($idFields))) === 0; }