コード例 #1
0
 /**
  * @param ClassMetadata $classMetadata
  *
  * @return EntityMetadata
  */
 public function createEntityMetadata(ClassMetadata $classMetadata)
 {
     $entityMetadata = new EntityMetadata();
     $entityMetadata->setClassName($classMetadata->name);
     $entityMetadata->setIdentifierFieldNames($classMetadata->getIdentifierFieldNames());
     return $entityMetadata;
 }
コード例 #2
0
 /**
  * @param ClassMetadata $classMetadata
  *
  * @return EntityMetadata
  */
 public function createEntityMetadata(ClassMetadata $classMetadata)
 {
     $entityMetadata = new EntityMetadata();
     $entityMetadata->setClassName($classMetadata->name);
     $entityMetadata->setIdentifierFieldNames($classMetadata->getIdentifierFieldNames());
     $entityMetadata->setInheritedType($classMetadata->inheritanceType !== ClassMetadata::INHERITANCE_TYPE_NONE);
     return $entityMetadata;
 }
コード例 #3
0
 /**
  * @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);
         }
     }
 }
コード例 #4
0
 /**
  * @param EntityMetadata  $entityMetadata
  * @param array           $config
  * @param MetadataContext $context
  */
 protected function loadMetadataForRelatedEntities(EntityMetadata $entityMetadata, array $config, MetadataContext $context)
 {
     $associations = $entityMetadata->getAssociations();
     foreach ($associations as $associationName => $association) {
         if (null !== $association->getTargetMetadata()) {
             // metadata for an associated entity is already loaded
             continue;
         }
         if (!isset($config[ConfigUtil::FIELDS][$associationName][ConfigUtil::FIELDS])) {
             // a configuration of an association fields does not exist
             continue;
         }
         $relatedEntityMetadata = $this->metadataProvider->getMetadata($association->getTargetClassName(), $context->getVersion(), $context->getRequestType(), $context->getExtras(), $config[ConfigUtil::FIELDS][$associationName]);
         if (null !== $relatedEntityMetadata) {
             $association->setTargetMetadata($relatedEntityMetadata);
         }
     }
 }
コード例 #5
0
 /**
  * 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;
 }
コード例 #6
0
 /**
  * @param EntityMetadata $entityMetadata
  * @param array          $config
  */
 protected function normalizeMetadata(EntityMetadata $entityMetadata, array $config)
 {
     $fields = ConfigUtil::getArrayValue($config, ConfigUtil::FIELDS);
     foreach ($fields as $fieldName => $fieldConfig) {
         if (null === $fieldConfig) {
             continue;
         }
         if (ConfigUtil::isExclude($fieldConfig)) {
             $entityMetadata->removeProperty($fieldName);
         } elseif (isset($fieldConfig[ConfigUtil::PROPERTY_PATH])) {
             $path = ConfigUtil::explodePropertyPath($fieldConfig[ConfigUtil::PROPERTY_PATH]);
             if (count($path) === 1) {
                 $entityMetadata->renameProperty(reset($path), $fieldName);
             }
         }
     }
     if (ConfigUtil::isExcludeAll($config)) {
         $toRemoveFieldNames = array_diff(array_merge(array_keys($entityMetadata->getFields()), array_keys($entityMetadata->getAssociations())), array_keys($fields));
         foreach ($toRemoveFieldNames as $fieldName) {
             $entityMetadata->removeProperty($fieldName);
         }
     }
 }
コード例 #7
0
 /**
  * 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;
 }