]> BookStack Code Mirror - bookstack/blobdiff - app/Exports/ZipExports/ZipValidationHelper.php
ZIP Exports: Added ID checks and testing to validator
[bookstack] / app / Exports / ZipExports / ZipValidationHelper.php
index 55c86b03b5ba1ee982e34f37bb7410922ecff40a..7659c228bcdedca5d780b5ee903c33bee9652ece 100644 (file)
@@ -9,6 +9,13 @@ class ZipValidationHelper
 {
     protected Factory $validationFactory;
 
+    /**
+     * Local store of validated IDs (in format "<type>:<id>". Example: "book:2")
+     * which we can use to check uniqueness.
+     * @var array<string, bool>
+     */
+    protected array $validatedIds = [];
+
     public function __construct(
         public ZipExportReader $zipReader,
     ) {
@@ -31,6 +38,23 @@ class ZipValidationHelper
         return new ZipFileReferenceRule($this);
     }
 
+    public function uniqueIdRule(string $type): ZipUniqueIdRule
+    {
+        return new ZipUniqueIdRule($this, $type);
+    }
+
+    public function hasIdBeenUsed(string $type, int $id): bool
+    {
+        $key = $type . ':' . $id;
+        if (isset($this->validatedIds[$key])) {
+            return true;
+        }
+
+        $this->validatedIds[$key] = true;
+
+        return false;
+    }
+
     /**
      * Validate an array of relation data arrays that are expected
      * to be for the given ZipExportModel.