X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=application%2Fapi%2FApiUtils.php;h=e51b73e187014b9663fd22d4c46587783181eb39;hb=00af48d9d20af1ce51c8ad42fe354fafc9ceb8a3;hp=d40158652d1f66680cfd5ceea17623b9a8eef5a8;hpb=9977c418d6d0de9e22e4ec276e7d476e184b5d01;p=github%2Fshaarli%2FShaarli.git diff --git a/application/api/ApiUtils.php b/application/api/ApiUtils.php index d4015865..e51b73e1 100644 --- a/application/api/ApiUtils.php +++ b/application/api/ApiUtils.php @@ -1,7 +1,7 @@ ! empty($input['title']) ? $input['title'] : $input['url'], + 'url' => $input['url'], + 'description' => ! empty($input['description']) ? $input['description'] : '', + 'tags' => ! empty($input['tags']) ? implode(' ', $input['tags']) : '', + 'private' => $private, + 'created' => new \DateTime(), + ]; + 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, + ]; + } }