<?php
namespace Shaarli\Api;
-use Shaarli\Base64Url;
use Shaarli\Api\Exceptions\ApiAuthorizationException;
+use Shaarli\Http\Base64Url;
/**
* REST API utilities
/**
* 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.
*
+ * @return bool true on success
+ *
* @throws ApiAuthorizationException the token is not valid.
*/
public static function validateJwtToken($token, $secret)
) {
throw new ApiAuthorizationException('Invalid JWT issued time');
}
+
+ return true;
}
/**
* 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.
{
$out['id'] = $link['id'];
// Not an internal link
- if ($link['url'][0] != '?') {
+ if (! is_note($link['url'])) {
$out['url'] = $link['url'];
} else {
$out['url'] = $indexUrl . $link['url'];
];
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,
+ ];
+ }
}