コード例 #1
0
 /**
  * {@inheritdoc}
  */
 public function process(ContextInterface $context)
 {
     /** @var GetListContext $context */
     if ($context->hasQuery()) {
         // a query is already built
         return;
     }
     $entityClass = $context->getClassName();
     if (!$this->doctrineHelper->isManageableEntityClass($entityClass)) {
         // only manageable entities are supported
         return;
     }
     $filters = $context->getFilters();
     if (!$filters->has(self::SORT_FILTER_KEY)) {
         $filters->add(self::SORT_FILTER_KEY, new SortFilter(DataType::ORDER_BY, 'Result sorting. One or several fields separated by comma, for example \'field1,-field2\'.', function () use($entityClass) {
             return $this->doctrineHelper->getOrderByIdentifier($entityClass);
         }, function ($value) {
             $result = [];
             if (null !== $value) {
                 foreach ($value as $field => $order) {
                     $result[] = (Criteria::DESC === $order ? '-' : '') . $field;
                 }
             }
             return implode(RestRequest::ARRAY_DELIMITER, $result);
         }));
     }
 }
コード例 #2
0
 /**
  * @param array  $definition
  * @param string $entityClass
  *
  * @return bool
  */
 protected function updateRelations(array &$definition, $entityClass)
 {
     $hasChanges = false;
     $metadata = $this->doctrineHelper->getEntityMetadataForClass($entityClass);
     foreach ($definition[ConfigUtil::FIELDS] as $fieldName => &$fieldConfig) {
         if (!is_array($fieldConfig) || empty($fieldConfig[ConfigUtil::DEFINITION][ConfigUtil::FIELDS])) {
             continue;
         }
         $fieldDefinition = $fieldConfig[ConfigUtil::DEFINITION];
         $propertyPath = ConfigUtil::getPropertyPath($fieldDefinition, $fieldName);
         if (!$metadata->hasAssociation($propertyPath)) {
             continue;
         }
         $mapping = $metadata->getAssociationMapping($propertyPath);
         $targetMetadata = $this->doctrineHelper->getEntityMetadataForClass($mapping['targetEntity']);
         if ($targetMetadata->inheritanceType === ClassMetadata::INHERITANCE_TYPE_NONE) {
             continue;
         }
         if (!is_array($fieldDefinition[ConfigUtil::FIELDS])) {
             $fieldDefinition[ConfigUtil::FIELDS] = [$fieldDefinition[ConfigUtil::FIELDS] => null];
         }
         $fieldDefinition[ConfigUtil::FIELDS][ConfigUtil::CLASS_NAME] = null;
         $fieldConfig[ConfigUtil::DEFINITION] = $fieldDefinition;
         $hasChanges = true;
     }
     return $hasChanges;
 }
コード例 #3
0
ファイル: SetFieldsFilter.php プロジェクト: Maksold/platform
 /**
  * {@inheritdoc}
  */
 public function process(ContextInterface $context)
 {
     /** @var Context $context */
     $entityClass = $context->getClassName();
     if (!$this->doctrineHelper->isManageableEntityClass($entityClass)) {
         // only manageable entities are supported
         return;
     }
     $filters = $context->getFilters();
     if ($filters->has(self::FILTER_KEY)) {
         // filters have been already set
         return;
     }
     $fieldFilter = new FieldsFilter(DataType::STRING, self::FILTER_KEY_DESCRIPTION);
     $fieldFilter->setArrayAllowed(true);
     $filters->add(sprintf(self::FILTER_KEY_TEMPLATE, $this->entityClassTransformer->transform($entityClass)), $fieldFilter);
     $associations = $this->doctrineHelper->getEntityMetadata($entityClass)->getAssociationMappings();
     if (!$associations) {
         // no associations - no sense to add associations fields filters
         return;
     }
     $associationKeys = array_keys($associations);
     foreach ($associationKeys as $association) {
         $filters->add(sprintf(self::FILTER_KEY_TEMPLATE, $association), $fieldFilter);
     }
 }
コード例 #4
0
 /**
  * @param string[] $result
  * @param string   $entityClass
  * @param bool     $desc
  */
 protected function addEntityIdentifierFieldNames(array &$result, $entityClass, $desc = false)
 {
     $idFieldNames = $this->doctrineHelper->getEntityIdentifierFieldNamesForClass($entityClass);
     foreach ($idFieldNames as $fieldName) {
         $result[] = $desc ? '-' . $fieldName : $fieldName;
     }
 }
