Helper class for normalizing a file system paths.

This code is from GaufrettePHP5 library that provides a filesystem abstraction layer.

<?php

// namespace GaufretteUtil;

/**
 * Path utils
 *
 * @package Gaufrette
 * @author  Antoine Hrault <antoine.herault@gmail.com>
 */
class Path
{
    /**
     * Normalizes the given path
     *
     * @param string $path
     *
     * @return string
     */
    public static function normalize($path)
    {
        $path   = str_replace('', '/', $path);
        $prefix = static::getAbsolutePrefix($path);
        $path   = substr($path, strlen($prefix));
        $parts  = array_filter(explode('/', $path), 'strlen');
        $tokens = array();

        foreach ($parts as $part) {
            switch ($part) {
                case '.':
                    continue;
                case '..':
                    if (0 !== count($tokens)) {
                        array_pop($tokens);
                        continue;
                    } elseif (!empty($prefix)) {
                        continue;
                    }
                default:
                    $tokens[] = $part;
            }
        }

        return $prefix . implode('/', $tokens);
    }

    /**
     * Indicates whether the given path is absolute or not
     *
     * @param string $path A normalized path
     *
     * @return boolean
     */
    public static function isAbsolute($path)
    {
        return '' !== static::getAbsolutePrefix($path);
    }

    /**
     * Returns the absolute prefix of the given path
     *
     * @param string $path A normalized path
     *
     * @return string
     */
    public static function getAbsolutePrefix($path)
    {
        preg_match('|^(?P<prefix>([a-zA-Z]:)?/)|', $path, $matches);

        if (empty($matches['prefix'])) {
            return '';
        }

        return strtolower($matches['prefix']);
    }
}

// namespace specGaufretteUtil;

// use PHPSpec2ObjectBehavior;

// class Path extends ObjectBehavior
// {
//     function it_should_check_if_path_is_absolute()
//     {
//         $this->isAbsolute('/home/path')->shouldBe(true);
//         $this->isAbsolute('home/path')->shouldBe(false);
//         $this->isAbsolute('../home/path')->shouldBe(false);
//     }

//     function it_should_normalize_file_path()
//     {
//         $this->normalize('C:someother.txt')->shouldReturn('c:/some/other.txt');
//         $this->normalize('..other.txt')->shouldReturn('../other.txt');
//         $this->normalize('..other.txt')->shouldReturn('../other.txt');
//         $this->normalize('/home/other/../new')->shouldReturn('/home/new');
//         $this->normalize('/home/other/./new')->shouldReturn('/home/other/new');
//     }
// }