Normalize metadata

@param array $metadata Metadata
@param array $normalized Normalized metadata
@param string $namespace Namespace for keys
@return array Returns an associative array with only one level
439720
private function normalizeMetadata(array &$metadata, array &$normalized, $namespace = '') {
        foreach ($metadata as $key => $value) {
            if (strstr($key, $this->metadataNamespaceSeparator) !== false) {
                throw new DatabaseException('Invalid metadata', 400);
            }

            $ns = $namespace . ($namespace ? $this->metadataNamespaceSeparator : '') . $key;

            if (is_array($value)) {
                $this->normalizeMetadata($value, $normalized, $ns);
            } else {
                $normalized[$ns] = $value;
            }
        }
    }
601574
protected function mergeMeta(array $metadata)
    {
        $ret = static::$defaultMeta;

        $ret['uid'] = $this->uid->getUid();
        $ret['gid'] = $this->uid->getGid();

        $ret['mode'] = $metadata['type'] === 'dir' ? 040000 : 0100000;
        $ret['mode'] += $this->permissions[$metadata['type']][$metadata['visibility']];

        if (isset($metadata['size'])) {
            $ret['size'] = (int) $metadata['size'];
        }
        if (isset($metadata['timestamp'])) {
            $ret['mtime'] = (int) $metadata['timestamp'];
            $ret['ctime'] = (int) $metadata['timestamp'];
        }

        $ret['atime'] = time();

        return array_merge(array_values($ret), $ret);
    }
490485
private function normalizeMetadata(array $metaData, string $basePath): array
    {
        if (empty($basePath)) {
            $basePath = '.';
        }

        if (empty($metaData['path'])) {
            $metaData['path'] = '.';
        }

        if (empty($metaData['type'])) {
            $metaData['type'] = 'dir';
        }

        if ($metaData['path'] == $basePath) {
            if ('file' == $metaData['type']) {
                $parts = explode('/', $metaData['path']);
                $metaData['path'] = array_pop($parts);
            } else {
                $metaData['path'] = '.';
            }
        } elseif ('.' != $basePath) {
            $metaData['path'] = substr($metaData['path'], strlen($basePath) + 1);
        }

        if (isset($metaData['size'])) {
            $metaData['size'] = $this->humanFileSize($metaData['size']);
        } else {
            $metaData['size'] = '';
        }

        return $metaData;
    }
259979
public static function getSchemasByNamespaces(array $namespaces)
    {
        $schemas = [];

        /** @var SchemaDescriptor $schema */
        foreach (self::$schemasByCurie as $schema) {
            if (in_array($schema->getId()->getNamespace(), $namespaces)) {
                $schemas[] = $schema;
            }
        }

        return $schemas;
    }
214027
public function generate($namespace, $basePath, ClassMetadata $metadata)
    {
        $out = array();
        $out[] = $this->generateManagers($namespace, $basePath, $metadata);
        $out[] = $this->generateAdmins($namespace, $basePath, $metadata);

        $out = array_merge(
            $out,
            $this->updateExtension($namespace, $basePath)
        );

        return $out;
    }
198336
public static function decodeMetadata($metadata)
    {
        if (is_null($metadata) || count($metadata) == 0) {
            return [];
        }
        $result = [];
        foreach ($metadata as $key => $values) {
            foreach ($values as $value) {
                $decodedValue = [
                    '@type' => $key,
                ];
                if (self::hasBinaryHeaderSuffix($key)) {
                    if (isset(self::$metadataKnownTypes[$key])) {
                        $class = self::$metadataKnownTypes[$key];
                        /** @var Message $message */
                        $message = new $class();
                        try {
                            $message->mergeFromString($value);
                            $decodedValue += self::serializeToPhpArray($message);
                        } catch (\Exception $e) {
                            // We encountered an error trying to deserialize the data
                            $decodedValue += [
                                'data' => '<Unable to deserialize data>',
                            ];
                        }
                    } else {
                        // The metadata contains an unexpected binary type
                        $decodedValue += [
                            'data' => '<Unknown Binary Data>',
                        ];
                    }
                } else {
                    $decodedValue += [
                        'data' => $value,
                    ];
                }
                $result[] = $decodedValue;
            }
        }
        return $result;
    }
1084974
public function invalidate($namespace)
	{
		if($this->prefix) {
			$namespace = $this->prefix.$this->delimiter.$namespace;
		}

		return $this->mapper->invalidate($namespace);
	}
525659
private function createGroups(array $namespaces)
    {
        $groups = [];

        foreach ($this->groups as $group) {
            if (is_array($group)) {
                $groups[] = array_fill_keys($group, []);
            } else {
                $groups[$group] = [];
            }
        }

        if (!array_key_exists('_main', $groups)) {
            $groups = array_merge(
                ['_main' => []],
                $groups
            );
        }

        foreach ($namespaces as $namespace) {
            foreach ($groups as $groupKey => $group) {
                if (is_int($groupKey)) {
                    foreach ($group as $subGroupKey => $subGroup) {
                        if (strpos($namespace, $subGroupKey) === 0) {
                            array_push($groups[$groupKey][$subGroupKey], $namespace);

                            continue 3;
                        }
                    }
                } elseif (is_string($groupKey) && strpos($namespace, $groupKey) === 0) {
                    array_push($groups[$groupKey], $namespace);

                    continue 2;
                }
            }

            array_push($groups['_main'], $namespace);
        }

        return $groups;
    }
214711
public function getMapping($namespace)
    {
        $cacheName = 'ongr.metadata.document.'.md5($namespace);

        $namespace = $this->getClassName($namespace);
        $this->enableCache && $mapping = $this->cache->fetch($cacheName);

        if (isset($mapping) && false !== $mapping) {
            return $mapping;
        }

        $mapping = $this->getDocumentReflectionMapping(new \ReflectionClass($namespace));

        $this->enableCache && $this->cache->save($cacheName, $mapping);

        return $mapping;
    }
