Normalize types and prefix types with namespaces

@param array $fieldDefinitions The field definitions
@param string $namespace The namespace
@return array The normalized and type converted field definitions
498519
protected function normalizeFieldDefinitions(array $fieldDefinitions, $namespace = '')
    {
        foreach ($fieldDefinitions as &$fieldDefinition) {
            if ($fieldDefinition['type'] == 'bool') {
                $fieldDefinition['type'] = 'boolean';
            } elseif ($fieldDefinition['type'] == 'int') {
                $fieldDefinition['type'] = 'integer';
            } elseif (preg_match('/^[A-Z]/', $fieldDefinition['type'])) {
                if (class_exists($fieldDefinition['type'])) {
                    $fieldDefinition['type'] = '\\' . $fieldDefinition['type'];
                } else {
                    $fieldDefinition['type'] = '\\' . $namespace . '\\' . $fieldDefinition['type'];
                }
            }
        }
        return $fieldDefinitions;
    }
38903
public function addFieldToQueryType(FieldDefinitionNode $field): self
    {
        $query = $this->queryTypeDefinition();
        $query->fields = ASTHelper::mergeNodeList($query->fields, [$field]);

        $this->setDefinition($query);

        return $this;
    }
723595
public function addField(FieldDefinition $field): FieldsAwareDefinitionInterface
    {
        $this->fields[$field->getName()] = $field;

        return $this;
    }
884747
protected function translateFieldCode(FieldsDefinition $fields, $fieldId)
    {
        $field = $fields->getField($fieldId);
        if ($field instanceof FieldInterface) {
            return  '{f' . $field->getOrder() . '}';
        }

        return $fieldId;
    }
1247289
protected function generateNamespaceTree($namespaces)
    {
        sort($namespaces);

        $result = array();
        foreach ($namespaces as $namespace) {
            if (!$namespace) {
                $namespace = 'global';
            }

            $namespace_list = explode('\\', $namespace);

            $node = &$result;
            foreach ($namespace_list as $singular) {
                if (!isset($node[$singular])) {
                    $node[$singular] = array();
                }

                $node = &$node[$singular];
            }
        }

        return $result;
    }
556301
private function getFieldLayoutFieldsDefinition(array $fields): array
    {
        $fieldDefinitions = [];

        foreach ($fields as $field) {
            $fieldDefinitions[$field->handle] = $field->required;
        }

        return $fieldDefinitions;
    }
268897
public function toFieldDefinition(StorageFieldDefinition $storageDef, FieldDefinition $fieldDef)
    {
        $fieldDef->fieldTypeConstraints->fieldSettings = new FieldSettings(
            array(
                'isMultiple' => !empty($storageDef->dataInt1) ? (bool)$storageDef->dataInt1 : false,
            )
        );

        $fieldDef->defaultValue->data = empty($storageDef->dataText5)
            ? null
            : explode(',', $storageDef->dataText5);
        // TODO This will contain comma separated country codes, which is correct for value but not for sort key.
        // Sort key should contain comma separated lowercased country names.
        $fieldDef->defaultValue->sortKey = $storageDef->dataText5;
    }
236128
protected function generateNamespaceContext(array & $tokens)
	{
		$namespaceContext = new NamespaceContext();
		
		for($size = count($tokens), $i = 0; $i < $size; $i++)
		{
			if(is_array($tokens[$i]))
			{
				switch($tokens[$i][0])
				{
					case T_NAMESPACE:
						
						$namespace = '';
						
						for($i++; $i < $size; $i++)
						{
							if(is_array($tokens[$i]))
							{
								switch($tokens[$i][0])
								{
									case T_WHITESPACE:
									case T_COMMENT:
									case T_DOC_COMMENT:
										continue 2;
									case T_NS_SEPARATOR:
									case T_STRING:
										$namespace .= $tokens[$i][1];
										continue 2;
								}
							}
							
							break;
						}
						
						$namespaceContext = new NamespaceContext($namespace);
						
						break;
					case T_USE:
						do
						{
							$i++;
							$this->readImport($tokens, $i, $namespaceContext);
						}
						while($tokens[$i] == ',');
						
						break;
					case T_CLASS:
					case T_INTERFACE:
					case T_TRAIT:
						// Abort namespace detection as soon as code declaring a type is reached.
						break 2;
				}
			}
		}
		
		return $namespaceContext;
	}
235834
protected function normalizeFieldTypes(array &$fields)
    {
        foreach ($fields as $rkey => &$value) {
            $value['fieldname'] = $rkey;

            if ($value['type'] === 'select' && isset($value['multiple']) && $value['multiple'] === true) {
                $value['type'] = 'selectmultiple';
            }

            if (isset($this->typemap[$value['type']])) {
                $value['fieldtype'] = $this->typemap[$value['type']];
            } else {
                $value['fieldtype'] = $this->typemap['text'];
            }
        }
    }
