From 980ed8cea90b6f7ab1b601777915dba23ab7c30e Mon Sep 17 00:00:00 2001 From: Mike Colagrosso Date: Mon, 29 Jan 2024 23:34:48 -0700 Subject: [PATCH] Convert ebook SORT_ constants to an enum --- README.md | 2 -- lib/Constants.php | 4 ---- lib/EbookSort.php | 7 +++++++ lib/Library.php | 12 ++++++------ templates/SearchForm.php | 8 ++++---- 5 files changed, 17 insertions(+), 16 deletions(-) create mode 100644 lib/EbookSort.php diff --git a/README.md b/README.md index 0593a6ce..63a6d3a1 100644 --- a/README.md +++ b/README.md @@ -134,8 +134,6 @@ Before submitting design contributions, please discuss them with the Standard Eb - Finding a self-hosted replacement for GitHub, like possibly [Gitea](https://gitea.io/) or [GitLab](https://about.gitlab.com/), and figuring out how to reproducably install and update it on the SE server. -- Converting some constants to enums, like `SORT_*` or `SOURCE_*`. - ## PHP code style - Indent with tabs. diff --git a/lib/Constants.php b/lib/Constants.php index 9cec01e0..5da9e77b 100644 --- a/lib/Constants.php +++ b/lib/Constants.php @@ -32,10 +32,6 @@ const DATABASE_DEFAULT_DATABASE = 'se'; const DATABASE_DEFAULT_HOST = 'localhost'; const EBOOKS_PER_PAGE = 12; -const SORT_NEWEST = 'newest'; -const SORT_AUTHOR_ALPHA = 'author-alpha'; -const SORT_READING_EASE = 'reading-ease'; -const SORT_LENGTH = 'length'; const ARTWORK_THUMBNAIL_HEIGHT = 350; const ARTWORK_THUMBNAIL_WIDTH = 350; diff --git a/lib/EbookSort.php b/lib/EbookSort.php new file mode 100644 index 00000000..ad52b72d --- /dev/null +++ b/lib/EbookSort.php @@ -0,0 +1,7 @@ + $tags - * @param string $sort + * @param EbookSort $sort * @return array */ public static function FilterEbooks(string $query = null, array $tags = [], string $sort = null){ @@ -24,7 +24,7 @@ class Library{ $matches = $ebooks; if($sort === null){ - $sort = SORT_NEWEST; + $sort = EbookSort::Newest->value; } if(sizeof($tags) > 0 && !in_array('all', $tags)){ // 0 tags means "all ebooks" @@ -51,13 +51,13 @@ class Library{ } switch($sort){ - case SORT_AUTHOR_ALPHA: + case EbookSort::AuthorAlpha->value: usort($matches, function($a, $b){ return strcmp(mb_strtolower($a->Authors[0]->SortName), mb_strtolower($b->Authors[0]->SortName)); }); break; - case SORT_NEWEST: + case EbookSort::Newest->value: usort($matches, function($a, $b){ if($a->Created < $b->Created){ return -1; @@ -73,7 +73,7 @@ class Library{ $matches = array_reverse($matches); break; - case SORT_READING_EASE: + case EbookSort::ReadingEase->value: usort($matches, function($a, $b){ if($a->ReadingEase < $b->ReadingEase){ return -1; @@ -89,7 +89,7 @@ class Library{ $matches = array_reverse($matches); break; - case SORT_LENGTH: + case EbookSort::Length->value: usort($matches, function($a, $b){ if($a->WordCount < $b->WordCount){ return -1; diff --git a/templates/SearchForm.php b/templates/SearchForm.php index 17ff29de..7c63a6b3 100644 --- a/templates/SearchForm.php +++ b/templates/SearchForm.php @@ -17,10 +17,10 @@ $allSelected = sizeof($tags) == 0 || in_array('all', $tags); Sort