]> git.immae.eu Git - github/shaarli/Shaarli.git/blobdiff - application/api/ApiUtils.php
namespacing: \Shaarli\Http\Base64Url
[github/shaarli/Shaarli.git] / application / api / ApiUtils.php
index b8155a3442669e452234e8b41a2bfc780befb130..e51b73e187014b9663fd22d4c46587783181eb39 100644 (file)
@@ -1,7 +1,7 @@
 <?php
 namespace Shaarli\Api;
 
-use Shaarli\Base64Url;
+use Shaarli\Http\Base64Url;
 use Shaarli\Api\Exceptions\ApiAuthorizationException;
 
 /**
@@ -12,7 +12,7 @@ class ApiUtils
     /**
      * Validates a JWT token authenticity.
      *
-     * @param string $token JWT token extracted from the headers.
+     * @param string $token  JWT token extracted from the headers.
      * @param string $secret API secret set in the settings.
      *
      * @throws ApiAuthorizationException the token is not valid.
@@ -50,7 +50,7 @@ class ApiUtils
     /**
      * Format a Link for the REST API.
      *
-     * @param array $link Link data read from the datastore.
+     * @param array  $link     Link data read from the datastore.
      * @param string $indexUrl Shaarli's index URL (used for relative URL).
      *
      * @return array Link data formatted for the REST API.
@@ -108,4 +108,46 @@ 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;
+    }
+
+    /**
+     * Format a Tag for the REST API.
+     *
+     * @param string $tag         Tag name
+     * @param int    $occurrences Number of links using this tag
+     *
+     * @return array Link data formatted for the REST API.
+     */
+    public static function formatTag($tag, $occurences)
+    {
+        return [
+            'name'       => $tag,
+            'occurrences' => $occurences,
+        ];
+    }
 }