Browse Source

added Path class; added PropertyAccessTrait::__isset()

Fabian Peter Hammerle 7 years ago
parent
commit
4891e9d4d0
5 changed files with 450 additions and 1 deletions
  1. 155 0
      Path.php
  2. 10 0
      PropertyAccessTrait.php
  3. 1 1
      StringHelper.php
  4. 245 0
      tests/PathTest.php
  5. 39 0
      tests/PropertyAccessTraitTest.php

+ 155 - 0
Path.php

@@ -0,0 +1,155 @@
+<?php
+
+namespace fphammerle\helpers;
+
+class Path
+{
+    use PropertyAccessTrait;
+
+    /**
+     * @var Path|null
+     */
+    protected $_dir_path;
+    /**
+     * @var string|null
+     */
+    protected $_filename;
+    /**
+     * @var string|null
+     */
+    protected $_extension;
+
+    public function __construct($path = null)
+    {
+        $this->setPath($path);
+    }
+
+    /**
+     * @return string|null
+     */
+    public function getBasename()
+    {
+        return StringHelper::implode('', [
+            $this->_filename,
+            isset($this->_extension) ? ('.' . $this->_extension) : null,
+            ]);
+    }
+
+    /**
+     * @return string|null
+     */
+    public function setBasename($basename)
+    {
+        $this->setExtension(pathinfo($basename, PATHINFO_EXTENSION));
+
+        if(isset($this->_extension)) {
+            $this->setFilename(substr(
+                $basename,
+                0,
+                strlen($basename) - strlen($this->_extension) - 1
+            ));
+        } else {
+            $this->setFilename($basename);
+        }
+    }
+
+    /**
+     * @return Path|null
+     */
+    public function getDirPath()
+    {
+        return $this->_dir_path;
+    }
+
+    /**
+     * @var Path|string|null $path
+     */
+    public function setDirPath($path)
+    {
+        if($path instanceof Path) {
+            $this->_dir_path = $path;
+        } elseif($path === null) {
+            $this->_dir_path = null;
+        } else {
+            $this->_dir_path = new self($path);
+        }
+    }
+
+    /**
+     * @return string|null
+     */
+    public function getExtension()
+    {
+        return $this->_extension;
+    }
+
+    /**
+     * @param string|null $extension
+     */
+    public function setExtension($extension)
+    {
+        $this->_extension = ((string)$extension) ?: null;
+    }
+
+    /**
+     * @return string|null
+     */
+    public function getFilename()
+    {
+        return $this->_filename;
+    }
+
+    /**
+     * @param string|null $filename
+     */
+    public function setFilename($filename)
+    {
+        $this->_filename = ((string)$filename) ?: null;
+    }
+
+    /**
+     * @return string|null
+     */
+    public function getPath()
+    {
+        if($this->isRoot()) {
+            return '/';
+        } elseif(!isset($this->_dir_path)) {
+            return $this->basename;
+        } else {
+            $dir_path = $this->_dir_path->path;
+            if($dir_path == '/') {
+                return '/' . $this->basename;
+            } else {
+                return $dir_path . '/' . $this->basename;
+            }
+        }
+    }
+
+    /**
+     * @return string|null
+     */
+    public function setPath($path)
+    {
+        $basename = basename($path);
+        $this->setBasename($basename);
+        if(strlen($basename) < strlen($path)) {
+            $dirname = dirname($path);
+            if($dirname == $path) { // root?
+                $this->setDirPath($this);
+            } else {
+                $this->setDirPath(dirname($path));
+            }
+        } else {
+            $this->setDirPath(null);
+        }
+    }
+
+    /**
+     * @return bool
+     */
+    public function isRoot()
+    {
+        return $this->_dir_path === $this;
+    }
+}

+ 10 - 0
PropertyAccessTrait.php

@@ -31,4 +31,14 @@ trait PropertyAccessTrait
             $this->$setter_name($value);
         }
     }
+
+    /**
+     * @param string $name
+     * @return boolean
+     */
+    public function __isset($name)
+    {
+        $getter_name = 'get' . $name;
+        return method_exists($this, $getter_name) && ($this->$getter_name() !== null);
+    }
 }