915545
public function setMetadata(array $metadata)
    {
        $this->getMetadata()->setData(
            array_replace_recursive(
                $this->getDefaultMetadata(),
                $metadata
            )
        );

        return $this;
    }
623616
private function getFieldFilter(ClassMetadata $metadata, array $fields)
    {
        /* @var \Doctrine\ORM\Mapping\ClassMetadata $metadata */
        $filters = array();
        foreach ($fields as $field) {
            $fieldName = $this->getFieldName($metadata, $field);
            try {
                $filters[] = $metadata->getFieldMapping($fieldName);
            } catch (\Exception $ex) {
                $mapping = $metadata->getAssociationMapping($fieldName);
                $associationMatadata = $this->entityManager->getClassMetadata($mapping['targetEntity']);
                $associationFields = $associationMatadata->getFieldNames();
                $associationIdentifier = $associationMatadata->getIdentifierFieldNames();
                $associationFields = array_values(array_filter(
                    $associationFields,
                    function ($value) use ($associationIdentifier) {
                        return !in_array($value, $associationIdentifier);
                    }
                ));
                if ($associationFields) {
                    $filters[] = array_merge(array(
                        'join' => true,
                        'join_field' => $fieldName,
                        'join_alias' => $this->getAlias(),
                    ), $associationMatadata->getFieldMapping($associationFields[0]));
                }
            }
        }

        return $filters;
    }
1287441
protected function create_metadata_general($met, DOMDocument &$doc, $xmlnode) {
        $nd = $doc->createElementNS($this->ccnamespaces['lomimscc'], 'general');

        foreach ($met->arraygeneral as $name => $value) {
            !is_array($value) ? $value = array($value) : null;
            foreach ($value as $v) {
                if ($name != 'language' && $name != 'catalog' && $name != 'entry') {
                    $nd2 = $doc->createElementNS($this->ccnamespaces['lomimscc'], $name);
                    $nd3 = $doc->createElementNS($this->ccnamespaces['lomimscc'], 'string', $v[1]);
                    $ndatt = $doc->createAttribute('language');
                    $ndatt->nodeValue = $v[0];
                    $nd3->appendChild($ndatt);
                    $nd2->appendChild($nd3);
                    $nd->appendChild($nd2);
                } else {
                    if ($name == 'language') {
                        $nd2 = $doc->createElementNS($this->ccnamespaces['lomimscc'], $name, $v[0]);
                        $nd->appendChild($nd2);
                    }
                }
            }
        }
        if (!empty($met->arraygeneral['catalog']) || !empty($met->arraygeneral['entry'])) {
            $nd2 = $doc->createElementNS($this->ccnamespaces['lomimscc'], 'identifier');
            $nd->appendChild($nd2);
            if (!empty($met->arraygeneral['catalog'])) {
                $nd3 = $doc->createElementNS($this->ccnamespaces['lomimscc'], 'catalog', $met->arraygeneral['catalog'][0][0]);
                $nd2->appendChild($nd3);
            }
            if (!empty($met->arraygeneral['entry'])) {
                $nd4 = $doc->createElementNS($this->ccnamespaces['lomimscc'], 'entry', $met->arraygeneral['entry'][0][0]);
                $nd2->appendChild($nd4);
            }
        }
        return $nd;
    }
64763
private function getProperties(ClassMetadata $metadata)
    {
        return array_merge(
            $metadata->getFieldNames(),
            array_map(function (array $mapping) {
                return $mapping['fieldName'];
            }, $metadata->getEmbeddedFieldsMappings())
        );
    }
972260
public function normalize(EntityMetadata $metadata, array $record, Store $store)
    {
        // Get the identifier and model type value from the raw record.
        list($identifier, $type) = $this->extractIdAndType($metadata, $record);

        // Reload the metadata in case a polymorphic type was found.
        $metadata = $store->getMetadataForType($type);

        // Convert relationships to the proper format.
        $record = $this->convertRelationships($metadata, $record);

        return [
            'identifier'    => (String) $identifier,
            'type'          => $type,
            'properties'    => $record,
        ];
    }
1368346
protected function extractAttributes(array $data, EntityMetadata $metadata)
    {
        $flattened = [];
        if (!isset($data['attributes']) || !is_array($data['attributes'])) {
            return $data['attributes'] = [];
        }

        $keyMap = array_flip(array_keys($data['attributes']));
        foreach ($metadata->getProperties() as $key => $propMeta) {
            if (!isset($keyMap[$key])) {
                continue;
            }
            if (true === $metadata->hasAttribute($key) || true === $metadata->hasEmbed($key)) {
                $flattened[$key] = $data['attributes'][$key];
            }
        }
        return $flattened;
    }
902406
protected static function _namespaceGetAll($namespace)
    {
        $currentData  = (isset($_SESSION[$namespace]) && is_array($_SESSION[$namespace])) ?
            $_SESSION[$namespace] : array();
        $expiringData = (isset(self::$_expiringData[$namespace]) && is_array(self::$_expiringData[$namespace])) ?
            self::$_expiringData[$namespace] : array();
        return array_merge($currentData, $expiringData);
    }
832073
static public function formatNamespace($namespace)
	{
	    if (is_array($namespace)) {
	        foreach ($namespace as $k => $v) {
	            $namespace[$k] =  static::formatNamespace($v);
	        }
	    } else {
	        $namespace = trim(str_replace('/', '\\', $namespace), '\\') . '\\';
	    }
	    
	    return $namespace;
	}