From 581944b3cf0f58c4f8c35bdf2aa41fdf9fd2b550 Mon Sep 17 00:00:00 2001 From: Dennis Eichhorn Date: Tue, 16 Jan 2018 18:45:53 +0100 Subject: [PATCH] Simplify flag checks --- README.md | 2 +- Stdlib/Base/Enum.php | 16 ++++++++++++++++ System/File/Local/File.php | 12 ++++++------ 3 files changed, 23 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index ee67105f9..3c5784431 100644 --- a/README.md +++ b/README.md @@ -25,7 +25,7 @@ Features this framework provides are: * Request/Response management * Math (e.g. matrix, forecasting, optimization, geometry, stochastics, etc.) * Module management -* Uri +* Uri * Utils (e.g. barcodes, comporession, unit converter, jobqueue, git, etc.) * Value validation * View management diff --git a/Stdlib/Base/Enum.php b/Stdlib/Base/Enum.php index 7bc7bdf0c..64146c552 100644 --- a/Stdlib/Base/Enum.php +++ b/Stdlib/Base/Enum.php @@ -138,4 +138,20 @@ abstract class Enum return count(self::getConstants()); } + /** + * Check if flag is set + * + * This only works for binary flags. + * + * @param int $flags Set flags + * @param int $checkForFlag Check if this flag is part of the set flags + * + * @return bool + * + * @since 1.0.0 + */ + public static function hasFlag(int $flags, int $checkForFlag) : bool + { + return ($flags & $checkForFlag) === $checkForFlag; + } } diff --git a/System/File/Local/File.php b/System/File/Local/File.php index 067aa1060..a634a3c14 100644 --- a/System/File/Local/File.php +++ b/System/File/Local/File.php @@ -67,14 +67,14 @@ class File extends FileAbstract implements FileInterface $exists = file_exists($path); if ( - (($mode & ContentPutMode::APPEND) === ContentPutMode::APPEND && $exists) - || (($mode & ContentPutMode::PREPEND) === ContentPutMode::PREPEND && $exists) - || (($mode & ContentPutMode::REPLACE) === ContentPutMode::REPLACE && $exists) - || (!$exists && ($mode & ContentPutMode::CREATE) === ContentPutMode::CREATE) + (ContentPutMode::hasFlag($mode, ContentPutMode::APPEND) && $exists) + || (ContentPutMode::hasFlag($mode, ContentPutMode::PREPEND) && $exists) + || (ContentPutMode::hasFlag($mode, ContentPutMode::REPLACE) && $exists) + || (!$exists && ContentPutMode::hasFlag($mode, ContentPutMode::CREATE)) ) { - if (($mode & ContentPutMode::APPEND) === ContentPutMode::APPEND && $exists) { + if (ContentPutMode::hasFlag($mode, ContentPutMode::APPEND) && $exists) { file_put_contents($path, file_get_contents($path) . $content); - } elseif (($mode & ContentPutMode::PREPEND) === ContentPutMode::PREPEND && $exists) { + } elseif (ContentPutMode::hasFlag($mode, ContentPutMode::PREPEND) && $exists) { file_put_contents($path, $content . file_get_contents($path)); } else { if (!Directory::exists(dirname($path))) {