コード例 #5
0
 /**
  * @param array  $definition
  * @param string $entityClass
  *
  * @return bool
  */
 protected function updateRelations(array &$definition, $entityClass)
 {
     $hasChanges = false;
     $metadata = $this->doctrineHelper->getEntityMetadataForClass($entityClass);
     foreach ($definition[ConfigUtil::FIELDS] as $fieldName => &$fieldConfig) {
         if (!is_array($fieldConfig) || empty($fieldConfig[ConfigUtil::DEFINITION][ConfigUtil::FIELDS])) {
             continue;
         }
         $fieldDefinition = $fieldConfig[ConfigUtil::DEFINITION];
         if (ConfigUtil::isExclude($fieldDefinition)) {
             continue;
         }
         $propertyPath = ConfigUtil::getPropertyPath($fieldDefinition, $fieldName);
         if (!$metadata->hasAssociation($propertyPath)) {
             continue;
         }
         $mapping = $metadata->getAssociationMapping($propertyPath);
         $targetMetadata = $this->doctrineHelper->getEntityMetadataForClass($mapping['targetEntity']);
         if ($this->isResourceForRelatedEntityAccessible($targetMetadata)) {
             continue;
         }
         $fieldDefinition[ConfigUtil::EXCLUDE] = true;
         $fieldConfig[ConfigUtil::DEFINITION] = $fieldDefinition;
         $hasChanges = true;
     }
     return $hasChanges;
 }
コード例 #6
0
 /**
  * {@inheritdoc}
  */
 public function process(ContextInterface $context)
 {
     /** @var MetadataContext $context */
     if ($context->hasResult()) {
         // metadata is already loaded
         return;
     }
     $entityClass = $context->getClassName();
     if (!$this->doctrineHelper->isManageableEntityClass($entityClass)) {
         // only manageable entities are supported
         return;
     }
     // filter excluded fields on this stage though there is another processor doing the same
     // it is done due to performance reasons
     $allowedFields = $this->getAllowedFields($context->getConfig());
     $classMetadata = $this->doctrineHelper->getEntityMetadataForClass($entityClass);
     $entityMetadata = $this->entityMetadataFactory->createEntityMetadata($classMetadata);
     $fields = $classMetadata->getFieldNames();
     foreach ($fields as $fieldName) {
         if (!isset($allowedFields[$fieldName])) {
             continue;
         }
         $entityMetadata->addField($this->entityMetadataFactory->createFieldMetadata($classMetadata, $fieldName));
     }
     $associations = $classMetadata->getAssociationNames();
     foreach ($associations as $associationName) {
         if (!isset($allowedFields[$associationName])) {
             continue;
         }
         $entityMetadata->addAssociation($this->entityMetadataFactory->createAssociationMetadata($classMetadata, $associationName));
     }
     $context->setResult($entityMetadata);
 }
コード例 #7
0
ファイル: FixFieldNaming.php プロジェクト: Maksold/platform
 /**
  * Checks whether the given field is an identifier of the given entity
  *
  * @param string $entityClass
  * @param string $fieldName
  *
  * @return bool
  */
 protected function isIdentifierField($entityClass, $fieldName)
 {
     $metadata = $this->doctrineHelper->getEntityMetadataForClass($entityClass, false);
     if (null === $metadata) {
         return false;
     }
     $idFieldNames = $metadata->getIdentifierFieldNames();
     return count($idFieldNames) === 1 && reset($idFieldNames) === $fieldName;
 }
コード例 #8
0
 /**
  * {@inheritdoc}
  */
 public function reverseTransform($entityClass, $value)
 {
     if ($this->doctrineHelper->isManageableEntityClass($entityClass)) {
         $metadata = $this->doctrineHelper->getEntityMetadataForClass($entityClass);
         $idFields = $metadata->getIdentifierFieldNames();
         $value = count($idFields) === 1 ? $this->reverseTransformSingleId($value, $metadata->getTypeOfField(reset($idFields))) : $this->reverseTransformCombinedEntityId($value, $idFields, $metadata);
     }
     return $value;
 }
