public static function filter(array $paths, $glob, $flags = self::FILTER_VALUE)
    {
        if (($flags & self::FILTER_VALUE) && ($flags & self::FILTER_KEY)) {
            throw new InvalidArgumentException('The flags Glob::FILTER_VALUE and Glob::FILTER_KEY cannot be passed at the same time.');
        }

        if (!self::isDynamic($glob)) {
            if ($flags & self::FILTER_KEY) {
                return isset($paths[$glob]) ? array($glob => $paths[$glob]) : array();
            }

            $key = array_search($glob, $paths);

            return false !== $key ? array($key => $glob) : array();
        }

        $staticPrefix = self::getStaticPrefix($glob, $flags);
        $regExp = self::toRegEx($glob, $flags);
        $filter = function ($path) use ($staticPrefix, $regExp) {
            return 0 === strpos($path, $staticPrefix) && preg_match($regExp, $path);
        };

        if (PHP_VERSION_ID >= 50600) {
            $filterFlags = ($flags & self::FILTER_KEY) ? ARRAY_FILTER_USE_KEY : 0;

            return array_filter($paths, $filter, $filterFlags);
        }

        // No support yet for the third argument of array_filter()
        if ($flags & self::FILTER_KEY) {
            $result = array();

            foreach ($paths as $path => $value) {
                if ($filter($path)) {
                    $result[$path] = $value;
                }
            }

            return $result;
        }

        return array_filter($paths, $filter);
    }