From f51b01089dedfb4d126a6a2f3963d0a2b8d576ef Mon Sep 17 00:00:00 2001 From: Dennis Eichhorn Date: Fri, 19 Aug 2016 16:52:38 +0200 Subject: [PATCH] Create basic functions --- System/File/Directory.php | 14 +++----------- System/File/File.php | 11 +++++++++++ Utils/StringUtils.php | 35 +++++++++++++++++++++++++++++++---- 3 files changed, 45 insertions(+), 15 deletions(-) diff --git a/System/File/Directory.php b/System/File/Directory.php index 1ce556cd9..41e632433 100644 --- a/System/File/Directory.php +++ b/System/File/Directory.php @@ -250,20 +250,12 @@ class Directory extends FileAbstract implements \Iterator, \ArrayAccess * @since 1.0.0 * @author Dennis Eichhorn */ - public static function createPath(string $path, string $permission = '0644', bool $recursive = false) : bool + public static function createPath(string $path, string $permission = '0644', bool $recursive = true) : bool { - if ($recursive && !file_exists($parent = self::getParent($path))) { - self::createPath($parent, $permission, $recursive); - } - if (!file_exists($path)) { - if (is_writable(self::getParent($path))) { - mkdir($path, $permission, true); + mkdir($path, $permission, $recursive); - return true; - } else { - throw new PermissionException($path); - } + return true; } return false; diff --git a/System/File/File.php b/System/File/File.php index 3d2e4732b..04b772282 100644 --- a/System/File/File.php +++ b/System/File/File.php @@ -122,4 +122,15 @@ class File extends FileAbstract { file_put_contents($this->path, $content); } + + public static function copy(string $p1, string $p2, bool $recursive = true) : bool + { + Directory::createPath(dirname($p2)); + + if(realpath($p1) === false) { + throw new PathException($p1); + } + + return copy($p1, $p2); + } } \ No newline at end of file diff --git a/Utils/StringUtils.php b/Utils/StringUtils.php index 02e91e8c9..6162d07ba 100644 --- a/Utils/StringUtils.php +++ b/Utils/StringUtils.php @@ -42,8 +42,8 @@ class StringUtils /** * Contains any string * - * @param string $haystack Haystack - * @param array $needles Needles + * @param string $haystack Haystack + * @param array $needles Needles * * @return bool * @@ -52,8 +52,8 @@ class StringUtils */ public static function contains(string $haystack, array $needles) : bool { - foreach($needles as $needle) { - if(strpos($haystack, $needle) !== false) { + foreach ($needles as $needle) { + if (strpos($haystack, $needle) !== false) { return true; } } @@ -268,4 +268,31 @@ class StringUtils return preg_replace('/(^[' . $charlist . ']+)/us', '', $string); } } + + /** + * Count occurences of character at the beginning of string. + * + * @param string $string String to manipulate + * @param string $character Character to count + * + * @return int + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public static function countCharacterFromStart(string $string, string $character) : int + { + $count = 0; + $length = strlen($string); + + for ($i = 0; $i < $length; $i++) { + if ($string[$i] !== $character) { + break; + } + + $count++; + } + + return $count; + } }