From cd46a3ec735cac9bc4f752fbbde1163320acc96b Mon Sep 17 00:00:00 2001 From: Joshua Ramon Enslin Date: Mon, 24 Jun 2024 16:53:01 +0200 Subject: [PATCH] Add wrapper around file_put_contents --- src/MD_STD.php | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/src/MD_STD.php b/src/MD_STD.php index 1699e29..2ac7089 100644 --- a/src/MD_STD.php +++ b/src/MD_STD.php @@ -33,6 +33,27 @@ final class MD_STD { } + /** + * Wrapper around file_put_contents, that provides catches errors on it and returns + * with type safety. + * + * @param non-empty-string $filename Filepath of the file to read. + * @param string $data Data to write. + * + * @return integer + */ + public static function file_put_contents(string $filename, string $data):int { + + $status = \file_put_contents($filename, $data); + + if ($status === false) { + throw new MDFileIsNotWritable("File {$filename} is not writable"); + } + + return $status; + + } + /** * Returns the real path of a relative file path. Throws an error rather than * returning the default false.