public function testGetFields() { $entityName = 'TestEntity'; $withRelations = true; $expectedFields = array(array('name' => 'field')); $this->fieldProvider->expects($this->once())->method('getFields')->with($entityName, $withRelations)->will($this->returnValue($expectedFields)); $this->assertEquals($expectedFields, $this->helper->getFields($entityName, $withRelations)); }
/** * @param string $entityName * @param string $fieldName * @return string|null */ protected function getRelationEntityName($entityName, $fieldName) { $fields = $this->fieldHelper->getFields($entityName, true); foreach ($fields as $field) { if ($field['name'] == $fieldName && $this->fieldHelper->isMultipleRelation($field)) { return $field['related_entity_name']; } } return null; }
/** * @param string $entityName * @param bool $fullData * @param int $singleRelationDeepLevel * @param int $multipleRelationDeepLevel * @return array */ protected function getEntityRulesAndBackendHeaders($entityName, $fullData = false, $singleRelationDeepLevel = 0, $multipleRelationDeepLevel = 0) { // get fields data $fields = $this->fieldHelper->getFields($entityName, true); $rules = array(); $backendHeaders = array(); $defaultOrder = 10000; // generate conversion rules and backend header foreach ($fields as $field) { $fieldName = $field['name']; if ($this->fieldHelper->getConfigValue($entityName, $fieldName, 'excluded')) { continue; } // get import/export config parameters $fieldHeader = $this->fieldHelper->getConfigValue($entityName, $fieldName, 'header', $field['label']); $fieldOrder = $this->fieldHelper->getConfigValue($entityName, $fieldName, 'order'); if ($fieldOrder === null || $fieldOrder === '') { $fieldOrder = $defaultOrder; $defaultOrder++; } $fieldOrder = (int) $fieldOrder; // process relations if ($this->fieldHelper->isRelation($field) && !$this->fieldHelper->processRelationAsScalar($entityName, $fieldName)) { $isSingleRelation = $this->fieldHelper->isSingleRelation($field) && $singleRelationDeepLevel > 0; $isMultipleRelation = $this->fieldHelper->isMultipleRelation($field) && $multipleRelationDeepLevel > 0; // if relation must be included if ($fullData && ($isSingleRelation || $isMultipleRelation)) { $relatedEntityName = $field['related_entity_name']; $fieldFullData = $this->fieldHelper->getConfigValue($entityName, $fieldName, 'full', false); // process and merge relation rules and backend header for relation list($relationRules, $relationBackendHeaders) = $this->getEntityRulesAndBackendHeaders($relatedEntityName, $fieldFullData, $singleRelationDeepLevel - 1, $multipleRelationDeepLevel - 1); $relationRules = $this->buildRelationRules($relationRules, $isSingleRelation, $isMultipleRelation, $fieldName, $fieldHeader, $fieldOrder); $rules = array_merge($rules, $relationRules); $relationBackendHeaders = $this->buildBackendHeaders($relationBackendHeaders, $isSingleRelation, $isMultipleRelation, $entityName, $fieldName, $fieldOrder); $backendHeaders = array_merge($backendHeaders, $relationBackendHeaders); } } else { // process scalars if ($fullData || $this->fieldHelper->getConfigValue($entityName, $fieldName, 'identity')) { $rules[$fieldHeader] = array('value' => $fieldName, 'order' => $fieldOrder); $backendHeaders[] = $rules[$fieldHeader]; } } } return array($this->sortData($rules), $this->sortData($backendHeaders)); }
/** * @param string $entityName * @return bool */ protected function hasIdentityFields($entityName) { $fields = $this->fieldHelper->getFields($entityName, true); foreach ($fields as $field) { $fieldName = $field['name']; if ($this->fieldHelper->getConfigValue($entityName, $fieldName, 'identity')) { return true; } } return false; }
/** * @param string $entityName * @param bool $fullData * @param int $singleRelationDeepLevel * @param int $multipleRelationDeepLevel * @return array */ protected function getEntityRulesAndBackendHeaders($entityName, $fullData = false, $singleRelationDeepLevel = 0, $multipleRelationDeepLevel = 0) { // get fields data $fields = $this->fieldHelper->getFields($entityName, true); $rules = []; $backendHeaders = []; $defaultOrder = self::DEFAULT_ORDER; // generate conversion rules and backend header foreach ($fields as $field) { $fieldName = $field['name']; if ($this->fieldHelper->getConfigValue($entityName, $fieldName, 'excluded')) { continue; } // get import/export config parameters $fieldHeader = $this->getFieldHeader($entityName, $field); $fieldOrder = $this->fieldHelper->getConfigValue($entityName, $fieldName, 'order'); if ($fieldOrder === null || $fieldOrder === '') { $fieldOrder = $defaultOrder; $defaultOrder++; } $fieldOrder = (int) $fieldOrder; // process relations if ($this->fieldHelper->isRelation($field) && !$this->fieldHelper->processRelationAsScalar($entityName, $fieldName)) { list($relationRules, $relationBackendHeaders) = $this->getRelatedEntityRulesAndBackendHeaders($entityName, $fullData, $singleRelationDeepLevel, $multipleRelationDeepLevel, $field, $fieldHeader, $fieldOrder); $rules = array_merge($rules, $relationRules); $backendHeaders = array_merge($backendHeaders, $relationBackendHeaders); } else { // process scalars if ($fullData || $this->fieldHelper->getConfigValue($entityName, $fieldName, 'identity')) { $rules[$fieldHeader] = ['value' => $fieldName, 'order' => $fieldOrder]; $backendHeaders[] = $rules[$fieldHeader]; } } } return [$this->sortData($rules), $this->sortData($backendHeaders)]; }