Gets Foreign contstrains from given fields.

@param array of CrestApps\CodeGenerator\Models\Field $field

@return array of CrestApps\CodeGenerator\Models\ForeignConstraint $field
163762
protected function getConstraintsFromfields(array $fields)
    {
        $constraints = [];

        foreach ($fields as $field) {
            if ($field->hasForeignConstraint()) {
                $constraints[] = $field->getForeignConstraint();
            }
        }

        return $constraints;
    }
18191
private function getMainFields(array $fields): array
    {
        $main = [];
        foreach ($fields as $field) {
            if ($field instanceof ChildrenAssociationField) {
                continue;
            }

            if ($field->is(Deferred::class)) {
                continue;
            }

            if (!$field->is(PrimaryKey::class)) {
                $main[] = $field;
                continue;
            }

            if ($field instanceof FkField) {
                $main[] = $field;
            }
        }

        return $main;
    }
174406
public function map(Field $field)
    {
        $values = array();

        foreach ((array)$field->value as $value) {
            $values[] = (bool)$value;
        }

        return $values;
    }
818677
public function getMessages($field = null)
    {
        $context = $this->_context;

        if ($field) {
            return isset($context->messages[$field]) ? $context->messages[$field] : [];
        } else {
            return $context->messages;
        }
    }
1032349
protected function getOptionsFromModel($field, $fieldOptions)
    {
        /*
         * Advanced usage, supplied options are callable
         */
        if (is_array($fieldOptions) && is_callable($fieldOptions)) {
            $fieldOptions = call_user_func($fieldOptions, $this, $field);
        }

        /*
         * Refer to the model method or any of its behaviors
         */
        if (!is_array($fieldOptions) && !$fieldOptions) {

            try {
                list($model, $attribute) = $field->resolveModelAttribute($this->model, $field->fieldName);
            }
            catch (Exception $ex) {
                throw new ApplicationException(Lang::get('backend::lang.field.options_method_invalid_model', [
                    'model' => get_class($this->model),
                    'field' => $field->fieldName
                ]));
            }

            $methodName = 'get'.studly_case($attribute).'Options';
            if (
                !$this->objectMethodExists($model, $methodName) &&
                !$this->objectMethodExists($model, 'getDropdownOptions')
            ) {
                throw new ApplicationException(Lang::get('backend::lang.field.options_method_not_exists', [
                    'model'  => get_class($model),
                    'method' => $methodName,
                    'field'  => $field->fieldName
                ]));
            }

            if ($this->objectMethodExists($model, $methodName)) {
                $fieldOptions = $model->$methodName($field->value, $this->data);
            }
            else {
                $fieldOptions = $model->getDropdownOptions($attribute, $field->value, $this->data);
            }
        }
        /*
         * Field options are an explicit method reference
         */
        elseif (is_string($fieldOptions)) {
            if (!$this->objectMethodExists($this->model, $fieldOptions)) {
                throw new ApplicationException(Lang::get('backend::lang.field.options_method_not_exists', [
                    'model'  => get_class($this->model),
                    'method' => $fieldOptions,
                    'field'  => $field->fieldName
                ]));
            }

            $fieldOptions = $this->model->$fieldOptions($field->value, $field->fieldName, $this->data);
        }

        return $fieldOptions;
    }