コード例 #9
0
 /**
  * @param string $entityClass
  * @param string $filterKey
  * @param array  $filterConfig
  *
  * @return ConfigInterface|null
  */
 protected function findFieldConfig($entityClass, $filterKey, $filterConfig)
 {
     $path = ConfigUtil::explodePropertyPath(isset($filterConfig[ConfigUtil::PROPERTY_PATH]) ? $filterConfig[ConfigUtil::PROPERTY_PATH] : $filterKey);
     if (count($path) === 1) {
         return $this->getFieldConfig($entityClass, reset($path));
     }
     $linkedProperty = array_pop($path);
     $classMetadata = $this->doctrineHelper->findEntityMetadataByPath($entityClass, $path);
     return null !== $classMetadata ? $this->getFieldConfig($classMetadata->name, $linkedProperty) : null;
 }
コード例 #10
0
ファイル: CompleteCriteria.php プロジェクト: Maksold/platform
 /**
  * {@inheritdoc}
  */
 public function process(ContextInterface $context)
 {
     /** @var Context $context */
     $entityClass = $context->getClassName();
     if (!$this->doctrineHelper->isManageableEntityClass($entityClass)) {
         // only manageable entities are supported
         return;
     }
     $criteria = $context->getCriteria();
     $this->setJoinAliases($criteria);
     $this->completeJoins($criteria);
 }
コード例 #11
0
 /**
  * {@inheritdoc}
  */
 public function process(ContextInterface $context)
 {
     /** @var Context $context */
     $entityClass = $context->getClassName();
     if (!$this->doctrineHelper->isManageableEntityClass($entityClass)) {
         // only manageable entities are supported
         return;
     }
     if (!$this->securityContext->isGranted($this->permission, new ObjectIdentity('entity', $entityClass))) {
         throw new AccessDeniedException();
     }
 }
コード例 #12
0
 /**
  * @param ClassMetadata $classMetadata
  * @param string        $associationName
  *
  * @return AssociationMetadata
  */
 public function createAssociationMetadata(ClassMetadata $classMetadata, $associationName)
 {
     $targetClass = $classMetadata->getAssociationTargetClass($associationName);
     $associationMetadata = new AssociationMetadata();
     $associationMetadata->setName($associationName);
     $associationMetadata->setTargetClassName($targetClass);
     $associationMetadata->setIsCollection($classMetadata->isCollectionValuedAssociation($associationName));
     $targetMetadata = $this->doctrineHelper->getEntityMetadataForClass($targetClass);
     $targetIdFields = $targetMetadata->getIdentifierFieldNames();
     if (count($targetIdFields) === 1) {
         $associationMetadata->setDataType($targetMetadata->getTypeOfField(reset($targetIdFields)));
     } else {
         $associationMetadata->setDataType(DataType::STRING);
     }
     return $associationMetadata;
 }
コード例 #13
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);
         }
     }
 }
コード例 #14
0
ファイル: RestDocHandler.php プロジェクト: Maksold/platform
 /**
  * @param ApiDoc $annotation
  * @param string $entityClass
  * @param string $requirement
  */
 protected function addIdRequirement(ApiDoc $annotation, $entityClass, $requirement)
 {
     $metadata = $this->doctrineHelper->getEntityMetadataForClass($entityClass);
     $idFields = $metadata->getIdentifierFieldNames();
     $dataType = count($idFields) === 1 ? $metadata->getTypeOfField(reset($idFields)) : DataType::STRING;
     $annotation->addRequirement(RestRouteOptionsResolver::ID_ATTRIBUTE, ['dataType' => ApiDocDataTypeConverter::convertToApiDocDataType($dataType), 'requirement' => $requirement, 'description' => self::ID_DESCRIPTION]);
 }
コード例 #15
0
ファイル: BuildQuery.php プロジェクト: Maksold/platform
 /**
  * {@inheritdoc}
  */
 public function process(ContextInterface $context)
 {
     /** @var GetListContext $context */
     if ($context->hasQuery()) {
         // a query is already built
         return;
     }
     $entityClass = $context->getClassName();
     if (!$this->doctrineHelper->isManageableEntityClass($entityClass)) {
         // only manageable entities are supported
         return;
     }
     $query = $this->doctrineHelper->getEntityRepositoryForClass($entityClass)->createQueryBuilder('e');
     $this->doctrineHelper->applyCriteria($query, $context->getCriteria());
     $context->setQuery($query);
 }
