Implement exceptions

This commit is contained in:
Dennis Eichhorn 2017-10-07 17:14:34 +02:00
parent 0110db8edb
commit ff29e53d3c
2 changed files with 23 additions and 10 deletions

View File

@ -79,6 +79,10 @@ class Directory extends FileAbstract implements DirectoryInterface
*/ */
public static function list(string $path, string $filter = '*') : array public static function list(string $path, string $filter = '*') : array
{ {
if (!file_exists($path)) {
throw new PathException($path);
}
$list = []; $list = [];
$path = rtrim($path, '\\/'); $path = rtrim($path, '\\/');
@ -152,6 +156,10 @@ class Directory extends FileAbstract implements DirectoryInterface
*/ */
public static function size(string $dir, bool $recursive = true) : int public static function size(string $dir, bool $recursive = true) : int
{ {
if (!file_exists($dir)) {
throw new PathException($dir);
}
$countSize = 0; $countSize = 0;
$count = 0; $count = 0;
@ -180,6 +188,10 @@ class Directory extends FileAbstract implements DirectoryInterface
*/ */
public static function count(string $path, bool $recursive = true, array $ignore = []) : int public static function count(string $path, bool $recursive = true, array $ignore = []) : int
{ {
if (!file_exists($path)) {
throw new PathException($path);
}
$size = 0; $size = 0;
$files = scandir($path); $files = scandir($path);
$ignore[] = '.'; $ignore[] = '.';
@ -206,10 +218,6 @@ class Directory extends FileAbstract implements DirectoryInterface
*/ */
public static function delete(string $path) : bool public static function delete(string $path) : bool
{ {
if (!file_exists($path) || !is_dir($path)) {
throw new PathException($path);
}
$files = scandir($path); $files = scandir($path);
/* Removing . and .. */ /* Removing . and .. */
@ -247,10 +255,7 @@ class Directory extends FileAbstract implements DirectoryInterface
public static function created(string $path) : \DateTime public static function created(string $path) : \DateTime
{ {
if(!file_exists($path)) { if(!file_exists($path)) {
$created = new \DateTime(); throw new PathException($path);
$created->setTimestamp(0);
return $created;
} }
$created = new \DateTime('now'); $created = new \DateTime('now');
@ -303,6 +308,10 @@ class Directory extends FileAbstract implements DirectoryInterface
*/ */
public static function copy(string $from, string $to, bool $overwrite = false) : bool public static function copy(string $from, string $to, bool $overwrite = false) : bool
{ {
if (!is_dir($from)) {
throw new PathException($from);
}
if(!$overwrite && file_exists($to)) { if(!$overwrite && file_exists($to)) {
return false; return false;
} }
@ -330,6 +339,10 @@ class Directory extends FileAbstract implements DirectoryInterface
*/ */
public static function move(string $from, string $to, bool $overwrite = false) : bool public static function move(string $from, string $to, bool $overwrite = false) : bool
{ {
if (!is_dir($from)) {
throw new PathException($from);
}
if (!$overwrite && file_exists($to)) { if (!$overwrite && file_exists($to)) {
return false; return false;
} }

View File

@ -259,7 +259,7 @@ class File extends FileAbstract implements FileInterface
*/ */
public static function copy(string $from, string $to, bool $overwrite = false) : bool public static function copy(string $from, string $to, bool $overwrite = false) : bool
{ {
if (!file_exists($from)) { if (!is_file($from)) {
throw new PathException($from); throw new PathException($from);
} }
@ -281,7 +281,7 @@ class File extends FileAbstract implements FileInterface
*/ */
public static function move(string $from, string $to, bool $overwrite = false) : bool public static function move(string $from, string $to, bool $overwrite = false) : bool
{ {
if (!file_exists($from)) { if (!is_file($from)) {
throw new PathException($from); throw new PathException($from);
} }