/** * {@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); } }
/** * @param object $entity * @param int $level * * @return array */ protected function normalizeEntity($entity, $level) { $result = []; $metadata = $this->doctrineHelper->getEntityMetadata($entity); $nextLevel = $level + 1; $fields = $metadata->getFieldNames(); foreach ($fields as $field) { $value = null; if ($this->dataAccessor->tryGetValue($entity, $field, $value)) { $result[$field] = $this->normalizeValue($value, $nextLevel); } } $associations = $metadata->getAssociationNames(); foreach ($associations as $field) { $value = null; if ($this->dataAccessor->tryGetValue($entity, $field, $value)) { $result[$field] = $this->normalizeValue($value, $nextLevel); } } return $result; }
/** * {@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); }