コード例 #16
0
 /**
  * @param ConfigContext $context
  * @param array         $fields
  * @param string        $rootEntityClass
  * @param ClassMetadata $metadata
  * @param string|null   $fieldPath
  */
 protected function processFields(ConfigContext $context, array &$fields, $rootEntityClass, ClassMetadata $metadata, $fieldPath = null)
 {
     foreach ($fields as $fieldName => &$fieldConfig) {
         if (is_array($fieldConfig)) {
             $propertyPath = !empty($fieldConfig[ConfigUtil::PROPERTY_PATH]) ? $fieldConfig[ConfigUtil::PROPERTY_PATH] : $fieldName;
             $path = ConfigUtil::explodePropertyPath($propertyPath);
             if (count($path) === 1) {
                 $this->setFieldCustomizationHandler($context, $fieldConfig, $metadata, $propertyPath, $rootEntityClass, $this->buildFieldPath($fieldName, $fieldPath));
             } else {
                 $linkedField = array_pop($path);
                 $linkedMetadata = $this->doctrineHelper->findEntityMetadataByPath($metadata->name, $path);
                 if (null !== $linkedMetadata) {
                     $this->setFieldCustomizationHandler($context, $fieldConfig, $linkedMetadata, $linkedField, $rootEntityClass, $this->buildFieldPath($fieldName, $fieldPath));
                 }
             }
         }
     }
 }
コード例 #17
0
ファイル: ApiTestCase.php プロジェクト: startupz/platform-1
 /**
  * @param string $entityClass
  * @param array  $content
  *
  * @return array
  */
 protected function getGetRequestConfig($entityClass, $content)
 {
     $recordExist = count($content) === 1;
     $recordContent = $recordExist ? $content[0] : [];
     $idFields = $this->doctrineHelper->getEntityIdentifierFieldNamesForClass($entityClass);
     $idFieldCount = count($idFields);
     if ($idFieldCount === 1) {
         // single identifier
         return [$recordExist ? $recordContent[reset($idFields)] : 1, $recordExist];
     } elseif ($idFieldCount > 1) {
         // combined identifier
         $requirements = [];
         foreach ($idFields as $field) {
             $requirements[$field] = $recordExist ? $content[$field] : 1;
         }
         return [implode(RestRequest::ARRAY_DELIMITER, $requirements), $recordExist];
     }
 }
コード例 #18
0
ファイル: CompleteSorters.php プロジェクト: Maksold/platform
 /**
  * @param array      $sorters
  * @param string     $entityClass
  * @param array|null $config
  *
  * @return array
  */
 protected function completeSorters(array $sorters, $entityClass, $config)
 {
     $metadata = $this->doctrineHelper->getEntityMetadataForClass($entityClass);
     $fields = array_merge(array_keys($this->doctrineHelper->getIndexedFields($metadata)), array_keys($this->doctrineHelper->getIndexedAssociations($metadata)));
     foreach ($fields as $fieldName) {
         if (array_key_exists($fieldName, $sorters)) {
             // already defined
             continue;
         }
         $sorters[$fieldName] = null;
     }
     if (!empty($config)) {
         foreach ($sorters as $fieldName => &$fieldConfig) {
             if (ConfigUtil::isExcludedField($config, $fieldName)) {
                 $fieldConfig[ConfigUtil::EXCLUDE] = true;
             }
         }
     }
     return $sorters;
 }
コード例 #19
0
 /**
  * @param array  $definition
  * @param string $entityClass
  * @param int    $limit
  */
 protected function setLimits(array &$definition, $entityClass, $limit)
 {
     if (isset($definition[ConfigUtil::FIELDS]) && is_array($definition[ConfigUtil::FIELDS])) {
         $metadata = $this->doctrineHelper->getEntityMetadataForClass($entityClass);
         foreach ($definition[ConfigUtil::FIELDS] as $fieldName => &$fieldConfig) {
             if (is_array($fieldConfig)) {
                 $propertyPath = ConfigUtil::getPropertyPath($fieldConfig, $fieldName);
                 $path = ConfigUtil::explodePropertyPath($propertyPath);
                 if (count($path) === 1) {
                     $this->setFieldLimit($fieldConfig, $metadata, $propertyPath, $limit);
                 } else {
                     $linkedField = array_pop($path);
                     $linkedMetadata = $this->doctrineHelper->findEntityMetadataByPath($entityClass, $path);
                     if (null !== $linkedMetadata) {
                         $this->setFieldLimit($fieldConfig, $linkedMetadata, $linkedField, $limit);
                     }
                 }
             }
         }
     }
 }