1281293
protected function parseNamespaceDeclaration($namespaceDeclaration)
    {
        $result = preg_match(self::SPLIT_PATTERN_NAMESPACEDECLARATION, $namespaceDeclaration, $matches);
        if ($result !== 1 || !(isset($matches['alias']) && isset($matches['packageKey']))) {
            throw new Fusion\Exception('Invalid namespace declaration "' . $namespaceDeclaration . '"', 1180547190);
        }

        $namespaceAlias = $matches['alias'];
        $namespacePackageKey = $matches['packageKey'];
        $this->objectTypeNamespaces[$namespaceAlias] = $namespacePackageKey;
    }
524574
public function prependField(FieldDefinition $field): FieldsAwareDefinitionInterface
    {
        $this->fields = array_merge([$field->getName() => $field], $this->fields);

        return $this;
    }
42363
public function toStorageFieldDefinition(FieldDefinition $fieldDef, StorageFieldDefinition $storageDef)
    {
        if (isset($fieldDef->fieldTypeConstraints->validators[self::STRING_LENGTH_VALIDATOR_IDENTIFIER]['maxStringLength'])) {
            $storageDef->dataInt1 = $fieldDef->fieldTypeConstraints->validators[self::STRING_LENGTH_VALIDATOR_IDENTIFIER]['maxStringLength'];
        } else {
            $storageDef->dataInt1 = 0;
        }

        if (isset($fieldDef->fieldTypeConstraints->validators[self::STRING_LENGTH_VALIDATOR_IDENTIFIER]['minStringLength'])) {
            $storageDef->dataInt2 = $fieldDef->fieldTypeConstraints->validators[self::STRING_LENGTH_VALIDATOR_IDENTIFIER]['minStringLength'];
        } else {
            $storageDef->dataInt2 = 0;
        }

        $storageDef->dataText1 = $fieldDef->defaultValue->data;
    }
184080
public function toStorageFieldDefinition(FieldDefinition $fieldDef, StorageFieldDefinition $storageDef)
    {
        $storageDef->dataInt3 = (int)$fieldDef->defaultValue->data;
    }
668148
private function getNamespacedIntersectionTypes($namespace, array $aliased_classes, $this_class, $use_phpdoc_format)
    {
        if (!$this->extra_types) {
            return '';
        }

        return '&' . implode(
            '&',
            array_map(
                /**
                 * @param TNamedObject|TTemplateParam|TIterable $extra_type
                 * @return string
                 */
                function (Atomic $extra_type) use (
                    $namespace,
                    $aliased_classes,
                    $this_class,
                    $use_phpdoc_format
                ) {
                    return $extra_type->toNamespacedString(
                        $namespace,
                        $aliased_classes,
                        $this_class,
                        $use_phpdoc_format
                    );
                },
                $this->extra_types
            )
        );
    }
267420
public function toNamespacedString($namespace, array $aliased_classes, $this_class, $use_phpdoc_format)
    {
        $printed_int = false;
        $printed_float = false;
        $printed_string = false;

        $s = '';

        foreach ($this->types as $type) {
            if ($type instanceof TLiteralFloat) {
                if ($printed_float) {
                    continue;
                }

                $printed_float = true;
            } elseif ($type instanceof TLiteralString) {
                if ($printed_string) {
                    continue;
                }

                $printed_string = true;
            } elseif ($type instanceof TLiteralInt) {
                if ($printed_int) {
                    continue;
                }

                $printed_int = true;
            }

            $s .= $type->toNamespacedString($namespace, $aliased_classes, $this_class, $use_phpdoc_format) . '|';
        }

        return substr($s, 0, -1) ?: '';
    }
1205785
public function buildFields(JsonDefinition $jsonDefinition)
    {
        $fields = [];
        foreach ($jsonDefinition->getFields() as $field) {
            if ($field->getName() != 'id') {
                $fields[] = [
                    'fieldName' => $field->getName(),
                    'type' => $field->getTypeDoctrine()
                ];
            }
        }

        return $fields;
    }
543510
public function getNamespace($namespace, $documentsDirectory = null)
    {
        if (!$documentsDirectory) {
            $documentsDirectory = $this->documentDir;
        }

        if (strpos($namespace, ':') !== false) {
            list($bundle, $document) = explode(':', $namespace);
            $bundle = $this->getBundleClass($bundle);

            // If bundle has a sub-namespace it needs to be replaced
            if (strpos($documentsDirectory, '\\')) {
                $bundleSubNamespace = substr(
                    $bundle,
                    $start = strpos($bundle, '\\') + 1,
                    strrpos($bundle, '\\') - $start + 1
                );

                $documentsDirectory = str_replace(
                    $bundleSubNamespace,
                    '',
                    $documentsDirectory
                );
            }

            $namespace = substr($bundle, 0, strrpos($bundle, '\\')) . '\\' .
                $documentsDirectory . '\\' . $document;
        }

        return $namespace;
    }