X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=application%2Fapi%2FApiUtils.php;h=fc5ecaf1e75931d3a2006dae957be7129bb7df1e;hb=d3f42ca487287447efb81061609644108044a038;hp=b8155a3442669e452234e8b41a2bfc780befb130;hpb=f9ff7f1b69f19b42569ffa67280807ba56f5d48a;p=github%2Fshaarli%2FShaarli.git diff --git a/application/api/ApiUtils.php b/application/api/ApiUtils.php index b8155a34..fc5ecaf1 100644 --- a/application/api/ApiUtils.php +++ b/application/api/ApiUtils.php @@ -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, + ]; + } }