From 794e1747481f0ce0ae643aa0695dfa68b1ed0afd Mon Sep 17 00:00:00 2001 From: root Date: Mon, 28 Nov 2022 20:33:21 +0100 Subject: [PATCH] Add function to check if object with given identifier exists --- src/SeriousJSON/JsonDatabase.php | 26 ++++++++++++++++++++++++++ src/SeriousJSON/JsonHistoryItem.php | 7 ------- 2 files changed, 26 insertions(+), 7 deletions(-) delete mode 100644 src/SeriousJSON/JsonHistoryItem.php diff --git a/src/SeriousJSON/JsonDatabase.php b/src/SeriousJSON/JsonDatabase.php index 64a9bf1..b9dd89f 100644 --- a/src/SeriousJSON/JsonDatabase.php +++ b/src/SeriousJSON/JsonDatabase.php @@ -88,6 +88,32 @@ class JsonDatabase implements IJsonUnflattener return $retPath; } + public function exists(string $class, string $name, ?int $timestamp = null): bool + { + if ($class == null || empty(trim($name))) + return false; + + if ($name != null && !empty(trim(strval($name)))) + $name = JSONDatabase::SanitizeIdentifier($name); + else + return false; + + $class = trim($class); + + if (!$this->audit && $timestamp != null) + return false; + + $objPath = $this->getObjectPath($class, $name, ($timestamp != null)); + + if (strlen($objPath) > PHP_MAXPATHLEN) + return false; + + if (!is_file($objPath)) + return false; + + return true; + } + public function load(string $class, string $name, ?int $timestamp = null): JsonSerializable|null { if ($class == null || empty(trim($name))) diff --git a/src/SeriousJSON/JsonHistoryItem.php b/src/SeriousJSON/JsonHistoryItem.php deleted file mode 100644 index 70c0ac5..0000000 --- a/src/SeriousJSON/JsonHistoryItem.php +++ /dev/null @@ -1,7 +0,0 @@ -