コード例 #20
0
 /**
  * @param ClassMetadata $classMetadata
  * @param string        $associationName
  *
  * @return AssociationMetadata
  */
 public function createAssociationMetadata(ClassMetadata $classMetadata, $associationName)
 {
     $targetClass = $classMetadata->getAssociationTargetClass($associationName);
     $associationMetadata = new AssociationMetadata();
     $associationMetadata->setName($associationName);
     $associationMetadata->setTargetClassName($targetClass);
     $associationMetadata->setIsCollection($classMetadata->isCollectionValuedAssociation($associationName));
     $targetMetadata = $this->doctrineHelper->getEntityMetadataForClass($targetClass);
     $targetIdFields = $targetMetadata->getIdentifierFieldNames();
     if (count($targetIdFields) === 1) {
         $associationMetadata->setDataType($targetMetadata->getTypeOfField(reset($targetIdFields)));
     } else {
         $associationMetadata->setDataType(DataType::STRING);
     }
     if ($targetMetadata->inheritanceType !== ClassMetadata::INHERITANCE_TYPE_NONE) {
         $associationMetadata->setAcceptableTargetClassNames($targetMetadata->subClasses);
     } else {
         $associationMetadata->addAcceptableTargetClassName($targetClass);
     }
     return $associationMetadata;
 }
コード例 #21
0
ファイル: CompleteFilters.php プロジェクト: Maksold/platform
 /**
  * @param array         $filters
  * @param ClassMetadata $metadata
  *
  * @return array
  */
 protected function getAssociationFilters(array $filters, ClassMetadata $metadata)
 {
     $relations = $this->doctrineHelper->getIndexedAssociations($metadata);
     foreach ($relations as $fieldName => $dataType) {
         if (array_key_exists($fieldName, $filters)) {
             // already defined
             continue;
         }
         $filters[$fieldName] = [ConfigUtil::DATA_TYPE => $dataType, ConfigUtil::ALLOW_ARRAY => true];
     }
     return $filters;
 }
コード例 #22
0
ファイル: RemoveDuplicates.php プロジェクト: Maksold/platform
 /**
  * @param array  $sectionConfig
  * @param string $entityClass
  *
  * @return array
  */
 protected function removeDuplicates(array $sectionConfig, $entityClass)
 {
     if (empty($sectionConfig[ConfigUtil::FIELDS])) {
         return $sectionConfig;
     }
     $keys = array_keys($sectionConfig[ConfigUtil::FIELDS]);
     foreach ($keys as $key) {
         $fieldPath = !empty($sectionConfig[ConfigUtil::FIELDS][$key][ConfigUtil::PROPERTY_PATH]) ? $sectionConfig[ConfigUtil::FIELDS][$key][ConfigUtil::PROPERTY_PATH] : $key;
         $path = ConfigUtil::explodePropertyPath($fieldPath);
         if (count($path) === 1) {
             continue;
         }
         $fieldName = array_pop($path);
         if (array_key_exists(implode(ConfigUtil::PATH_DELIMITER, $path), $sectionConfig[ConfigUtil::FIELDS])) {
             $metadata = $this->doctrineHelper->findEntityMetadataByPath($entityClass, $path);
             if (null !== $metadata && in_array($fieldName, $metadata->getIdentifierFieldNames(), true)) {
                 unset($sectionConfig[ConfigUtil::FIELDS][$key]);
             }
         }
     }
     return $sectionConfig;
 }
コード例 #23
0
ファイル: SetIncludeFilter.php プロジェクト: Maksold/platform
 /**
  * {@inheritdoc}
  */
 public function process(ContextInterface $context)
 {
     /** @var Context $context */
     $entityClass = $context->getClassName();
     if (!$this->doctrineHelper->isManageableEntityClass($entityClass)) {
         // only manageable entities are supported
         return;
     }
     $indexedAssociations = $this->doctrineHelper->getIndexedAssociations($this->doctrineHelper->getEntityMetadata($entityClass));
     if (!$indexedAssociations) {
         // no associations - no sense to add include filters
         return;
     }
     $filters = $context->getFilters();
     if ($filters->has(self::FILTER_KEY)) {
         // filters have been already set
         return;
     }
     $includeFilter = new IncludeFilter(DataType::STRING, 'Inclusion of related resources. Comma-separated paths, e.g. "articles,comments,author"');
     $includeFilter->setArrayAllowed(true);
     $filters->add(self::FILTER_KEY, $includeFilter);
 }
