Create basic functions

This commit is contained in:
Dennis Eichhorn 2016-08-19 16:52:38 +02:00
parent aca0e8ff08
commit f51b01089d
3 changed files with 45 additions and 15 deletions

View File

@ -250,20 +250,12 @@ class Directory extends FileAbstract implements \Iterator, \ArrayAccess
* @since 1.0.0 * @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com> * @author Dennis Eichhorn <d.eichhorn@oms.com>
*/ */
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 (!file_exists($path)) {
if (is_writable(self::getParent($path))) { mkdir($path, $permission, $recursive);
mkdir($path, $permission, true);
return true; return true;
} else {
throw new PermissionException($path);
}
} }
return false; return false;

View File

@ -122,4 +122,15 @@ class File extends FileAbstract
{ {
file_put_contents($this->path, $content); 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);
}
} }

View File

@ -42,8 +42,8 @@ class StringUtils
/** /**
* Contains any string * Contains any string
* *
* @param string $haystack Haystack * @param string $haystack Haystack
* @param array $needles Needles * @param array $needles Needles
* *
* @return bool * @return bool
* *
@ -52,8 +52,8 @@ class StringUtils
*/ */
public static function contains(string $haystack, array $needles) : bool public static function contains(string $haystack, array $needles) : bool
{ {
foreach($needles as $needle) { foreach ($needles as $needle) {
if(strpos($haystack, $needle) !== false) { if (strpos($haystack, $needle) !== false) {
return true; return true;
} }
} }
@ -268,4 +268,31 @@ class StringUtils
return preg_replace('/(^[' . $charlist . ']+)/us', '', $string); 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;
}
} }