/**
  * {@inheritdoc}
  */
 public function process(ContextInterface $context)
 {
     /** @var ConfigContext $context */
     /** @var array|null $definition */
     $definition = $context->getResult();
     if (empty($definition) || ConfigUtil::isExcludeAll($definition)) {
         // nothing to do
         return $definition;
     }
     $entityClass = $context->getClassName();
     if (!$this->doctrineHelper->isManageableEntityClass($entityClass)) {
         return $definition;
     }
     $expandRelations = $context->get(ExpandRelatedEntitiesConfigExtra::NAME);
     $associations = $this->doctrineHelper->getEntityMetadataForClass($entityClass)->getAssociationMappings();
     foreach ($associations as $fieldName => $mapping) {
         if (!$this->isAssociationCompletionRequired($fieldName, $definition)) {
             continue;
         }
         if (!in_array($fieldName, $expandRelations)) {
             continue;
         }
         $extras = array_filter($context->getExtras(), function ($item) {
             if ($item instanceof ExpandRelatedEntitiesConfigExtra || $item instanceof FilterFieldsConfigExtra) {
                 return false;
             }
             return true;
         });
         $config = $this->configProvider->getConfig($mapping['targetEntity'], $context->getVersion(), $context->getRequestType(), $extras);
         $definition[ConfigUtil::FIELDS][$fieldName] = $config;
     }
     $context->setResult([ConfigUtil::EXCLUSION_POLICY => ConfigUtil::EXCLUSION_POLICY_NONE, ConfigUtil::FIELDS => $definition[ConfigUtil::FIELDS]]);
 }
 /**
  * {@inheritdoc}
  */
 public function process(ContextInterface $context)
 {
     /** @var ConfigContext $context */
     $definition = $context->getResult();
     if (!isset($definition[ConfigUtil::FIELDS]) || !is_array($definition[ConfigUtil::FIELDS]) || !ConfigUtil::isExcludeAll($definition)) {
         // expected normalized configs
         return;
     }
     $entityClass = $context->getClassName();
     if (!$this->doctrineHelper->isManageableEntityClass($entityClass)) {
         // only manageable entities are supported
         return;
     }
     if ($this->updateRelations($definition, $entityClass)) {
         $context->setResult($definition);
     }
 }
Пример #3
0
 /**
  * {@inheritdoc}
  */
 public function process(ContextInterface $context)
 {
     /** @var ConfigContext $context */
     /** @var array|null $definition */
     $definition = $context->getResult();
     if (empty($definition) || ConfigUtil::isExcludeAll($definition)) {
         // nothing to normalize
         return;
     }
     if (ConfigUtil::getArrayValue($definition, ConfigUtil::DEFINITION)) {
         $definition = ConfigUtil::getArrayValue($definition, ConfigUtil::DEFINITION);
     }
     $fields = ConfigUtil::getArrayValue($definition, ConfigUtil::FIELDS);
     $entityClass = $context->getClassName();
     if ($this->doctrineHelper->isManageableEntityClass($entityClass)) {
         $fields = $this->completeDefinition($fields, $entityClass, $context->getVersion(), $context->getRequestType(), $context->getExtras());
     }
     $context->setResult([ConfigUtil::EXCLUSION_POLICY => ConfigUtil::EXCLUSION_POLICY_ALL, ConfigUtil::FIELDS => $fields]);
 }
Пример #4
0
 /**
  * {@inheritdoc}
  */
 public function process(ContextInterface $context)
 {
     /** @var ConfigContext $context */
     $filters = $context->getFilters();
     if (empty($filters)) {
         // nothing to normalize
         return;
     }
     $fields = ConfigUtil::getArrayValue($filters, ConfigUtil::FIELDS);
     if (ConfigUtil::isExcludeAll($filters)) {
         $fields = ConfigUtil::removeExclusions($fields);
     } else {
         $entityClass = $context->getClassName();
         if ($this->doctrineHelper->isManageableEntityClass($entityClass)) {
             $fields = ConfigUtil::removeExclusions($this->completeFilters($fields, $entityClass, $context->getResult()));
         }
     }
     $context->setFilters([ConfigUtil::EXCLUSION_POLICY => ConfigUtil::EXCLUSION_POLICY_ALL, ConfigUtil::FIELDS => $fields]);
 }