コード例 #24
0
ファイル: BuildQuery.php プロジェクト: Maksold/platform
 /**
  * {@inheritdoc}
  */
 public function process(ContextInterface $context)
 {
     /** @var GetContext $context */
     if ($context->hasQuery()) {
         // a query is already built
         return;
     }
     $entityClass = $context->getClassName();
     if (!$this->doctrineHelper->isManageableEntityClass($entityClass)) {
         // only manageable entities are supported
         return;
     }
     $query = $this->doctrineHelper->getEntityRepositoryForClass($entityClass)->createQueryBuilder('e');
     $this->doctrineHelper->applyCriteria($query, $context->getCriteria());
     $entityId = $context->getId();
     $idFields = $this->doctrineHelper->getEntityIdentifierFieldNamesForClass($entityClass);
     if (count($idFields) === 1) {
         // single identifier
         if (is_array($entityId)) {
             throw new \UnexpectedValueException(sprintf('The entity identifier cannot be an array because the entity "%s" has single primary key.', $entityClass));
         }
         $query->andWhere(sprintf('e.%s = :id', reset($idFields)))->setParameter('id', $entityId);
     } else {
         // combined identifier
         if (!is_array($entityId)) {
             throw new \UnexpectedValueException(sprintf('The entity identifier must be an array because the entity "%s" has composite primary key.', $entityClass));
         }
         $counter = 1;
         foreach ($idFields as $field) {
             if (!array_key_exists($field, $entityId)) {
                 throw new \UnexpectedValueException(sprintf('The entity identifier array must have the key "%s" because ' . 'the entity "%s" has composite primary key.', $field, $entityClass));
             }
             $query->andWhere(sprintf('e.%s = :id%d', $field, $counter))->setParameter(sprintf('id%d', $counter), $entityId[$field]);
             $counter++;
         }
     }
     $context->setQuery($query);
 }
コード例 #25
0
ファイル: LoadFromMetadata.php プロジェクト: Maksold/platform
 /**
  * {@inheritdoc}
  */
 public function process(ContextInterface $context)
 {
     /** @var RelationConfigContext $context */
     $config = $context->getResult();
     if (null !== $config && ConfigUtil::isRelationInitialized($config)) {
         // a config already exists
         return;
     }
     $entityClass = $context->getClassName();
     if (!$this->doctrineHelper->isManageableEntityClass($entityClass)) {
         // only manageable entities are supported
         return;
     }
     if (null === $config) {
         $config = [];
     }
     $targetIdFields = $this->doctrineHelper->getEntityIdentifierFieldNamesForClass($entityClass);
     if (!isset($config[ConfigUtil::EXCLUSION_POLICY])) {
         $config[ConfigUtil::EXCLUSION_POLICY] = ConfigUtil::EXCLUSION_POLICY_ALL;
     }
     $config[ConfigUtil::FIELDS] = count($targetIdFields) === 1 ? reset($targetIdFields) : array_fill_keys($targetIdFields, null);
     $context->setResult($config);
 }
コード例 #26
0
 /**
  * {@inheritdoc}
  */
 public function process(ContextInterface $context)
 {
     /** @var MetadataContext $context */
     if ($context->hasResult()) {
         // metadata is already loaded
         return;
     }
     $entityClass = $context->getClassName();
     if (!$this->doctrineHelper->isManageableEntityClass($entityClass)) {
         // only manageable entities are supported
         return;
     }
     $classMetadata = $this->doctrineHelper->getEntityMetadataForClass($entityClass);
     $entityMetadata = $this->entityMetadataFactory->createEntityMetadata($classMetadata);
     $fields = $classMetadata->getFieldNames();
     foreach ($fields as $fieldName) {
         $entityMetadata->addField($this->entityMetadataFactory->createFieldMetadata($classMetadata, $fieldName));
     }
     $associations = $classMetadata->getAssociationNames();
     foreach ($associations as $associationName) {
         $entityMetadata->addAssociation($this->entityMetadataFactory->createAssociationMetadata($classMetadata, $associationName));
     }
     $context->setResult($entityMetadata);
 }
