This commit is contained in:
Joshua Ramon Enslin 2020-07-26 14:52:41 +02:00 committed by Stefan Rohde-Enslin
commit 481233efad

54
MD_STD.php Normal file
View File

@ -0,0 +1,54 @@
<?PHP
/**
* Provides type-safe overrides of default PHP functions.
*/
declare(strict_types = 1);
/**
* Standard class providing overrides of default PHP functions as static
* functions.
*/
class MD_STD {
/**
* Wrapper around file_get_contents, that provides catches errors on it and returns
* with type safety.
*
* @param string $filename Filepath of the file to read.
*
* @return string
*/
public static function file_get_contents(string $filename):string {
if (substr($filename, 0, 4) !== 'http' && !file_exists($filename)) {
throw new MDFileDoesNotExist("There is no file {$filename}");
}
$contents = file_get_contents($filename);
if (is_bool($contents)) {
throw new MDFileIsNotReadable("File {$filename} is not readable");
}
return $contents;
}
/**
* Gets contents of a folder.
*
* @param string $filepath Directory path.
*
* @return array<string>
*/
public static function scandir(string $filepath):array {
if (!is_dir($filepath) || ($output = scandir($filepath)) === false) {
throw new MDFileDoesNotExist("There is no file {$filename}");
}
return array_values(array_diff($output, ['.', '..']));
}
}