Пример #5
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);
         }
     }
 }
Пример #6
0
 /**
  * {@inheritdoc}
  */
 public function process(ContextInterface $context)
 {
     /** @var GetListContext $context */
     $configOfFilters = $context->getConfigOfFilters();
     if (empty($configOfFilters)) {
         // a filters' configuration does not contains any data
         return;
     }
     if (!ConfigUtil::isExcludeAll($configOfFilters)) {
         // it seems that filters' configuration was not normalized
         throw new \RuntimeException(sprintf('Expected "all" exclusion policy for filters. Got: %s.', ConfigUtil::getExclusionPolicy($configOfFilters)));
     }
     $fields = ConfigUtil::getArrayValue($configOfFilters, ConfigUtil::FIELDS);
     $filters = $context->getFilters();
     foreach ($fields as $field => $fieldConfig) {
         if ($filters->has($field)) {
             continue;
         }
         $filter = $this->createFilter(ConfigUtil::getPropertyPath($fieldConfig, $field), $fieldConfig);
         if (null !== $filter) {
             $filters->add($field, $filter);
         }
     }
 }
Пример #7
0
 /**
  * @param object $object
  * @param array  $config
  * @param int    $level
  *
  * @return array
  *
  * @SuppressWarnings(PHPMD.NPathComplexity)
  * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  */
 protected function normalizeObjectByConfig($object, $config, $level)
 {
     if (!ConfigUtil::isExcludeAll($config)) {
         throw new \RuntimeException(sprintf('The "%s" must be "%s".', ConfigUtil::EXCLUSION_POLICY, ConfigUtil::EXCLUSION_POLICY_ALL));
     }
     if (!array_key_exists(ConfigUtil::FIELDS, $config)) {
         throw new \RuntimeException(sprintf('The "%s" config does not exist.', ConfigUtil::FIELDS));
     }
     $fields = $config[ConfigUtil::FIELDS];
     if (!is_array($fields)) {
         throw new \RuntimeException(sprintf('The "%s" config must be an array.', ConfigUtil::FIELDS));
     }
     $result = [];
     foreach ($fields as $fieldName => $fieldConfig) {
         $value = null;
         if (is_array($fieldConfig)) {
             if (ConfigUtil::isExclude($fieldConfig)) {
                 continue;
             }
             $propertyPath = !empty($fieldConfig[ConfigUtil::PROPERTY_PATH]) ? $fieldConfig[ConfigUtil::PROPERTY_PATH] : $fieldName;
             if ($this->dataAccessor->tryGetValue($object, $propertyPath, $value) && null !== $value) {
                 $childFields = isset($fieldConfig[ConfigUtil::FIELDS]) ? $fieldConfig[ConfigUtil::FIELDS] : null;
                 if (is_string($childFields)) {
                     if ($value instanceof \Traversable) {
                         $childValue = [];
                         foreach ($value as $val) {
                             $childVal = null;
                             $this->dataAccessor->tryGetValue($val, $childFields, $childVal);
                             $childValue[] = $childVal;
                         }
                     } else {
                         $childValue = null;
                         if (!$this->dataAccessor->tryGetValue($value, $childFields, $childValue)) {
                             continue;
                         }
                     }
                     $value = $childValue;
                 } elseif (is_array($childFields)) {
                     $value = $this->normalizeObjectByConfig($value, $fieldConfig, $level + 1);
                 }
             }
         } elseif (!$this->dataAccessor->tryGetValue($object, $fieldName, $value)) {
             continue;
         }
         $result[$fieldName] = $value;
     }
     if (isset($config[ConfigUtil::POST_SERIALIZE])) {
         $result = call_user_func($config[ConfigUtil::POST_SERIALIZE], $result);
     }
     return $result;
 }