コード例 #27
0
 /**
  * @param Route  $route
  * @param string $entityClass
  */
 protected function setIdRequirement(Route $route, $entityClass)
 {
     $metadata = $this->doctrineHelper->getEntityMetadataForClass($entityClass);
     $idFields = $metadata->getIdentifierFieldNames();
     $idFieldCount = count($idFields);
     if ($idFieldCount === 1) {
         // single identifier
         $route->setRequirement(self::ID_ATTRIBUTE, $this->valueNormalizer->getRequirement($metadata->getTypeOfField(reset($idFields)), [RequestType::REST, RequestType::JSON_API]));
     } elseif ($idFieldCount > 1) {
         // combined identifier
         $requirements = [];
         foreach ($idFields as $field) {
             $requirements[] = $field . '=' . $this->valueNormalizer->getRequirement($metadata->getTypeOfField($field), [RequestType::REST, RequestType::JSON_API]);
         }
         $route->setRequirement(self::ID_ATTRIBUTE, implode(RestRequest::ARRAY_DELIMITER, $requirements));
     }
 }
コード例 #28
0
ファイル: ObjectNormalizer.php プロジェクト: Maksold/platform
 /**
  * @param mixed      $value
  * @param int        $level
  * @param array|null $config
  *
  * @return mixed
  */
 protected function normalizeValue($value, $level, $config = null)
 {
     if (is_array($value)) {
         $nextLevel = $level + 1;
         foreach ($value as &$val) {
             $val = $this->normalizeValue($val, $nextLevel, $config);
         }
     } elseif (is_object($value)) {
         $objectNormalizer = $this->getObjectNormalizer($value);
         if (null !== $objectNormalizer) {
             $value = $objectNormalizer->normalize($value);
         } elseif ($value instanceof \Traversable) {
             $result = [];
             $nextLevel = $level + 1;
             foreach ($value as $val) {
                 $result[] = $this->normalizeValue($val, $nextLevel, $config);
             }
             $value = $result;
         } elseif (!empty($config)) {
             $value = $this->normalizeObjectByConfig($value, $config, $level);
         } elseif ($this->doctrineHelper->isManageableEntity($value)) {
             if ($level <= static::MAX_NESTING_LEVEL) {
                 $value = $this->normalizeEntity($value, $level);
             } else {
                 $entityId = $this->doctrineHelper->getEntityIdentifier($value);
                 $count = count($entityId);
                 if ($count === 1) {
                     $value = reset($entityId);
                 } elseif ($count > 1) {
                     $value = $entityId;
                 } else {
                     throw new \RuntimeException(sprintf('The entity "%s" does not have an identifier.', ClassUtils::getClass($value)));
                 }
             }
         } else {
             if ($level <= static::MAX_NESTING_LEVEL) {
                 $value = $this->normalizePlainObject($value, $level);
             } elseif (method_exists($value, '__toString')) {
                 $value = (string) $value;
             } else {
                 throw new \RuntimeException(sprintf('Instance of "%s" cannot be normalized.', get_class($value)));
             }
         }
     }
     return $value;
 }
コード例 #29
0
 /**
  * @param array         $definition
  * @param ClassMetadata $metadata
  * @param string        $version
  * @param string[]      $requestType
  * @param array         $extras
  *
  * @return array
  */
 protected function getAssociations(array $definition, ClassMetadata $metadata, $version, array $requestType, array $extras)
 {
     $associations = $metadata->getAssociationMappings();
     foreach ($associations as $fieldName => $mapping) {
         if (!$this->isAssociationCompletionRequired($fieldName, $definition)) {
             continue;
         }
         $identifierFieldNames = $this->doctrineHelper->getEntityIdentifierFieldNamesForClass($mapping['targetEntity']);
         $config = [ConfigUtil::DEFINITION => [ConfigUtil::EXCLUSION_POLICY => ConfigUtil::EXCLUSION_POLICY_ALL, ConfigUtil::FIELDS => count($identifierFieldNames) === 1 ? reset($identifierFieldNames) : array_fill_keys($identifierFieldNames, null)]];
         if (isset($definition[$fieldName]) && is_array($definition[$fieldName])) {
             $config = array_merge_recursive($config, [ConfigUtil::DEFINITION => $definition[$fieldName]]);
         }
         if ($this->exclusionProvider->isIgnoredRelation($metadata, $fieldName)) {
             $config[ConfigUtil::DEFINITION][ConfigUtil::EXCLUDE] = true;
         }
         $definition[$fieldName] = $config;
     }
     return $definition;
 }
コード例 #30
0
 /**
  * @param string $entityClass
  *
  * @return string
  */
 protected function getDefaultValue($entityClass)
 {
     return $this->doctrineHelper->getOrderByIdentifier($entityClass);
 }