X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=application%2Fbookmark%2FBookmarkArray.php;h=b93281166df4ef676500eff9a4319d48ade1c81c;hb=8a6b7e96b7176e03238bbb1bcaa4c8b0c25e6358;hp=b427c91a588277f51b0a59b7ea325ebdb98f8639;hpb=336a28fa4a09b968ce4705900bf57693e672f0bf;p=github%2Fshaarli%2FShaarli.git diff --git a/application/bookmark/BookmarkArray.php b/application/bookmark/BookmarkArray.php index b427c91a..b9328116 100644 --- a/application/bookmark/BookmarkArray.php +++ b/application/bookmark/BookmarkArray.php @@ -1,5 +1,7 @@ getId() === null || empty($value->getUrl()) || ($offset !== null && ! is_int($offset)) || ! is_int($value->getId()) || $offset !== null && $offset !== $value->getId() @@ -118,7 +121,7 @@ class BookmarkArray implements \Iterator, \Countable, \ArrayAccess $realOffset = $this->getBookmarkOffset($offset); $url = $this->bookmarks[$realOffset]->getUrl(); unset($this->urls[$url]); - unset($this->ids[$realOffset]); + unset($this->ids[$offset]); unset($this->bookmarks[$realOffset]); } @@ -187,13 +190,13 @@ class BookmarkArray implements \Iterator, \Countable, \ArrayAccess /** * Returns a bookmark offset in bookmarks array from its unique ID. * - * @param int $id Persistent ID of a bookmark. + * @param int|null $id Persistent ID of a bookmark. * * @return int Real offset in local array, or null if doesn't exist. */ - protected function getBookmarkOffset($id) + protected function getBookmarkOffset(?int $id): ?int { - if (isset($this->ids[$id])) { + if ($id !== null && isset($this->ids[$id])) { return $this->ids[$id]; } return null; @@ -205,7 +208,7 @@ class BookmarkArray implements \Iterator, \Countable, \ArrayAccess * * @return int next ID. */ - public function getNextId() + public function getNextId(): int { if (!empty($this->ids)) { return max(array_keys($this->ids)) + 1; @@ -214,13 +217,14 @@ class BookmarkArray implements \Iterator, \Countable, \ArrayAccess } /** - * @param $url + * @param string $url * * @return Bookmark|null */ - public function getByUrl($url) + public function getByUrl(string $url): ?Bookmark { - if (! empty($url) + if ( + ! empty($url) && isset($this->urls[$url]) && isset($this->bookmarks[$this->urls[$url]]) ) { @@ -234,16 +238,17 @@ class BookmarkArray implements \Iterator, \Countable, \ArrayAccess * * Also update the urls and ids mapping arrays. * - * @param string $order ASC|DESC + * @param string $order ASC|DESC + * @param bool $ignoreSticky If set to true, sticky bookmarks won't be first */ - public function reorder($order = 'DESC') + public function reorder(string $order = 'DESC', bool $ignoreSticky = false): void { $order = $order === 'ASC' ? -1 : 1; // Reorder array by dates. - usort($this->bookmarks, function ($a, $b) use ($order) { + usort($this->bookmarks, function ($a, $b) use ($order, $ignoreSticky) { /** @var $a Bookmark */ /** @var $b Bookmark */ - if ($a->isSticky() !== $b->isSticky()) { + if (false === $ignoreSticky && $a->isSticky() !== $b->isSticky()) { return $a->isSticky() ? -1 : 1; } return $a->getCreated() < $b->getCreated() ? 1 * $order : -1 * $order;