Re: Converting string representation of a directory to multi-dimensional array

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



# jochem@xxxxxxxxxxxxx / 2007-02-06 12:45:22 +0100:
> there may also be some kind of solution based on
> ArrayArrayArrayIteratorIteratorRecursiveArrayIteratorIteratorRecursiveIterator
> but personally I don't grok those exotic forms of SPL interface well
> enough to tell you.

I had wanted to post something when the thread started, but gave up,
because I quickly ran into ambiguities.  What should the conversion do
when given a basename, e. g.  addToList("file.txt", ...)?

Anyway, here's an implementation, and a few Testilence tests.  The code
doesn't provide for iteration since, AFAICT, the OP is looking for
a map, not an iterable collection.

<?php

class pata
implements ArrayAccess
{
    function __construct($path, $sep = '/')
    {
        $this->sep = $sep;
        $path = trim($path, $sep);
        if ($this->isBasename($path)) {
            throw new InvalidArgumentException;
        }
        list($key, $rest) = $this->split($path);
        $this->key = $key;
        if ($this->isBasename($rest)) {
            $this->data = $rest;
        } else {
            $this->data = new self($rest, $sep);
        }
    }
    function __toString()
    {
        return $this->key . $this->sep . $this->data;
    }
    function offsetExists($key)
    {
        return $this->key === $key;
    }
    function offsetGet($key)
    {
        return $this->data;
    }
    function offsetUnset($key) { throw new LogicException; }
    function offsetSet($key, $value) { throw new LogicException; }

    private function isBasename($path)
    {
        return (false === strpos($path, $this->sep));
    }
    private function split($path)
    {
        $sp = $this->strpos($path, $this->sep);
        $key = substr($path, 0, $sp);
        $rest = trim(substr($path, $sp), $this->sep);
        return array($key, $rest);
    }
    private function strpos($haystack, $needle)
    {
        $rv = strpos($haystack, $needle);
        return (false === $rv) ? strlen($haystack) : $rv;
    }
    private $key, $data = array();
}

class pata_SingleSegmentTest extends Tence_TestCase
{
    function testThrowsOnBaseName()
    {
        $this->willThrow('InvalidArgumentException');
        new pata('foo');
    }
}

abstract class pata_MultipleSegmentTest extends Tence_TestCase
{
    abstract protected /* string */ function separator();
    function setUp()
    {
        $this->garbage = 'garbage';
        $this->key = 'foo';
        $segments = array('bar', 'baz');
        $sep = $this->separator();
        $this->path = implode($sep, $segments);
        $this->pata = new pata($this->key . $sep . $this->path, $sep);
    }
    function testIssetWithPathAsKeySucceeds()
    {
        return $this->assertTrue(isset($this->pata[$this->key]));
    }
    function testIssetWithWrongKeyFails()
    {
        return $this->assertFalse(isset($this->pata[$this->garbage]));
    }
    function testGetWithPathAsKey()
    {
        return $this->assertEquals($this->path, $this->strval($this->pata[$this->key]));
    }
    private function strval($v)
    {
        if (is_object($v)) {
            return $v->__toString();
        } else {
            return strval($v);
        }
    }
}
class pata_WinTest extends pata_MultipleSegmentTest
{
    function separator() { return '\\'; }
}
class pata_POSIXTest extends pata_MultipleSegmentTest
{
    function separator() { return '/'; }
}
class pata_tests extends Tence_TestSuite
{
    function __construct()
    {
        $this
            ->add(new pata_SingleSegmentTest)
            ->add(new pata_WinTest)
            ->add(new pata_POSIXTest)
        ;
    }
}

?>

-- 
How many Vietnam vets does it take to screw in a light bulb?
You don't know, man.  You don't KNOW.
Cause you weren't THERE.             http://bash.org/?255991

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux