private function buildRoleMap($hierarchy)
    {
        $this->map = $this->arr();

        foreach ($hierarchy as $main => $roles) {
            $hierarchy[$main] = $this->arr((array)$roles);
        }

        $hierarchy = $this->arr($hierarchy);
        foreach ($hierarchy as $main => $roles) {
            $this->map->append($main, $roles->val());
            $additionalRoles = clone $roles;
            $parsed = $this->arr();
            $role = '';

            while ($additionalRoles->count() > 0 && $additionalRoles->removeFirst($role)) {
                if (!$hierarchy->keyExists($role)) {
                    continue;
                }

                $parsed->append($role);
                $innerRole = $this->arr($this->map->key($main));
                $innerRole->merge($hierarchy[$role]->val());
                $this->map->append($main, $innerRole->val());
                $additionalRoles->merge($hierarchy->key($role)->diff($parsed->val())->val());
            }
        }
    }