/** * 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 EntityMetadata $entityMetadata * @param string $propertyName * @param string[] $propertyPath */ protected function addLinkedProperty(EntityMetadata $entityMetadata, $propertyName, array $propertyPath) { $linkedProperty = array_pop($propertyPath); $classMetadata = $this->doctrineHelper->findEntityMetadataByPath($entityMetadata->getClassName(), $propertyPath); if (null !== $classMetadata) { if ($classMetadata->hasAssociation($linkedProperty)) { $associationMetadata = $this->entityMetadataFactory->createAssociationMetadata($classMetadata, $linkedProperty); $associationMetadata->setName($propertyName); $entityMetadata->addAssociation($associationMetadata); } else { $fieldMetadata = $this->entityMetadataFactory->createFieldMetadata($classMetadata, $linkedProperty); $fieldMetadata->setName($propertyName); $entityMetadata->addField($fieldMetadata); } } }
/** * @param mixed $object * @param EntityMetadata|null $metadata * * @return array */ protected function handleObject($object, EntityMetadata $metadata = null) { $result = []; if (null === $metadata) { $result[self::ATTRIBUTES] = $this->objectAccessor->toArray($object); } else { $className = $this->objectAccessor->getClassName($object); $entityType = $className ? $this->getEntityType($className, $metadata->getClassName()) : $this->getEntityType($metadata->getClassName()); $result = $this->getResourceIdObject($entityType, $this->entityIdAccessor->getEntityId($object, $metadata)); $data = $this->objectAccessor->toArray($object); foreach ($data as $name => $value) { if (in_array($name, $metadata->getIdentifierFieldNames(), true)) { continue; } if ($metadata->hasAssociation($name)) { $associationMetadata = $metadata->getAssociation($name); $result[self::RELATIONSHIPS][$name][self::DATA] = $associationMetadata->isCollection() ? $this->handleRelatedCollection($value, $associationMetadata) : $this->handleRelatedObject($value, $associationMetadata); } else { $result[self::ATTRIBUTES][$name] = $value; } } } return $result; }