+ 1 - 1
StringHelper.php

@@ -61,7 +61,7 @@ class StringHelper
         if(sizeof($pieces) == 0) {
             return null;
         } else {
-            return implode($glue, array_filter($pieces));
+            return implode($glue, $pieces);
         }
     }
 }

+ 245 - 0
tests/PathTest.php

@@ -0,0 +1,245 @@
+<?php
+
+namespace fphammerle\helpers\tests;
+
+use \fphammerle\helpers\Path;
+
+class PathTest extends \PHPUnit_Framework_TestCase
+{
+    /**
+     * @dataProvider pathProvider
+     */
+    public function testConstruct($path, $expected_dir_path, $expected_filename, $expected_extension)
+    {
+        $p = new Path($path);
+        if(isset($expected_dir_path)) {
+            $this->assertSame($expected_dir_path, $p->dirPath->path);
+        } else {
+            $this->assertNull($p->dirPath);
+        }
+        $this->assertSame($expected_filename, $p->filename);
+        $this->assertSame($expected_extension, $p->extension);
+    }
+
+    public function getBasenameProvider()
+    {
+        return [
+            ['.', null, '.'],
+            ['..', null, '..'],
+            ['file', 'php', 'file.php'],
+            ['file', null, 'file'],
+            [null, 'php', '.php'],
+            [null, null, null],
+            ];
+    }
+
+    /**
+     * @dataProvider getBasenameProvider
+     */
+    public function testGetBasename($filename, $extension, $expected_basename)
+    {
+        $p = new Path;
+        $p->filename = $filename;
+        $p->extension = $extension;
+        $this->assertSame($expected_basename, $p->basename);
+    }
+
+    public function setBasenameProvider()
+    {
+        return [
+            ['.', '.', null],
+            ['..', '..', null],
+            ['..txt', '.', 'txt'],
+            ['.php', null, 'php'],
+            ['file', 'file', null],
+            ['file.php', 'file', 'php'],
+            [null, null, null],
+            ];
+    }
+
+    /**
+     * @dataProvider setBasenameProvider
+     */
+    public function testSetBasename($basename, $expected_filename, $expected_extension)
+    {
+        $p = new Path;
+        $p->basename = $basename;
+        $this->assertSame($expected_filename, $p->filename);
+        $this->assertSame($expected_extension, $p->extension);
+    }
+
+    public function dirPathProvider()
+    {
+        return [
+            [null, null],
+            ];
+    }
+
+    /**
+     * @dataProvider dirPathProvider
+     */
+    public function testDirPath($input_dir_path, $expected_dir_path)
+    {
+        $p = new Path;
+        $p->dirPath = $input_dir_path;
+        $this->assertSame($expected_dir_path, $p->dirPath);
+    }
+
+    public function extensionProvider()
+    {
+        return [
+            ['.', '.'],
+            ['.php', '.php'],
+            ['file', 'file'],
+            ['file.php', 'file.php'],
+            [123, '123'],
+            [null, null],
+            ];
+    }
+
+    /**
+     * @dataProvider extensionProvider
+     */
+    public function testExtension($input_extension, $expected_extension)
+    {
+        $p = new Path;
+        $p->extension = $input_extension;
+        $this->assertSame($expected_extension, $p->extension);
+    }
+
+    public function filenameProvider()
+    {
+        return [
+            ['.', '.'],
+            ['.php', '.php'],
+            ['file', 'file'],
+            ['file.php', 'file.php'],
+            [123, '123'],
+            [null, null],
+            ];
+    }
+
+    /**
+     * @dataProvider filenameProvider
+     */
+    public function testFilename($input_filename, $expected_filename)
+    {
+        $p = new Path;
+        $p->filename = $input_filename;
+        $this->assertSame($expected_filename, $p->filename);
+    }
+
+    public function pathProvider()
+    {
+        return [
+            ['.', null, '.', null],
+            ['..', null, '..', null],
+            ['../../file.php', '../..', 'file', 'php'],
+            ['../file.php', '..', 'file', 'php'],
+            ['./file.php', '.', 'file', 'php'],
+            ['dir/../file.php', 'dir/..', 'file', 'php'],
+            ['dir/file.php', 'dir', 'file', 'php'],
+            ['dir/subdir/file.php', 'dir/subdir', 'file', 'php'],
+            ['/file.php', '/', 'file', 'php'],
+            ['/dir/file.php', '/dir', 'file', 'php'],
+            ['/', '/', null, null],
+            ['file.php', null, 'file', 'php'],
+            [null, null, null, null],
+            ];
+    }
+
+    /**
+     * @dataProvider pathProvider
+     */
+    public function testGetPath($expected_path, $dir_path, $filename, $extension)
+    {
+        $p = new Path;
+        $p->dirPath = $dir_path;
+        $p->filename = $filename;
+        $p->extension = $extension;
+        $this->assertSame($expected_path, $p->path);
+    }
+
+    /**
+     * @dataProvider pathProvider
+     */
+    public function testSetPath($path, $expected_dir_path, $expected_filename, $expected_extension)
+    {
+        $p = new Path;
+        $p->path = $path;
+        if(isset($expected_dir_path)) {
+            $this->assertSame($expected_dir_path, $p->dirPath->path);
+        } else {
+            $this->assertNull($p->dirPath);
+        }
+        $this->assertSame($expected_filename, $p->filename);
+        $this->assertSame($expected_extension, $p->extension);
+    }
+
+    /**
+     * @dataProvider pathProvider
+     */
+    public function testSetEqualsGet($path)
+    {
+        $p = new Path;
+        $p->path = $path;
+        $this->assertSame($path, $p->path);
+    }
+
+    public function isRootProvider()
+    {
+        return [
+            ['.', false],
+            ['..', false],
+            ['../../file.php', false],
+            ['../file.php', false],
+            ['./file.php', false],
+            ['/', true],
+            ['//', false],
+            ['/dir/file.php', false],
+            ['/file.php', false],
+            ['dir/../file.php', false],
+            ['dir/file.php', false],
+            ['dir/subdir/file.php', false],
+            ['file.php', false],
+            [null, false],
+            ];
+    }
+
+    /**
+     * @dataProvider isRootProvider
+     */
+    public function testIsRoot($path, $expected_result)
+    {
+        $p = new Path;
+        $p->path = $path;
+        $this->assertSame($expected_result, $p->isRoot());
+    }
+
+    public function reverseNameProvider()
+    {
+        return [
+            ['.', '.'],
+            ['..', '..'],
+            ['../../file.php', '../../elif.php'],
+            ['dir/subdir/file.html', 'dir/subdir/elif.lmth'],
+            ['file.png', 'elif.gnp'],
+            [null, null],
+            ];
+    }
+
+    /**
+     * @dataProvider reverseNameProvider
+     */
+    public function testReverseName($path, $expected_result)
+    {
+        $p = new Path($path);
+        if(isset($p->filename)) {
+            $p->filename = strrev($p->filename);
+        }
+        if(isset($p->extension)) {
+            $p->extension = strrev($p->extension);
+        }
+        $this->assertSame($expected_result, $p->path);
+    }
+}

+ 39 - 0
tests/PropertyAccessTraitTest.php

@@ -79,4 +79,43 @@ class PropertyAccessTraitTest extends \PHPUnit_Framework_TestCase
         $o = new TestClass(1);
         $o->square = 4;
     }
+
+    public function testIncrement()
+    {
+        $o = new TestClass(1);
+        $o->a++;
+        $this->assertEquals(2, $o->getA());
+    }
+
+    public function testAdd()
+    {
+        $o = new TestClass(1);
+        $o->a += 3;
+        $this->assertEquals(4, $o->getA());
+    }
+
+    public function testIssetTrue()
+    {
+        $o = new TestClass(2);
+        $this->assertTrue(isset($o->a));
+    }
+
+    public function testIssetEmpty()
+    {
+        $o = new TestClass('');
+        $this->assertEquals('', $o->a);
+        $this->assertTrue(isset($o->a));
+    }
+
+    public function testIssetNull()
+    {
+        $o = new TestClass(null);
+        $this->assertFalse(isset($o->a));
+    }
+
+    public function testIssetUndefined()
+    {
+        $o = new TestClass(null);
+        $this->assertFalse(isset($o->b));
+    }
 }