550246
private function mapToRelationshipAndAttribute(string $field): array
    {
        $rootSchema = $this->getRootSchema();
        if ($rootSchema->hasAttributeMapping($field) === true) {
            $relationship = null;
            $schema       = $rootSchema;
            $attribute    = new Attribute($field, $schema);

            return [$relationship, $attribute];
        } elseif ($rootSchema->hasRelationshipMapping($field) === true) {
            $fromSchema   = $rootSchema;
            $toSchema     = $this->getJsonSchemas()->getRelationshipSchema(get_class($fromSchema), $field);
            $relationship = new Relationship($field, $fromSchema, $toSchema);
            $attribute    = new Attribute($toSchema::RESOURCE_ID, $toSchema);

            return [$relationship, $attribute];
        } elseif (count($mightBeRelAndAttr = explode('.', $field, 2)) === 2) {
            // Last chance. It could be a dot ('.') separated relationship with an attribute.

            $mightBeRel  = $mightBeRelAndAttr[0];
            $mightBeAttr = $mightBeRelAndAttr[1];

            $fromSchema = $rootSchema;
            if ($fromSchema->hasRelationshipMapping($mightBeRel)) {
                $toSchema = $this->getJsonSchemas()->getRelationshipSchema(get_class($fromSchema), $mightBeRel);
                if ($toSchema::hasAttributeMapping($mightBeAttr) === true) {
                    $relationship = new Relationship($mightBeRel, $fromSchema, $toSchema);
                    $attribute    = new Attribute($mightBeAttr, $toSchema);

                    return [$relationship, $attribute];
                }
            }
        }

        $error = $this->createQueryError($field, static::MSG_ERR_INVALID_FIELD);
        throw new InvalidQueryParametersException($error);
    }
52350
public function select(FieldContract $field)
    {
        $attributes  = $this->html->decorate($field->get('attributes'));
        $optionsData = $field->get('optionsData') ?: [];
        if (array_get($attributes, 'data-selectar') === false or ! is_null(array_get($attributes, 'data-selectar--search'))) {
            unset($attributes['data-selectar']);
        }
        return $this->form->select(
                        $field->get('name'), $field->get('options'), $field->get('value'), $attributes, $optionsData
        );
    }
366608
private function getOrderClause(string $field) : array
    {
        $className = App::shortName(get_class($this->table), 'Model/Table', 'Table');
        $config = (new ModuleConfig(ConfigType::FIELDS(), $className))->parse();

        if (! property_exists($config, $field)) {
            return [];
        }

        if (! property_exists($config->{$field}, 'orderBy')) {
            return [];
        }

        if (! property_exists($config->{$field}, 'orderDir')) {
            return [];
        }

        return [$config->{$field}->orderBy => $config->{$field}->orderDir];
    }
815821
protected function extractImportsFromFields(array $fields)
    {
        $imports = [];

        foreach ($fields as $field) {
            $options = $field->getLanguage(static::LANGUAGE);
            $imports = array_merge($imports, explode(PHP_EOL, $options->get('imports')));

            if ($field->getFormat()) {
                $imports[] = 'use Gdbots\Pbj\Enum\Format;';
            }

            switch ($field->getType()->getTypeName()->getValue()) {
                case TypeName::INT_ENUM;
                case TypeName::STRING_ENUM;
                    $enum = $field->getEnum();
                    $imports[] = sprintf(
                        'use %s\%s;',
                        $this->enumToNativeNamespace($enum),
                        $this->enumToClassName($enum)
                    );
                    break;

                case TypeName::MESSAGE;
                    $anyOfs = $field->getAnyOf() ?: [];
                    foreach ($anyOfs as $anyOf) {
                        $imports[] = sprintf(
                            'use %s\%s as %s;',
                            $this->schemaToNativeNamespace($anyOf),
                            $this->schemaToClassName($anyOf),
                            $this->schemaToFqClassName($anyOf)
                        );
                    }
                    break;

                default:
                    break;
            }
        }

        return $imports;
    }
241477
protected function _getFieldsFromConfig($fields = null) {
		$result = [];
		if (empty($fields)) {
			return $result;
		}

		if (!is_array($fields)) {
			$result = [$fields];
		} else {
			$result = array_keys($fields);
		}

		return $result;
	}
