]> git.immae.eu Git - github/shaarli/Shaarli.git/blobdiff - application/api/ApiUtils.php
REST API: implement PUT method
[github/shaarli/Shaarli.git] / application / api / ApiUtils.php
index b8155a3442669e452234e8b41a2bfc780befb130..f154bb5274a224130e40d4a495dfb1334323a1b0 100644 (file)
@@ -108,4 +108,30 @@ class ApiUtils
         ];
         return $link;
     }
+
+    /**
+     * Update link fields using an updated link object.
+     *
+     * @param array $oldLink data
+     * @param array $newLink data
+     *
+     * @return array $oldLink updated with $newLink values
+     */
+    public static function updateLink($oldLink, $newLink)
+    {
+        foreach (['title', 'url', 'description', 'tags', 'private'] as $field) {
+            $oldLink[$field] = $newLink[$field];
+        }
+        $oldLink['updated'] = new \DateTime();
+
+        if (empty($oldLink['url'])) {
+            $oldLink['url'] = '?' . $oldLink['shorturl'];
+        }
+
+        if (empty($oldLink['title'])) {
+            $oldLink['title'] = $oldLink['url'];
+        }
+
+        return $oldLink;
+    }
 }