Creating static create functions

This commit is contained in:
Dennis Eichhorn 2016-03-27 11:37:26 +02:00
parent ef243a7ee4
commit 6b84376742
2 changed files with 32 additions and 1 deletions

View File

@ -33,6 +33,21 @@ class Directory extends FileAbstract implements Iterator, ArrayAccess
private $filter = '*'; private $filter = '*';
private $nodes = []; private $nodes = [];
public static create(string $path, $permission = 0644) : bool
{
if (!file_exists($path)) {
if(is_writable($path)) {
mkdir($path, $permission, true);
return true;
} else {
throw new PathException($path);
}
}
return false;
}
public function __construct(string $path, string $filter = '*') public function __construct(string $path, string $filter = '*')
{ {
$this->filter = $filter; $this->filter = $filter;
@ -112,7 +127,7 @@ class Directory extends FileAbstract implements Iterator, ArrayAccess
{ {
return next($this->node); return next($this->node);
} }
public function valid() public function valid()
{ {
$key = key($this->node); $key = key($this->node);

View File

@ -30,6 +30,22 @@ namespace phpOMS\System\File;
*/ */
class File extends FileAbstract class File extends FileAbstract
{ {
public static create(string $path) : bool
{
if (!file_exists($path)) {
if(is_writable($path)) {
touch($path);
return true;
} else {
throw new PathException($path);
}
}
return false;
}
public function __construct(string $path) public function __construct(string $path)
{ {
parent::__constrct($path); parent::__constrct($path);