1330416
private FieldType findForeignFieldType(Class<?> clazz, Class<?> foreignClass, Dao<?, ?> foreignDao)
			throws SQLException {
		String foreignColumnName = fieldConfig.getForeignCollectionForeignFieldName();
		for (FieldType fieldType : foreignDao.getTableInfo().getFieldTypes()) {
			if (fieldType.getType() == foreignClass
					&& (foreignColumnName == null || fieldType.getField().getName().equals(foreignColumnName))) {
				if (!fieldType.fieldConfig.isForeign() && !fieldType.fieldConfig.isForeignAutoRefresh()) {
					// this may never be reached
					throw new SQLException("Foreign collection object " + clazz + " for field '" + field.getName()
							+ "' contains a field of class " + foreignClass + " but it's not foreign");
				}
				return fieldType;
			}
		}
		// build our complex error message
		StringBuilder sb = new StringBuilder();
		sb.append("Foreign collection class ").append(clazz.getName());
		sb.append(" for field '").append(field.getName()).append("' column-name does not contain a foreign field");
		if (foreignColumnName != null) {
			sb.append(" named '").append(foreignColumnName).append('\'');
		}
		sb.append(" of class ").append(foreignClass.getName());
		throw new SQLException(sb.toString());
	}
497104
protected function getFieldMap(array $fields, &$languageCodes = null)
    {
        $fieldMap = array();
        foreach ($fields as $field) {
            if (isset($languageCodes)) {
                $languageCodes[$field->languageCode] = true;
            }
            $fieldMap[$field->fieldDefinitionId][$field->languageCode] = $field;
        }

        return $fieldMap;
    }
448435
protected function getRequiredRelationCollections(array $fields)
    {
        $variables = [];

        $collections = $this->getRelationCollections($fields, 'form');

        foreach ($collections as $collection) {
            $accesor = $this->getRelationAccessor($collection);

            if (!in_array($accesor, $variables)) {
                $variables[] = $accesor;
            }
        }

        return implode(PHP_EOL, $variables);
    }
797593
public function map(Field $field)
    {
        $values = array();

        foreach ((array)$field->value as $value) {
            $values[] = $this->convert($value);
        }

        return $values;
    }
920263
public static function get(array $options)
    {
        if (!array_key_exists('type', $options) || !array_key_exists('params', $options) || !array_key_exists('name', $options)) {
			
			if(count($options) >= 3) {
				$values = array_values($options);
				$field = isset($values[3]) ? $values[3] : null;
				return new ForeignRelationship(
					$values[1],
					$values[2],
					$values[0],
					$field
				);				
			}
			
            return null;
        }
		
		$field = array_key_exists('field', $options) ? $options['field'] : null;

        return new ForeignRelationship(
            $options['type'],
            $options['params'],
            $options['name'],
            $field
        );
    }
1272533
public function importFields($model, $fields = null)
    {
        $this->model = $model;
        $this->form = $this->owner;

        if ($fields === false) {
            return;
        }

        if (!$fields) {
            if ($model->only_fields) {
                $fields = $model->only_fields;
            } else {
                $fields = [];
                // get all field-elements
                foreach ($model->elements as $field => $f_object) {
                    if ($f_object instanceof \atk4\data\Field
                        && $f_object->isEditable()
                        && !$f_object->isHidden()
                    ) {
                        $fields[] = $field;
                    }
                }
            }
        }

        if (!is_array($fields)) {
            $fields = [$fields];
        }

        // import fields one by one
        foreach ($fields as $field) {
            $this->importField($field);
        }

        // set update hook
        if (!$this->_hook_set) {
            $this->owner->addHook('update', array($this, 'update'));
            $model->addHook('afterLoad', array($this, 'setFields'));
            $this->_hook_set = true;
        }

        return $this;
    }
1005123
public static Field[] getDeclaredFields(Field relationalField)
    {
        Field[] fields;
        if (isCollection(relationalField.getType()))
        {
            fields = PropertyAccessorHelper.getGenericClass(relationalField).getDeclaredFields();
        }
        else
        {
            fields = relationalField.getType().getDeclaredFields();
        }
        return fields;
    }